Reta
Reta

Reputation: 383

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

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?

Upvotes: 3

Views: 11830

Answers (2)

duckmayr
duckmayr

Reputation: 16920

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

Upvotes: 2

Reta
Reta

Reputation: 383

I was able to run

ForcastData <- forecast(object=testData, model=fitModel)

without no error and Now want to plot the testData and the forecasting data and check if my model is accurate:

so I did:

output <- cbind(testData, ForcastData) plot(output) and gave me the error:

Error in error(x, ...) : 
  improper length of one or more arguments to merge.xts

So when I checked ForcastData, it gave the output:

> ForcastData
        Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
2293201    -20.2831770 -308.7474 268.1810 -461.4511 420.8847
2296801    -20.1765782 -346.6400 306.2868 -519.4593 479.1061
2300401    -18.3975657 -348.8556 312.0605 -523.7896 486.9945
2304001     -2.2829565 -332.7483 328.1824 -507.6860 503.1201
2307601      2.7023277 -327.8611 333.2658 -502.8509 508.2555
2311201      4.5777316 -328.6756 337.8311 -505.0893 514.2447
2314801      4.3198927 -331.4470 340.0868 -509.1913 517.8310
2318401      3.8277285 -332.7898 340.4453 -510.9844 518.6398
2322001      1.4364973 -335.2403 338.1133 -513.4662 516.3392
2325601     -0.4013561 -337.0807 336.2780 -515.3080 514.5053

I thought I will get list of result as I have in my testData. I need to get the chart that shows 2 lines of actual data(testData), and expected data(ForcastData). I have really went through many documentation about forcast, but I can't find something explain what I wanna do.

Upvotes: 0

Related Questions