Why is the error while running Accurracy function in R programming?

Viewed 58

I am trying to look for the accuracy of the predicted model using accuracy function in r programming. But I get the below error in R Studio. Do I have to change the mode of predicted variable or something? I see the class as ts and mode as numeric already.

I don't understand why, any help would be much appreciated. Below is the full code.

dput results:

dput(Enrollment_Data)
structure(list(COUNT = c(17L, 1L, 5L, 8L, 45L, 21L, 18L, 43L, 
82L, 116L, 192L, 289L, 242L, 254L, 335L, 138L, 71L, 98L, 91L, 
138L, 175L, 232L, 155L, 376L, 197L, 271L, 421L), Enrolment_date = structure(c(25L, 
20L, 5L, 10L, 8L, 16L, 1L, 18L, 14L, 12L, 3L, 26L, 23L, 21L, 
6L, 11L, 9L, 17L, 2L, 19L, 15L, 13L, 4L, 27L, 24L, 22L, 7L), .Label = c("APR2018", 
"APR2019", "AUG2018", "AUG2019", "DEC2017", "DEC2018", "DEC2019", 
"FEB2018", "FEB2019", "JAN2018", "JAN2019", "JUL2018", "JUL2019", 
"JUN2018", "JUN2019", "MAR2018", "MAR2019", "MAY2018", "MAY2019", 
"NOV2017", "NOV2018", "NOV2019", "OCT2018", "OCT2019", "SEP2017", 
"SEP2018", "SEP2019"), class = "factor")), class = "data.frame", row.names = c(NA, 
-27L))

data:

 COUNT Enrolment_date
1     17        SEP2017
2      1        NOV2017
3      5        DEC2017
4      8        JAN2018
5     45        FEB2018
6     21        MAR2018
7     18        APR2018
8     43        MAY2018
9     82        JUN2018
10   116        JUL2018
11   192        AUG2018
12   289        SEP2018
13   242        OCT2018
14   254        NOV2018
15   335        DEC2018
16   138        JAN2019
17    71        FEB2019
18    98        MAR2019
19    91        APR2019
20   138        MAY2019
21   175        JUN2019
22   232        JUL2019
23   155        AUG2019
24   376        SEP2019
25   197        OCT2019
26   271        NOV2019
27   421        DEC2019

Error:

> accuracy(forecast1)
Error in accuracy.default(forecast1) : 
  First argument should be a forecast object or a time series.

Full Code:

Enrollment_Data <- read.csv('RateT0.csv')

head(Enrollment_Data)

#load packages
library(tseries)
library(ggplot2)
library(forecast)


attach(Enrollment_Data)
#Data Exploration

plot(COUNT)
#Forecasting Returns

adf.test(COUNT, alternative="stationary")

d.COUNT <- diff(COUNT, differences = 3)
summary(COUNT)
summary(d.COUNT)

plot(d.COUNT)

adf.test(d.COUNT, alternative="stationary")

acf(d.COUNT)
pacf(d.COUNT)

#Step 2: Model Estimation

forecast::auto.arima(d.COUNT)

#Step 4: Diagnosis
arima.final <-arima(d.COUNT, c(3,0,0))

tsdiag(arima.final)

'Choose the one that has least AIC and significant co-efficients'

#arima.final <-arima(COUNT, c(3,3,1))

forecast1 <- predict(arima.final,n.ahead = 12)
forecast1

summary(forecast1)
accuracy(forecast1)
plot(d.COUNT)

1 Answers

The error speaks pretty clearly: the first argument of accuracy must be an object of class forecast or ts.

If you run class(forecast1) you will get list and that's why you're getting that error message.

Now, there are many ways to correct your code. Considering the structure of your code I suggest you the following one.

Change this

forecast1 <- predict(arima.final,n.ahead = 12)

into this:

forecast1 <- forecast(arima.final, h = 12)

Differently from the predict function, forecast returns an object of class forecast, which will make accuracy works as expected.

Related