Using tsCV() for time series cross-validation
The tsCV() function computes time series cross-validation errors. It requires you to specify the time series, the forecast method, and the forecast horizon. Here is the example used in the video:
> e = tsCV(oil, forecastfunction = naive, h = 1)
Here, you will use tsCV() to compute and plot the MSE values for up to 8 steps ahead, along with the naive() method applied to the goog data. The exercise uses ggplot2 graphics which you may not be familiar with, but we have provided enough of the code so you can work out the rest.
Be sure to reference the slides on tsCV() in the lecture. The goog data has been loaded into your workspace.
Este ejercicio forma parte del curso
Forecasting in R
Instrucciones del ejercicio
- Using the
googdata and forecasting with thenaive()function, compute the cross-validated errors for up to 8 steps ahead. Assign this toe. - Compute the MSE values for each forecast horizon and remove missing values in
eby specifying the second argument. The expression for calculating MSE has been provided. - Plot the resulting MSE values (
y) against the forecast horizon (x). Think through your knowledge of functions. IfMSE = mseis provided in the list of function arguments, thenmseshould refer to an object that exists in your workspace outside the function, whereasMSEis the variable that you refers to this object within your function.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# Compute cross-validated errors for up to 8 steps ahead
e <- tsCV(___, forecastfunction = ___, h = ___)
# Compute the MSE values and remove missing values
mse <- colMeans(e^2, na.rm = ___)
# Plot the MSE values against the forecast horizon
data.frame(h = 1:8, MSE = mse) %>%
ggplot(aes(x = h, y = ___)) + geom_point()