Smoa Faso
Smoa Faso

Reputation: 27

R Harmonic Prediction Failing - newdata structure

I am forecasting a time series using harmonic regression created as such:

(Packages used: tseries, forecast, TSA, plyr)

airp <- AirPassengers
TIME <- 1:length(airp)
SIN <- COS <- matrix(nrow = length(TIME), ncol = 6,0)
for (i in 1:6){
  SIN[,i] <- sin(2*pi*i*TIME/12)
  COS[,i] <- cos(2*pi*i*TIME/12)
}
SIN <- SIN[,-6]

decomp.seasonal <- decompose(airp)$seasonal

seasonalfit <- lm(airp ~ SIN + COS)

The fitting works just fine. The problem occurs when forecasting.

TIME.NEW <- seq(length(TIME)+1, length(TIME)+12, by=1)
SINNEW <- COSNEW <- matrix(nrow=length(TIME.NEW), ncol = 6, 0)
for (i in 1:6) {
  SINNEW[,i] <- sin(2*pi*i*TIME.NEW/12)
  COSNEW[,i] <- cos(2*pi*i*TIME.NEW/12)
}
SINNEW <- SINNEW[,-6]

prediction.harmonic.dataframe <- data.frame(TIME = TIME.NEW, SIN = SINNEW, COS = COSNEW)

seasonal.predictions <- predict(seasonalfit, newdata = prediction.harmonic.dataframe)

This causes the warning:

Warning message:
'newdata' had 12 rows but variables found have 144 rows

I went through and found that the names were SIN.1, SIN.2, et cetera, instead of SIN1 and SIN2... So I manually changed those and it still didn't work. I also manually removed the SIN.6 because it, for some reason, was still there.

Help?

Edit: I have gone through the similar posts as well, and the answers in those questions did not fix my problem.

Upvotes: 0

Views: 430

Answers (1)

MrFlick
MrFlick

Reputation: 206187

Trying to predict with a data.frame after fitting an lm model with variables not inside a data.frame (especially matrices) is not fun. It's better if you always fit your model from data in a data.frame.

For example if you did

seasonalfit <- lm(airp ~ ., data.frame(airp=airp,SIN=SIN,COS=COS))

Then your predict would work.

Alternatively you can try to cram matrices into data.frames but this is generally a bad idea. You would do

prediction.harmonic.dataframe <- data.frame(TIME = TIME.NEW, 
    SIN = I(SINNEW), COS = I(COSNEW))

The I() (or AsIs function) will keep them as matrices.

Upvotes: 2

Related Questions