0

I have a problem with my R code. I would like to run about 100 regressions and perform this process with a loop. I have tried to program the loop myself using help from YouTube and the like, but I am getting nowhere. Therefore, I would like to ask you if you can help me. Specifically, it's about the following: I have a dataset of the 100 companies in the Nasdaq100 and I would like to regress the sales per share with the stock price performance on a quarterly basis. Another problem is that the data set contains these 100 companies and a subset with the respective ticker symbol has to be created for each additional company so that R can access it correctly for each regression.

Here is an excerpt from the code:

Nasdaq_100 = read_xlsx("Nasdaq_100_Sales_Data.xlsx")

#Correlation between quarterly close price and Sales of AMD
AMD <- subset (Nasdaq_100, Nasdaq_100$TickerSymbol=="AMD")
AMD_regression = lm(AMD$Sales ~ AMD$Stockprice_quarterly, data = Nasdaq_100)
summary(AMD_regression)

Can you help me to program this loop for regression analysis? - I would like to analyse all 100 companies.

This is a sample I created to show the structure of my dataset:

TickerSymbol Quarter Sales Stockprice_quarterly
AMD 31.03.2021 $0.45 502.500
AMD 31.12.2020 $1.47 361.100
AMD 30.09.2020 $0.32 280.700
AMD 30.06.2020 $0.13 377.400
AMD 31.03.2020 $0.14 296.900
AMD 31.12.2019 $0.15 274.800
AMD 30.09.2019 $0.11 561.200
AMD 30.06.2019 $0.03 548.650
AMD 31.03.2019 $0.01 509.977
AAPL 31.03.2021 $1.40 359.038
AAPL 31.12.2020 $1.68 358.514
AAPL 30.09.2020 $0.75 357.991
AAPL 30.06.2020 $0.65 357.467
AAPL 31.03.2020 $0.64 356.944
AAPL 31.12.2019 $1.25 356.421
AAPL 30.09.2019 $0.77 355.897
AAPL 30.06.2019 $0.55 355.374
AAPL 31.03.2019 $0.62 354.851
EBAY 31.03.2021 $0.92 325.020
EBAY 31.12.2020 $1.39 324.496
EBAY 30.09.2020 $0.94 323.973
EBAY 30.06.2020 $1.05 323.449
EBAY 31.03.2020 $4.51 322.926
EBAY 31.12.2019 $0.69 322.403
EBAY 30.09.2019 $0.37 321.879
EBAY 30.06.2019 $0.46 321.356
EBAY 31.03.2019 $0.57 320.833

Thanks in advance for any help!

1 Answer 1

0

You can use one of the apply functions like so.

Nasdaq_100 <- read.table(
    header = T,
    text = "TickerSymbol Quarter Sales Stockprice_quarterly
                            AMD 31.03.2021 0.45 502.500
                            AMD 31.12.2020 1.47 361.100
                            AMD 30.09.2020 0.32 280.700
                            AMD 30.06.2020 0.13 377.400
                            AMD 31.03.2020 0.14 296.900
                            AMD 31.12.2019 0.15 274.800
                            AMD 30.09.2019 0.11 561.200
                            AMD 30.06.2019 0.03 548.650
                            AMD 31.03.2019 0.01 509.977
                            AAPL 31.03.2021 1.40 359.038
                            AAPL 31.12.2020 1.68 358.514
                            AAPL 30.09.2020 0.75 357.991
                            AAPL 30.06.2020 0.65 357.467
                            AAPL 31.03.2020 0.64 356.944
                            AAPL 31.12.2019 1.25 356.421
                            AAPL 30.09.2019 0.77 355.897
                            AAPL 30.06.2019 0.55 355.374
                            AAPL 31.03.2019 0.62 354.851
                            EBAY 31.03.2021 0.92 325.020
                            EBAY 31.12.2020 1.39 324.496
                            EBAY 30.09.2020 0.94 323.973
                            EBAY 30.06.2020 1.05 323.449
                            EBAY 31.03.2020 4.51 322.926
                            EBAY 31.12.2019 0.69 322.403
                            EBAY 30.09.2019 0.37 321.879
                            EBAY 30.06.2019 0.46 321.356
                            EBAY 31.03.2019 0.57 320.833"
)

my_regression <- function(subset_df) {
    subset_regression <- lm(Sales ~ Stockprice_quarterly, data = subset_df)
    subset_regression
}

ss <- lapply(unique(Nasdaq_100$TickerSymbol), function(ticker) 
    my_regression(subset(Nasdaq_100, Nasdaq_100$TickerSymbol == ticker)))

This is the result.

ss[[1]]
#Call:
#lm(formula = Sales ~ Stockprice_quarterly, data = subset_df)

#Coefficients:
         (Intercept)  Stockprice_quarterly  
           0.6717698            -0.0008715  

And you can access parts like this

ss[[1]]$coefficients
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much for your quick reply. I got the results as you did. Is there any way to get the results in more detail for each share? In clear values, so to speak? And also do you know how to calculate the correlation of each stock between Sales and Stock price quarterly using an apply function? Many thanks in advance!
Using lapply instead perhaps gives you more control. I'll edit the answer
Thank you! It worked very well with lapply. Now I'm thinking about how I can calculate the correlation with the lapply function between sales and share price - as a quick reference, so to speak. I tried this - but doens't work: ###correlation my_correlation <- function(subset_df) { subset_correlation <- image(cor(subset_df), x=Sales, y=Stockprice_quarterly) subset_correlation } ss <- lapply(unique(Nasdaq_100$TickerSymbol), function(ticker) my_correlation(subset(Nasdaq_100, Nasdaq_100$TickerSymbol == ticker))) I'm quite new to R - so please don't be surprised :)
You should raise a new post for each new question to follow the one question per post rule. And if you think the answer is acceptable you should mark it as such so that others can quickly find answers to questions.
Okay, I will ask a new question for that. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.