Skip to main content
2 of 2
improved formatting
Nick Cox
  • 59.5k
  • 8
  • 136
  • 212

Forecast using multivariate multiple linear regression using R for multiple dependent variables

I'm trying to do predict using multiple linear regression in R. I have been able to do the multiple regression bit, by converting raw data to data table. However, when i'm trying to use predict function, i'm unable to do it at multiple variable level, for each SKU ... as given below.

SKU Sales   Week    AP  Discount
101451  1308    2010_19 3.48    0.1
101451  1678    2010_20 3.5 0
101451  2003    2010_21 3.5 0.12
495172  168 2012_18 5.36    0
495172  138 2012_19 5.15    0
495172  97  2012_20 4.99    0
495172  377 2012_21 3.5 0.26
586606  130 2012_7  7.24    0
586606  143 2012_8  6.46    0.07
586606  1098    2012_9  4.99    0.28

The code I have used is (mlr is the rawdata)

library(data.table)
mlrt<- data.table(SKU=mlr$SKU,Sales=mlr$Sales,Week=mlr$Week,AP=mlr$AP,Discount=mlr$Discount)

# Run MLR model, by SKU,Display multiple Intercepts and slopes, SKU wise
mlrt[,list(intercept=coef(lm(Sales~AP+Discount))   [1],SLOPE1=coef(lm(Sales~AP+Discount))[2],SLOPE2=coef(lm(Sales~AP+Discount))[3]),by=SKU]

I'm unable to figure out how to predict using predict function here, for each SKU separately. (Sales is DV and AP and Discount are IDV)