rtime-seriesforecasting

Forecasting with `tslm` returning dimension error


I'm having a similar problem to the questioners here had with the linear model predict function, but I am trying to use the "time series linear model" function from Rob Hyndman's forecasting package.

Predict.lm in R fails to recognize newdata

predict.lm with newdata

totalConv <- ts(varData[,43])
metaSearch <- ts(varData[,45])
PPCBrand <- ts(varData[,38])
PPCGeneric <- ts(varData[,34])
PPCLocation <- ts(varData[,35])
brandDisplay <- ts(varData[,29])
standardDisplay <- ts(varData[,3])
TV <- ts(varData[,2])
richMedia <- ts(varData[,46])

df.HA <- data.frame(totalConv, metaSearch,  
            PPCBrand, PPCGeneric, PPCLocation,
            brandDisplay, standardDisplay, 
            TV, richMedia)

As you can see I've tried to avoid the names issues by creating a data frame of the time series objects.

However, I then fit a tslm object (time series linear model) as follows -

fit1 <- tslm(totalConv ~ metaSearch  
             + PPCBrand + PPCGeneric + PPCLocation 
             + brandDisplay + standardDisplay 
             + TV + richMedia data = df.HA
             )

Despite having created a data frame and named all the objects properly I get the same dimension error as these other users have experienced.

Error in forecast.lm(fit1) : Variables not found in newdata
In addition: Warning messages:
1: 'newdata' had 10 rows but variables found have 696 rows 
2: 'newdata' had 10 rows but variables found have 696 rows

the model frame seems to give sensible names to all of the variables, so I don't know what is up with the forecast function:-

names(model.frame(fit1))
[1] "totalConv"       "metaSearch"      "PPCBrand"        "PPCGeneric"      "PPCLocation"     "brandDisplay"   
[7] "standardDisplay" "TV"              "richMedia" 

Can anyone suggest any other improvements to my model specification that might help the forecast function to run?

EDIT 1: Ok, just so there's a working example, I've used the data given in Irsal's answer to this question (converting to time series objects) and then fitted the tslm. I get the same error (different dimensions obviously):-

Is there an easy way to revert a forecast back into a time series for plotting?

I'm really confused about what I'm doing wrong, my code looks identical to that used in all of the examples on this....

data <- c(11,53,50,53,57,69,70,65,64,66,66,64,61,65,69,61,67,71,74,71,77,75,85,88,95,
           93,96,89,95,98,110,134,127,132,107,94,79,72,68,72,70,66,62,62,60,59,61,67,
           74,87,112,134,51,50,38,40,44,54,52,51,48,50,49,49,48,57,52,53,50,50,55,50,
           55,60,65,67,75,66,65,65,69,72,93,137,125,110,93,72,61,55,51,52,50,46,46,45,
           48,44,45,53,55,65,89,112,38,7,39,35,37,41,51,53,57,52,57,51,52,49,48,48,51,
           54,48,50,50,53,56,64,71,74,66,69,71,75,84,93,107,111,112,90,75,62,53,51,52,
           51,49,48,49,52,50,50,59,58,69,95,148,49,83,40,40,40,53,57,54,52,56,53,55,
           55,51,54,45,49,46,52,49,50,57,58,63,73,66,63,72,72,71,77,105,97,104,85,73,
           66,55,52,50,52,48,48,46,48,53,49,58,56,72,84,124,76,4,40,39,36,38,48,55,49,
           51,48,46,46,47,44,44,45,43,48,46,45,50,50,56,62,53,62,63)
 
 data2 <- c(rnorm(237))


library(forecast)
 


 nData <- ts(data)
 nData2 <- ts(data2)
 dat.ts <- tslm(nData~nData2)
 forecast(dat.ts)

The last line gives:

Error in forecast.lm(dat.ts) : Variables not found in newdata
In addition: Warning messages:
1: 'newdata' had 10 rows but variables found have 237 rows 
2: 'newdata' had 10 rows but variables found have 237 rows 

EDIT 2: Same error even if I combine both series into a data frame.

nData.df <- data.frame(nData, nData2)
dat.ts <- tslm(nData~nData2, data = nData.df)
forecast(dat.ts)

Solution

  • tslm fits a linear regression model. You need to provide the future values of the explanatory variables if you want to forecast. These should be provided via the newdata argument of forecast.lm.