You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I may be a bit confused here, but I'm having trouble setting the window option in tsCV() while also using xreg. When not using the xreg, the window seems to scan the data correctly:
When using the same idea as above, but specifying an xreg, it does not work. Any tips on how to write my forecastfunction such that I can use a rolling window and xreg? It works for the first fit (i.e., before the window starts rolling), but not for other fits.
fc <- function(y, h, xreg)
{
X <- xreg[1:length(y),]
if(NROW(xreg) < length(y) + h)
stop("Not enough xreg data for forecasting")
newX <- xreg[length(y)+(1:h), ]
fit <- Arima(y, xreg=X, order = c(1, 0, 0))
forecast(fit, xreg=newX)
}
set.seed(1)
y <- ts(rnorm(100))
x <- matrix(ts(rnorm(100)),ncol=1)
e <- tsCV(y, fc, xreg=x, window = 20)
fit1 <- Arima(y[1:20], xreg = x[1:20, ], order = c(1, 0, 0))
fcast1 <- forecast(fit1, h = 1, xreg = x[21, ])
y[21] - fcast1$mean[1]; e[20]
# [1] 0.7328816
# [1] 0.7328816
fit2 <- Arima(y[2:21], xreg = x[2:21, ], order = c(1, 0, 0))
fcast2 <- forecast(fit2, h = 1, xreg = x[22, ])
y[22] - fcast2$mean[1]; e[21]
# [1] 0.7791716
# [1] 0.5096576
Thanks for any help!
The text was updated successfully, but these errors were encountered:
Hi, I may be a bit confused here, but I'm having trouble setting the
window
option intsCV()
while also usingxreg
. When not using thexreg
, the window seems to scan the data correctly:When using the same idea as above, but specifying an
xreg
, it does not work. Any tips on how to write myforecastfunction
such that I can use a rolling window andxreg
? It works for the first fit (i.e., before the window starts rolling), but not for other fits.Thanks for any help!
The text was updated successfully, but these errors were encountered: