# Time series cross-validation 3

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’ve updated my time-series cross validation algorithm to fix some bugs and allow for a possible xreg term. This allows for cross-validation of multivariate models, so long as they are specified as a function with the following paramters: x (the series to model), xreg (independent variables, optional), newxreg (xregs for the forecast), and h (the number of periods to forecast). Note that h should equal the number of rows in the xreg matrix. Also note that you need to forecast the xreg object BEFORE forecasting your x object. For example, if you wish to forecast 12 months into the future, your xreg object should have 12 extra rows.**Modern Toolmaking**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Here is the source code for the new function:

And here is an example, using a linear model with xregs:

I am particularly excited about this code because it will allow me to apply arbitrary machine learning algorithms to forecasting problems. For example, I could create an xreg matrix of lags and use a support vector machine, neural network, or random forest to make 1-step forecasts. I am planning to release this code as a package on CRAN, once I finish the documentation. I’m also planning to re-work the function a bit to return an S3 class, containing:

- Predicted values at each forecast horizon, including beyond the length of the input time series.
- Actual values at each forecast horizon, for easy comparison to #1.
- Matrix of average error at each horizon.
- The final model.
- Forecasts using the final model, from the last observation of x to the “max horizon”.
- A print method that will show #3.
- A plot method that will plot #3.

Let me know if you have any suggestions or spot any bugs!

To

**leave a comment**for the author, please follow the link and comment on their blog:**Modern Toolmaking**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.