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.
Diese Übung ist Teil des Kurses
Forecasting in R
Anleitung zur Übung
- Using the
goog
data 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
e
by 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 = mse
is provided in the list of function arguments, thenmse
should refer to an object that exists in your workspace outside the function, whereasMSE
is the variable that you refers to this object within your function.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# 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()