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

multiple regression

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)

Best Answer

Just use lm to run the linear model and then "predict" with the original data or new data

First, your linear regression

my_reg <-  lm(Sales ~ AP + discount, data = mlr)

If you want to the fitted values, you can use

fitted.sales <-  predict(my_reg)

If you want to use your estimates to predict sales on new data, use "newdata":

predicted.sales <- predict(my_reg, newdata = my_newdata)
Related Question