Forecasting with tsDyn - Error in R

Viewed 1551

Good morning,
I am currently using a time series of daily sales to make forecasting.
The dataset, called myts has previously been transformed into time series object.

Whenever I run the following code, it gives me an error:

require(tsDyn)
x <- log(myts)
mod.ar <- linear(x, m=2)

Error: x must be a vector, not a ts object, do you want stats::lag()?

Best regards, Alex

3 Answers

The problem reported by Alessandro can be generated by the lag function of dplyr which overrides the lag function of stats.
Try this:

detach("package:dplyr", unload=TRUE)
library(tsDyn)
linear(log(lynx), m=2)

Here linear works correctly giving:

Non linear autoregressive model

AR model
Coefficients:
     const      phi.1      phi.2 
 2.4352150  1.3842377 -0.7477757 

Now, try this:

detach("package:tsDyn", unload=TRUE)
library(dplyr)
library(tsDyn)
linear(log(lynx), m=2)

The code gives the error message:

Error: `x` must be a vector, not a ts object, do you want `stats::lag()`?

Try this example (notice that I start with a vector and then convert it to a time series object)

require(tsDyn)

set.seed(1234)
tsdatav <- (seq(1:300)+rnorm(300,1000,10))
myts <- ts(tsdatav, frequency = 365, start = c(2017, 6))
plot(myts)

x <- log(myts)
mod.ar <- linear(x, m = 2)
mod.ar

Yes, the problem is caused by dplyr having its own version of lag, which overrides the usual lag function, as well as a mistake in the way package tseriesChaos (which tsDyn depends on) imports stas::lag. A fix has been sent and approved, but not yet submitted to CRAN: https://github.com/antoniofabio/tseriesChaos/commit/8abcc5a2d6d65588cdcec5527d4e5cb96eeccaec

In the meanwhile, you can simply overwrite lag back:

lag <- stats::lag

This should now work:

mod.ar <- linear(lynx, m=2)

And if you really want dplyr's version, use dplyr::lag `

Related