Rolling forecast mit xreg and re-estimation

Methoden der Zeitreihenanalyse

Moderator: schubbiaschwilli

Antworten
lherming
Beiträge: 1
Registriert: Fr Okt 30, 2020 12:33 am

Rolling forecast mit xreg and re-estimation

Beitrag von lherming »

Ich versuche, xreg in meine rollierende Prognose mit Neueinschätzung zu implementieren. Leider habe ich Probleme mit Anfang und Ende für die letzte Periode (52).

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)
Ich erhalte immer wieder diese Fehlermeldung:

Code: Alles auswählen

Error in window.default(x, ...) : 'start' must be after 'end'
Ich habe verschiedene Ansätze ausprobiert, aber ich habe es nicht richtig hinbekommen.

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)
wegen notwendiger Transformation erhalte ich zusätzlich diese Fehlermeldung für die 4:

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
Wenn ich es für i=3 und i=5 selbst berechne, funktioniert es perfekt. Meine Vermutung ist, dass die Fehlermeldung mit der Forecast Funktion kommt.
Antworten