Search code examples
loopslinear-regressionstock

Linear Regression with Loop


I'm currently doing a regression analysis for every company on the Norwegian Stock Market, where i regress the stockreturns for each company against a benchmark. The period is 2009-2018. I've managed to do the regression for each company throughout the whole period, but i also want to do a regression for each month for every company. Our dataset contains monthly stockreturns.

The original dataset consists of 26000 observations, which i've then converted into subsets with a total of 390 elements(companies).

What i've done so far is shown below:

data_subset <- by(data,data$Name, subset)
data_lm <-lapply(data_subset,function(data) lm(data$CompanyReturn~data$DJReturn))
data_coef <- lapply(data_lm, coef)
data_tabell <- matrix(0,length(data_subset),2)
for (i in 1:length(data_subset)) {
data_tabell[i,]<-coef(data_lm[[i]])
}
colnames(data_tabell)<-c("Intercept","Coefficient")
rownames(data_tabell)<-names(data_subset)

Do anyone know how i can specify that i want to only do a regression for a company for a specific period, for example each year or each month for every company?

Thank you in advance for the help!


Solution

  • If you use tidyverse package in this case, it will be a lot easier for you to do all those things. Let's say you have your data in the following structure,

    data:
    
    | CompanyID | CompanyName | Date       | CompanyReturn | DJReturn |
    |-----------|-------------|------------|---------------|----------|
    | 1         | abc         | 01-01-2002 | 500           | 250      |
    |           |             |            |               |          |
    |           |             |            |               |          |
    

    Using tidyVerse, you can just use group_by to create the subset and run run the regression,

    data %>%
      group_by(CompanyID, month = month(as.Date(Date))) %>%
      nest() %>%
      mutate(model = map(data, ~lm(CompanyReturn ~ DJReturn, data = .)))
    

    This will give you a table with columns CompanyID, month and model, where the column model is called a list column and contains the corresponding fitted linear regression model.