nelakell
nelakell

Reputation: 189

forecast.lm predicts always the same time period ahead

I want to predict a linear model, which I estimated by ols. However, it always forecasts the same time period ahead, which is of the same length as my data set.

Here is what I have done.

data <- ts(matrix(rnorm(144, mean=0, sd=1), ncol=6), start=c(2007,1), frequency=12)

I computed two factors.

factors <- ts(t(t(eigen(cor(data))$vectors[,1:2] %*% 
         sqrt(diag(eigen(cor(data))$values[1:2]))) %*% 
         t(scale(data))), start=c(2007,1), frequency=12)
colnames(factors) <- c("f1", "f2")

I combined the factors with the data set.

favar <- ts.union(factors, data)
colnames(favar) <- c(colnames(factors), "a", "b", "c", "d", "e", "f")

Then, I estimated a linear model for "a".

require(forecast)
model <- tslm(a ~ f1 + f2 + b + c + d + e + f + 0, data=ts(sapply(favar, function(x) 
             lag(x, h=1))[-1,], start=c(2007, 2), frequency=12))

If I now forecast my model, it takes the same length for the time period ahead as my data set.

forecast(model, newdata=favar, h=6, ts=T)

It doesn't matter what value I set for h, the result is always a 24 month ahead forecast. I think, the problem occurs, because I have to provide the newdata, for which I used my original data set favar. However, if I try to forecast the model without it, I get the following error:

Error in eval(expr, envir, enclos) : object 'f1' not found

I've already tried to forecast it with predict.lm and estimating the model only with lm instead of tslm. In any case, I face the same problem: the forecasting period is always the same as the length of the newdata provided.

Update: I've just noticed that not only the length of my forecast is the same as my data set, but also the values. Basically, I have just a copy of my original data.

Thank you for your help.

Upvotes: 3

Views: 5055

Answers (1)

mra68
mra68

Reputation: 2960

forecast(model, newdata=favar, h=6, ts=T) calls forecast.lm. From the documentation for forecast.lm:

newdata

An optional data frame in which to look for variables with which to predict. If omitted, it is assumed that the only variables are trend and season, and h forecasts are produced.

h

Number of periods for forecasting. Ignored if newdata present.

Hence the reason for the error

> forecast(model,h=6,ts=T)
Error in eval(expr, envir, enclos) : object 'f1' not found
> 

is that the only variables known are trend and season, not f1, f2. etc. So newdata must not be missing and therefore h is ignored.

I'm afraid to get a forecast of length 6 from the forecast method you need some newdata of length 6. Then the linear function determined by the coefficients coef(model) is evaluated at these 6 points.

Of course you can ask the coefficients

> coef(model)
       f1        f2         b         c         d         e         f 
 2.008211  1.344910 -0.532548 -1.375166  0.378199  2.169784 -1.971422 

and use them without the forecast method.

> myData <- X[1:6,-3] + matrix(sample(-100:100,6*7,,replace=TRUE)/100,6,7)
> myData
             f1         f2          b          c          d          e          f
[1,]  1.3901181  0.5794323  0.2638713  1.7911077 -1.9140976 -0.1632654  1.2130388
[2,] -0.5106604  1.0037957 -0.5357955  1.1981059 -0.3636334 -1.2746126 -0.1845794
[3,]  2.0191347 -0.8724608 -1.7707524  0.2779736  1.2814462 -0.4834006  0.1504435
[4,]  1.4574348  0.2173202 -1.1881501  0.7911197 -0.7332919 -1.0103667 -0.8201907
[5,] -1.8129340  0.2294362  0.7379416 -1.3893631  0.5011054  0.4321159  0.4026663
[6,]  1.9659584  1.8596798  0.7286796  1.9930237  0.6643413 -0.2609216 -0.2635644
> fcst <- myData %*% coef(model)
> fcst
          [,1]
[1,] -2.502231
[2,] -3.577032
[3,]  2.581397
[4,]  1.911273
[5,] -1.481277
[6,]  3.525071
> forecast(model,myData,ts=T)
         Point Forecast     Lo 80     Hi 80     Lo 95     Hi 95
Jan 2009      -2.502231 -2.502231 -2.502231 -2.502231 -2.502231
Feb 2009      -3.577032 -3.577032 -3.577032 -3.577032 -3.577032
Mar 2009       2.581397  2.581397  2.581397  2.581397  2.581397
Apr 2009       1.911273  1.911273  1.911273  1.911273  1.911273
May 2009      -1.481277 -1.481277 -1.481277 -1.481277 -1.481277
Jun 2009       3.525071  3.525071  3.525071  3.525071  3.525071
> 

The name of the functionforecast is a bit misleading. forecast just calculates a forecasted value of a if forecasted values of f1,f2,b,c,d,e and f are given as newdata.

Upvotes: 1

Related Questions