Max
Max

Reputation: 31

Store coefficients from several regressions in R then call coefficients into second loop

I am trying to output coefficients from multiple multi-linear regressions, store each of them and then multiply the coefficients by a future data set to predict future revenue.

There are 91 regressions total. One for each 'DBA' numbered 0 to 90. These are ran against 680 dates.

I have the loop that runs all of the regressions and outputs the coefficients. I need help storing each of the unique 91 coefficient vectors.

 x = 0
while(x<91) {
  pa.coef <- lm(formula = Final_Rev ~  OTB_Revenue + ADR + Sessions,data=subset(data, DBA == x))
  y <- coef(pa.coef)
  print(cbind(x,y))
  x = x + 1
}

After storing each of the unique vectors I need to multiply the vectors by future 'dates' to output 'predicted revenue.'

Any help would be greatly appreciated!

Thanks!

Upvotes: 2

Views: 246

Answers (1)

Parfait
Parfait

Reputation: 107652

Since you need to store data from an iteration, consider an apply function over standard loops such as for or while. And because you need to subset by a group, consider using by (the object-oriented wrapper to tapply) which slices dataframe by factor(s) and passes subsets into a function. Such a needed function would call lm and predict.lm.

Below demonstrates with random data and otherdata dataframes (10 rows per DBA group) to return a named list of predicted Final_Rev vectors (each length 10 as per their DBA group).

Data

set.seed(51718)
data <- data.frame(DBA = rep(seq(0,90), 10),
                   Sessions = sample(100:200, 910, replace=TRUE),
                   ADR = abs(rnorm(910)) * 100,
                   OTB_Revenue = abs(rnorm(910)) * 1000,
                   Final_Rev = abs(rnorm(910)) * 1000)

set.seed(8888)
other_data <- data.frame(DBA = rep(seq(0,90), 10),
                         Sessions = sample(100:200, 910, replace=TRUE),
                         ADR = abs(rnorm(910)) * 100,
                         OTB_Revenue = abs(rnorm(910)) * 1000,
                         Final_Rev = abs(rnorm(910)) * 1000)

Prediction

final_rev_predict_list <- by(data, data$DBA, function(sub){
  pa.model <- lm(formula = Final_Rev ~  OTB_Revenue + ADR + Sessions, data=sub)

  predict.lm(pa.model, new_data=other_data)      
})

final_rev_predict_list[['0']]
#         1        92       183       274       365       456       547       638       729       820 
#  831.3382 1108.0749 1404.8833 1024.4387  784.5980  455.0259  536.9992  100.5486  575.0234  492.1356

final_rev_predict_list[['45']]
#        46       137       228       319       410       501       592       683       774       865 
# 1168.1625  961.9151  536.2392 1125.5452 1440.8600 1008.1956  609.7389  728.3272 1474.5348  700.1708 

final_rev_predict_list[['90']]
#       91      182      273      364      455      546      637      728      819      910 
# 749.9693 726.6120 488.7858 830.1254 659.7508 618.7387 929.6969 584.3375 628.9795 929.3194 

Upvotes: 1

Related Questions