1
$\begingroup$

I am using forecast::tsCV function in R to perform a rolling origin forecast of 1 day to 1 year ahead horizons using daily frequency data. Suppose my outcome variable is y, and I have x1, x2 and x3 as exogenous variables.

If I want to use the actual values of x1, x2 and x3 for training and testing sample to perform a seven day ahead out of sample forecast, then it would be as:

xreg <- data.frame(x1, x2, x3)

fit <- function(x, h, xreg, newxreg) {
  forecast(Arima(x, order=c(1,0,1), xreg=xreg), xreg=newxreg)
}

e <- forecast::tsCV(y, fit, h=7, window=1000, xreg=xreg)

However, the problem is that I want to use the actual values of x1 and x2 for the training sample and their forecasted values for the rest of the period.

So I have forecasted values of x1 and x2, lets call them f_x1 and f_x2. If the first forecast point 1007, x1, x2 and x3 have to be used for 1 to 1000, then switch to f_x1 and f_x2 from 1001 to 1007, and this have to roll over as the windows roll.

I have tried this one:

xreg1=contains x1, x2 and x3
xreg2= contains f_x1, f_x2, x3
then:
e <tsCV(price, fit, h=7,
window =1000,
if (length(xreg) < 1001) {
xreg=xreg1
} else if (length(xreg) > 1000) {
xreg=xreg2
})

But the output shows that it doesn't convert using data from xtreg 1 to xtreg2.

Can someone help me how can I develop the forecast::tsCV function to perform this?


Thanks for the answer, Its from PackageName::forecast

$\endgroup$
3
  • $\begingroup$ Which R package is tsCV? Please edit, for example by giving the function in the form PackageName::tsCV. $\endgroup$ Commented Oct 8, 2021 at 17:22
  • $\begingroup$ Thanks for the answer, Its from PackageName::forecast $\endgroup$
    – Shwan
    Commented Oct 8, 2021 at 18:17
  • $\begingroup$ Please do not give new information only in comments, edit your question to add the new information. We want posts to be self-contained, comments can be deleted, and anyhow, information in comments are not well organized. Also, many people do not read comments. $\endgroup$ Commented Oct 8, 2021 at 18:36

1 Answer 1

0
$\begingroup$

You would need to rewrite the tsCV function to implement this. The way it is written assumes that there is only one xreg row for each time period, but you are wanting to have a different xreg depending on whether the row is being used for training data or test data.

The function itself is not very long, and written only in R, so should not be difficult to adapt. Your new function will need to have two xreg arguments, one with actual values and one with forecasted values. Then you just need to switch to using the forecasted version when creating xreg_future in the function. The xreg_subset variable should use the actual values.

$\endgroup$
3
  • $\begingroup$ Thank you very much for your answer, if I understan correctly, newxreg in forecast function is to be applied as test data and there is no need to use loop, am I correct? but I could not find any example on how to implement it. $\endgroup$
    – Shwan
    Commented Oct 9, 2021 at 0:24
  • $\begingroup$ You will still need the loop. Inside the loop you will see there are two variables used: xreg_subset and xreg_future. The xreg_future variable needs to be modified as I have explained. $\endgroup$ Commented Oct 9, 2021 at 5:13
  • $\begingroup$ If you found this answer helpful, then please consider upvoting and/or accepting it. $\endgroup$ Commented Oct 9, 2021 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.