Error in rep(1, n.ahead) : invalid 'times' argument in R

Reta picture Reta · Nov 5, 2017 · Viewed 8.2k times · Source

I'm working on dataset to forecast with ARIMA, and I'm so close to the last step but I'm getting error and couldn't find reference to figure out what I'm missing.

I keep getting error message when I do the following command:

ForcastData<-forecast(fitModel,testData)

Error in rep(1, n.ahead) : invalid 'times' argument

I'll give brief view on the work I did where I have changed my dataset from data frame to Time series and did all tests to check volatility, and Detect if data stationary or not.
Then I got the DataAsStationary as good clean data to apply ARIMA, but since I wanna train the model on train data and test it on the other part of the data, I splitted dataset into training 70% and testing 30%:

ind <-sample(2, nrow(DataAsStationary), replace = TRUE, prob = c(0.7,0.3))
traingData<- DataStationary1[ind==1,]
testData<- DataStationary1[ind==2,]

I used Automatic Selection Algorithm and found that Arima(2,0,3) is the best.

autoARIMAFastTrain1<- auto.arima(traingData, trace= TRUE, ic ="aicc", approximation = FALSE, stepwise = FALSE)

I have to mentioned that I did check the if residuals are Uncorrelated (White Noise) and deal with it.

library(tseries)
library(astsa)
library(forecast)

After that I used the training dataset to fit the model:

fitModel <- Arima(traingData, order=c(2,0,3))
fitted(fitModel)

ForcastData<-forecast(fitModel,testData)
output <- cbind(testData, ForcastData)
accuracy(testData, ForcastData) 
plot(outp)

Couldn't find any resource about the error:

Error in rep(1, n.ahead) : invalid 'times' argument

Any suggestions!! Really

I tried

ForcastData<-forecast.Arima(fitModel,testData)

but I get error that

forecast.Arima not found !

Any idea why I get the error?

Answer

duckmayr picture duckmayr · Nov 5, 2017

You need to specify the arguments to forecast() a little differently; since you didn't post example data, I'll demonstrate with the gold dataset in the forecast package:

library(forecast)
data(gold)
trainingData <- gold[1:554]
testData <- gold[555:1108]
fitModel <- Arima(trainingData, order=c(2, 0, 3))
ForcastData <- forecast(fitModel, testData)
# Error in rep(1, n.ahead) : invalid 'times' argument
ForcastData <- forecast(object=testData, model=fitModel) # no error
accuracy(f=ForcastData) # you only need to give ForcastData; see help(accuracy)
                    ME     RMSE      MAE        MPE      MAPE     MASE
Training set 0.4751156 6.951257 3.286692 0.09488746 0.7316996 1.000819
                   ACF1
Training set -0.2386402

You may want to spend some time with the forecast package documentation to see what the arguments for the various functions are named and in what order they appear.

Regarding your forecast.Arima not found error, you can see this answer to a different question regarding the forecast package -- essentially that function isn't meant to be called by the user, but rather called by the forecast function.

EDIT:

After receiving your comment, it seems the following might help:

library(forecast)
# Read in the data
full_data <- read.csv('~/Downloads/onevalue1.csv')
full_data$UnixHour <- as.Date(full_data$UnixHour)
# Split the sample
training_indices <- 1:floor(0.7 * nrow(full_data))
training_data <- full_data$Lane1Flow[training_indices]
test_data <- full_data$Lane1Flow[-training_indices]
# Use automatic model selection:
autoARIMAFastTrain1 <- auto.arima(training_data, trace=TRUE, ic ="aicc",
                                  approximation=FALSE, stepwise=FALSE)
# Fit the model on test data:
fit_model <- Arima(training_data, order=c(2, 0, 3))
# Do forecasting
forecast_data <- forecast(object=test_data, model=fit_model)
# And plot the forecasted values vs. the actual test data:
plot(x=test_data, y=forecast_data$fitted, xlab='Actual', ylab='Predicted')

enter image description here

# It could help more to look at the following plot:
plot(test_data, type='l', col=rgb(0, 0, 1, alpha=0.7),
     xlab='Time', ylab='Value', xaxt='n', ylim=c(0, max(forecast_data$fitted)))
ticks <- seq(from=1, to=length(test_data), by=floor(length(test_data)/4))
times <- full_data$UnixHour[-training_indices]
axis(1, lwd=0, lwd.ticks=1, at=ticks, labels=times[ticks])
lines(forecast_data$fitted, col=rgb(1, 0, 0, alpha=0.7))
legend('topright', legend=c('Actual', 'Predicted'), col=c('blue', 'red'),
       lty=1, bty='n')

enter image description here