Prediction error in R when passing arguments in forecasts () and ar ()

When trying to compose a function from smaller ones using Rob Hindman's prediction library, for example:

> library('forecast') > arf <- function(data, ...) forecast(ar(data, order.max=1, method="ols"), ...) 

I get an error when trying to connect some data:

 > arf(ts(1:100, start=c(2000,1), frequency=4)) Error in ts(x, frequency = 1, start = 1) : object is not a matrix 

However, using the arf body directly works fine:

 > forecast(ar(ts(1:100, start=c(2000,1), frequency=4), order.max=1,method="ols")) Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 2025 Q1 101 101 101 101 101 2025 Q2 102 102 102 102 102 2025 Q3 103 103 103 103 103 2025 Q4 104 104 104 104 104 2026 Q1 105 105 105 105 105 2026 Q2 106 106 106 106 106 2026 Q3 107 107 107 107 107 2026 Q4 108 108 108 108 108 2027 Q1 109 109 109 109 109 2027 Q2 110 110 110 110 110 

Why doesn't arf work as it should?

+5
source share
2 answers

This is a problem (not quite a mistake) in forecast.ar() . All forecast.xxx() functions try to save the data used to evaluate the time series model, as required for graph and accuracy calculations. However, ar() does not return data, so forecast.ar() tries to find the data in the calling environment or in the parent environment. When you call forecast(ar(...)) directly, the function manages to find the data, but arf() puts the call on ar() one level deeper, which makes it much harder for forecast to determine which data was used.

I can change the function to make it more complex for the data (i.e. look at the environment of grandparents), but the design will still fail, because predict.ar() (part of the stats package) will lead to a similar error. It would be much better if ar() returned the data, but ar() is part of the stats package, and I do not control it.

There are several possible solutions.

  • You can replace ar with Arima :

     arf <- function(dat, ...) forecast(Arima(dat, order=c(1,0,0)), ...) 

    This should return the same model if the data is motionless (although the parameter estimates will be slightly different). It will not return the same answer for an example in your question, because time series are non-stationary.

  • Instead, you could use auto.arima() if you wanted to use more general ARIMA models than AR (1).

     arf <- function(dat, ...) forecast(auto.arima(dat, ...) 
  • (Based on the proposal from @agstudy). The workaround is to ensure that the data is stored in the ar object:

     arf <- function(dat, ...) { object <- ar(dat, order.max=1, method="ols") object$x <- dat forecast(object,...) } 
+3
source

the problem is the error in the S3 method for class ar . predict.ar try evaluating the newdata parameter with the ar object. Display first lines getS3method('predict','ar')

 function (object, newdata, n.ahead = 1L, se.fit = TRUE, ...) { if (n.ahead < 1L) stop("'n.ahead' must be at least 1") if (missing(newdata)) { newdata <- eval.parent(parse(text = object$series)) if (!is.null(nas <- object$call$na.action)) newdata <- eval.parent(call(nas, newdata)) } ..... } 

Relevant / Listened String:

  newdata <- eval.parent(parse(text = object$series)) 

But the $ series object does not have the correct expression / character. since it is hidden by a new level of arf wrapper function. Here's a workaround is to set the correct expression for this term:

 arf <- function(dat, ...) { object <- ar(dat, order.max=1, method="ols") object$series <- as.character(as.expression(as.list(match.call())$dat)) forecast(object,...) } arf( ts(1:100, start=c(2000,1), frequency=4) 

Note that; this solution also works with:

 aa <- ts(1:100, start=c(2000,1), frequency=4) arf(aa) 
+2
source

Source: https://habr.com/ru/post/1202530/


All Articles