Search code examples
rforecastingcross-validation

R: use forecast::accuracy() on split data


Having a hard time getting the accuracy() function from {forecast} to work on predicted test values.

First, build the LM model on the training data (here for reproducibility):

library(ISLR)
set.seed(1)
train <- sample(392, 196)
lm.fit <- lm(mpg~horsepower, data = Auto, subset = train)

Then compute the MSE of the test data:

mean((auto$mpg - predict(lm.fit, Auto))[-train]^2)

My goal is to use forecast::accuracy() to get MSE (rather than the above) and additional measures of error. However, I simply can not get it to run, no matter what I feed it. This is definitely user error, and looking for any thoughts out there.

I know forecast::accuracy() does not contain MSE "out of the box" but I plan on computing it via accuracy(data)[, 2]^2 and merging with the other output.


Solution

  • accuracy(forecast(lm.fit, newdata=Auto[-train,]), Auto$mpg[-train])[,2]^2