No applicable method for "accuracy" applied to an object of class "forecast" while knitting R

Hi! I am having difficulty knitting R with the following codes. The function accuracy () works properly to give its outputs in an rmd file, but when I attempted to knit it, I received the error depicted in the image below.

I thought accuracy is a function that takes both forecast and ts objects.. I have no clue what is going on here.
Any help will be much appreciated!

w_data = writing 

library(e1071)
library(caret)
library(forecast)
library(dplyr)

w_training = window(w_data, start=1968, end=c(1975,12))
w_test = window(w_data, start = 1976)

naive_test = stlf(w_training,t.window=13,lambda = BoxCox.lambda(w_training),s.window="periodic",robust=TRUE,method="naive",h=length(w_test))
accuracy(naive_test, w_test)

rwdrift_test = stlf(w_training,t.window=13,lambda = BoxCox.lambda(w_training),s.window="periodic",robust=TRUE,method="rwdrift", h=length(w_test))
accuracy(rwdrift_test, w_test)

(I got the output from above codes as below but keep having an error msg while knitting it)

It's hard to tell what the error is without a reprex (FAQ: What's a reproducible example (`reprex`) and how do I create one?). Here is an example that works with your code.

library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo

w_data <- ts(abs(rnorm(200)), start = 1968, frequency = 12)
w_training <- window(w_data, start = 1968, end = c(1975, 12))
w_test <- window(w_data, start = 1976)

naive_test <- stlf(w_training, t.window = 13, 
  lambda = BoxCox.lambda(w_training), s.window = "periodic", 
  robust = TRUE, method = "naive", h = length(w_test))
accuracy(naive_test, w_test)
#>                       ME      RMSE       MAE       MPE      MAPE      MASE
#> Training set  0.00394147 0.7864543 0.6299847 -4068.137 4128.5315 0.9773049
#> Test set     -0.17644808 0.6118244 0.5208971  -306.057  327.5739 0.8080756
#>                    ACF1 Theil's U
#> Training set -0.4930051        NA
#> Test set     -0.1100589  0.490973

rwdrift_test <- stlf(w_training, t.window = 13, 
  lambda = BoxCox.lambda(w_training), s.window = "periodic", 
  robust = TRUE, method = "rwdrift", h = length(w_test))
accuracy(rwdrift_test, w_test)
#>                        ME      RMSE       MAE        MPE      MAPE      MASE
#> Training set -0.001135496 0.7866437 0.6304382 -4105.1823 4164.9380 0.9780084
#> Test set     -0.454331138 0.7537172 0.6379784  -425.3502  436.0732 0.9897057
#>                     ACF1 Theil's U
#> Training set -0.49300666        NA
#> Test set     -0.05154857 0.5016373

Created on 2023-01-31 with reprex v2.0.2

Thank you for your work! The data I pulled off the training & test set was writing series from the package fpp2? Unfortunately, even after using your codes, I still fail to knit it with the same error message. :confused:

Please create a reprex: FAQ: What's a reproducible example (`reprex`) and how do I create one?

Otherwise there is no way to know what the issue is.

I followed the guideline. I am not sure if this is a way to be done. Thanks in advance

library(fpp2)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> ── Attaching packages ────────────────────────────────────────────── fpp2 2.4 ──
#> ✔ ggplot2   3.4.0     ✔ fma       2.4  
#> ✔ forecast  8.20      ✔ expsmooth 2.3
#> 
w_data = writing 
head(w_data,3)
#>          Jan     Feb     Mar
#> 1968 562.674 599.000 668.516

tsdisplay(w_data)

autoplot(w_data)

ggsubseriesplot(w_data, main = NULL) 

Created on 2023-01-31 with reprex v2.0.2

# Split into a 80-20 on training & test data 
w_training = window(w_data, start=1968, end=c(1975,12))
#> Error in window(w_data, start = 1968, end = c(1975, 12)): object 'w_data' not found
# w_training = head(w_data, 96)
w_test = window(w_data, start = 1976)
#> Error in window(w_data, start = 1976): object 'w_data' not found
# w_test = tail(w_data, 24)

# library(e1071)
# library(caret)
# library(dplyr)
library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
naive_test = stlf(w_training,t.window=13,lambda = BoxCox.lambda(w_training),s.window="periodic",robust=TRUE,method="naive",h=length(w_test))
#> Error in NCOL(x): object 'w_training' not found
accuracy(naive_test, w_test)
#> Error in accuracy(naive_test, w_test): object 'naive_test' not found

rwdrift_test = stlf(w_training,t.window=13,lambda = BoxCox.lambda(w_training),s.window="periodic",robust=TRUE,method="rwdrift", h=length(w_test))
#> Error in NCOL(x): object 'w_training' not found
accuracy(rwdrift_test, w_test)
#> Error in accuracy(rwdrift_test, w_test): object 'rwdrift_test' not found

Created on 2023-01-31 with reprex v2.0.2

This is two separate reprexes. As you can see from the errors, the second one can't access the data. If you put them in the same reprex, it should work.

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.