Code: Alles auswählen
# sample data
sample <- ts(rnorm(100, mean = 1000, sd=7), start = c(2015,1), end = c(2019,52), frequency = 52)
external <- ts(mtcars, start = c(2015,1), end = c(2019,52), frequency = 52)
#Define h --> One-step ahead (for a start, later to be increased)
h <- 1
#specify length to forecast
test <- window(sample, start = c(2019,01), end = c(2019,52), frequency = 52)
n <- length(test) - h + 1
#provide total length of regressors available
total_xreg <- ts(external[,c(1,2,3)], start = c(2015,1), end= c(2019,12), frequency = 12)
#create empty matrix
fcmatx <- matrix(0, nrow=n, ncol=h)
# create loop
for(i in 1:n)
{
# x is the target variable, provide training data
x <- window(sample, end= c(2018,52) + (i-1)/52)
# provide xregs for training data
xregs <- window(total_xreg, end = c(2018,52) + (i-1)/52)
# provide new xregs for forecasting, assuming that xreg is available for the forecasting period
xregs2 <- window(total_xreg, start = c(2019,1) + (i-1)/52)
# limit xregs2 to show only the first line since we are only forecasting 1 step in advance
xregs3 <- xregs2[1,,drop=FALSE]
# create auto.arima model
refit.multirex <- auto.arima(x, xreg = xregs)
# forecast using regressors
fcmatx[i,] <- forecast(refit.multirex,
h=h,
xreg = xregs3
)$mean
}
fcmattsx <- ts(fcmatx, start = c(2019,1), frequency = 52)
Code: Alles auswählen
Error in window.default(x, ...) : 'start' must be after 'end'
Nach dieser Schleife stehe ich vor einem zweiten Problem:
Wenn ich meine auto.arima-Funktion auf
Code: Alles auswählen
refit.multirex <- auto.arima(x, xreg = xregs,
lambda = "auto",
biasadj=TRUE)
Code: Alles auswählen
Error in NextMethod(.Generic) :
cannot assign 'tsp' to a vector of length 0
Additional: Warning message:
In .cbind.ts(list(e1, e2)), c(deparse(substitute(e1))[1L], deparse(substitute(e2))[1L]), :
non-intersecting series