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
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
I am getting the following error when I try to refit the ARIMA model.
new_model <- Arima(data,model=old_model)
Error in Ops.Date(driftmod$coeff[2], time(x)) :
* not defined for "Date" objects
Note: The class of data is zoo. I also tried using xts, but I got the same error.
Edit: As suggested by Joshua, here is the reproducible example.
library('zoo')
library('forecast')
#Creating sample data
sample_range <- seq(from=1, to=10, by=1)
x<-sample(sample_range, size=61, replace=TRUE)
ts<-seq.Date(as.Date('2017-03-01'),as.Date('2017-04-30'), by='day')
dt<-data.frame(ts=ts,data=x)
#Split the data to training set and testing set
noOfRows<-NROW(dt)
trainDataLength=floor(noOfRows*0.70)
trainData<-dt[1:trainDataLength,]
testData<-dt[(trainDataLength+1):noOfRows,]
# Use zoo, so that we get dates as index of dataframe
trainData.zoo<-zoo(trainData[,2:ncol(trainData)], order.by=as.Date((trainData$ts), format='%Y-%m-%d'))
testData.zoo<-zoo(testData[,2:ncol(testData)], order.by=as.Date((testData$ts), format='%Y-%m-%d'))
#Create Arima Model Using Forecast package
old_model<-Arima(trainData.zoo,order=c(2,1,2),include.drift=TRUE)
# Refit the old model with testData
new_model<-Arima(testData.zoo,model=old_model)
The ?Arima page says that y (the first argument) should be a ts object. My guess is that the first call to Arima coerces your zoo object to ts, but the second call does not.
An easy way to work-around this is to explicitly coerce to ts:
# Refit the old model with testData
new_model <- Arima(as.ts(testData.zoo), model = old_model)
When I do forecast using forecast library, I noticed following code does not run as expected:
library(forecast)
library(dplyr)
df1 <- data.frame(gp=gl(20,5), dt=seq(1:100))
get <- function (df1){
ts1 <- ts((df1%>%filter(gp==2))$dt)
as.numeric(forecast(ar(ts1),15)$mean)
}
print(get(df1))
The error return is:
Error in ts(x) : 'ts' object must have one or more observations
May be it is caused by ar or ar.burg function. Because if you change the function to ets or something else the function works well.
What is more strange is that if you change the code to:
library(forecast)
library(dplyr)
df1 <- data.frame(gp=gl(20,5), dt=seq(1:100))
ts1 <- ts((df1%>%filter(gp==2))$dt)
get <- function (ts1){
as.numeric(forecast(ar(ts1),15)$mean)
}
print(get(ts1))
The code is also running correctly. I think this may be a bug in ar function, and the problem is somehow related to scope. Any thoughts about this?
The problem is to do with scoping. forecast() tries to find the time series used to fit the model. The functions from the forecast package (such as ets) store this information in the model object, so it is easy for forecast() to find it. But ar() is from the stats package, and it does not store the time series used to fit the model. So forecast() goes looking for it. If you run your code outside of the get() function, it works ok because forecast() manages to find the ts1 object in the local environment. But within the get() function it causes an error.
One simple fix is to add the information to the fitted model before calling forecast:
library(forecast)
library(dplyr)
df1 <- data.frame(gp=gl(20,5), dt=seq(1:100))
ts1 <- ts((df1%>%filter(gp==2))$dt)
get <- function (ts1){
fit <- ar(ts1)
fit$x <- ts1
as.numeric(forecast(fit,15)$mean)
}
print(get(ts1))
Alternatively, use predict instead of forecast:
library(dplyr)
df1 <- data.frame(gp=gl(20,5), dt=seq(1:100))
ts1 <- ts((df1%>%filter(gp==2))$dt)
get <- function (ts1){
fit <- ar(ts1)
as.numeric(predict(fit,n.ahead=15)$pred)
}
print(get(ts1))
Q: What is the right way to set the frequency in an xts object given a set of dates? Ideally, auto.arima() called on this xts object would yield the same results as when called on an analogous ts object.
Detail: I was surprised to find different results from an auto.arima() fit based on whether I passed a ts or xts object. I found the difference had to do with the frequency (which, in the case of xts, was being reset to 1 despite my setting it to 12 in the construction). Below, setting up sim_ts_12 and estimating the intended model was relatively straightforward. But in my initial attempts at working with xts (sim_xts and sim_xts_not) I estimated the wrong model. I finally estimated the right model using xts (sim_xts_12, sim_ts2xts), but both of those approaches seem wrong in some way. I'd expect working with xts to be simpler than ts. But that doesn't seem to be the case here. Am I missing something?
sim <- scan(file="./sim.dat")
sim_ts_12 <- ts(sim, start=c(2016,1), frequency=12)
sim_ts2xts_12 <- as.xts(sim_ts_12)
sim_xts <- xts(x=sim, order.by=seq.Date(from=as.Date("2016-01-01"), by="month", length.out = length(sim)))
sim_xts_12_not <- xts(x=sim, order.by=seq.Date(from=as.Date("2016-01-01"), by="month", length.out = length(sim)), frequency=12)
sim_xts_12 <- sim_xts
attr(sim_xts_12, 'frequency') <- 12
auto.arima(sim_ts_12) # ARIMA(0,1,1)(0,1,0)[12]
auto.arima(sim_ts2xts_12) # ARIMA(0,1,1)(0,1,0)[12]
auto.arima(sim_xts) # ARIMA(0,1,1) with drift
auto.arima(sim_xts_12_not) # ARIMA(0,1,1) with drift
auto.arima(sim_xts_12) # ARIMA(0,1,1)(0,1,0)[12]
txt <- "0.04767597 0.07217235 0.03954613 0.03698637 0.04283896
0.03534811 0.04198519 0.04129214 0.04576022 0.03966146
0.03656881 0.04396736 0.04459328 0.07062732 0.03477407
0.0340033 0.039136 0.0347761 0.03819997 0.03634627
0.03966617 0.03455635 0.03009606 0.03927688 0.03959629
0.06554147 0.02908742 0.02619443 0.03179742 0.02468108
0.02612955 0.02300656 0.02988827 0.01878513 0.01399028
0.02601922 0.0250159 0.05610426 0.01537538 0.01231939
0.01330564 0.008744173 0.01296571 0.005741129 0.01674992
0.003210812 -0.007936987 0.01018758"
sim.dat <- scan(text=txt, what=numeric() )
UPDATE, NOT A DUPLICATE: The possible duplicate question/answer does not address the best practice method for handling frequency in an xts. The question does not ask for it, nor does the answer address it. The answer handles ts.
I want to forecast ARFIMA with Kalman filter and not able to fit the arfima model into the Kalmanforecast.
library(base)
library(stats)
library(parallel)
library(forecast)
sink(file='/home/nero/KF_arfima.log')
f=COST$COST
x=logb(p,10)
# Start the clock!
ptm <- proc.time()
p=arfima(x[1:50], drange=c(0, 0.5),estim=c("mle"))
pr <- KalmanForecast(2, p$model)
y=x[51:52]
yhat=pr$pred #predicted value
map=mean(abs((y - yhat)/y)) #MAPE
proc.time() - ptm
print(map)
I am getting the error
Error in KalmanForecast(2, p$model) : invalid argument type"
I also check and found that there is no object called model. I lost three days to solve it. I tried with various R packages, but none of them has solved it. Please let me know how to fix it.
Data Sample:
Timestamp,COST
2015-09-21T00:00:00+00:00,6
2015-09-21T00:06:00+00:00,7
2015-09-21T00:12:00+00:00,7
2015-09-21T00:18:00+00:00,7
2015-09-21T00:24:00+00:00,7
2015-09-21T00:30:00+00:00,7
2015-09-21T00:36:00+00:00,7
2015-09-21T00:42:00+00:00,6
2015-09-21T00:48:00+00:00,7
2015-09-21T00:54:00+00:00,6
2015-09-21T01:00:00+00:00,6
2015-09-21T01:06:00+00:00,7
2015-09-21T01:12:00+00:00,7
2015-09-21T01:18:00+00:00,7
2015-09-21T01:24:00+00:00,7
2015-09-21T01:30:00+00:00,7
2015-09-21T01:36:00+00:00,7
2015-09-21T01:42:00+00:00,6
2015-09-21T01:48:00+00:00,7
2015-09-21T01:54:00+00:00,6
2015-09-21T02:00:00+00:00,6
2015-09-21T02:06:00+00:00,8
2015-09-21T02:12:00+00:00,8
2015-09-21T02:18:00+00:00,7
2015-09-21T02:24:00+00:00,8
2015-09-21T02:30:00+00:00,7
2015-09-21T02:36:00+00:00,7
2015-09-21T02:42:00+00:00,7
2015-09-21T02:48:00+00:00,8
2015-09-21T02:54:00+00:00,7
2015-09-21T03:00:00+00:00,6
2015-09-21T03:06:00+00:00,7
2015-09-21T03:12:00+00:00,7
2015-09-21T03:18:00+00:00,7
2015-09-21T03:24:00+00:00,7
2015-09-21T03:30:00+00:00,7
2015-09-21T03:36:00+00:00,7
2015-09-21T03:42:00+00:00,7
2015-09-21T03:48:00+00:00,6
2015-09-21T03:54:00+00:00,6
2015-09-21T04:00:00+00:00,6
2015-09-21T04:06:00+00:00,7
2015-09-21T04:12:00+00:00,7
2015-09-21T04:18:00+00:00,7
2015-09-21T04:24:00+00:00,7
2015-09-21T04:30:00+00:00,6
2015-09-21T04:36:00+00:00,6
2015-09-21T04:42:00+00:00,6
2015-09-21T04:48:00+00:00,6
2015-09-21T04:54:00+00:00,7
2015-09-21T05:00:00+00:00,6
2015-09-21T05:06:00+00:00,7
2015-09-21T05:12:00+00:00,7
The help file for KalmanForecast clearly describes what sort of model is required. The arfima function does not produce output of the required kind.
Rather than use KalmanForecast, you can use the forecast function from the forecast package to produce the forecasts. It also uses a Kalman filter to compute the forecasts.
If you really want to use the KalmanForecast to do the work, you will have to figure out how to create the mod argument yourself.
I have following R code which does not work when trying to pre-whiten other series by the model generated for the other series.
-- Libraries;
library(forecast);
library(TSA);
library(xts);
-- Read from csv;
....
-- Do transforms;
Power=xts(data1[2],seq(from=as.Date("2011-01-01"), to=as.Date("2013-09-18"),by="day"),frequency=7);
Temp=xts(data2[1],seq(from=as.Date("2011-01-01"), to=as.Date("2013-09-18"),by="day"),frequency=7);
-- Prewhiten for CCF;
mod1=Arima(Temp,order=c(2,0,1),seasonal=list(order=c(1,1,1)));
Box.test(mod1$residuals,lag=365,type=c("Ljung-Box"));
x_series=mod1$residuals;
y_filtered=residuals(Arima(Power,model=mod1));
Last Part does not work since I get error:
Error in stats::arima(x = x, order = order, seasonal = seasonal, include.mean = include.mean, :
wrong length for 'fixed'
What goes wrong here?
Arima and stats::arima both require ts objects. The error is caused by xts objects being used. Try this instead:
Power <- ts(data1[2], frequency=7)
Temp <- ts(data2[1], frequency=7)
mod1 <- Arima(Temp,order=c(2,0,1),seasonal=c(1,1,1))
Box.test(residuals(mod1),lag=365,type=c("Ljung-Box"))
x_series <- residuals(mod1)
y_filtered <- residuals(Arima(Power,model=mod1))