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.
This is a part of the course
“Forecasting in R”
Exercise instructions
- 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.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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()
This exercise is part of the course
Forecasting in R
Learn how to make predictions about the future using time series forecasting in R including ARIMA models and exponential smoothing methods.
In this chapter, you will learn general tools that are useful for many different forecasting situations. It will describe some methods for benchmark forecasting, methods for checking whether a forecasting method has adequately utilized the available information, and methods for measuring forecast accuracy. Each of the tools discussed in this chapter will be used repeatedly in subsequent chapters as you develop and explore a range of forecasting methods.
Exercise 1: Forecasts and potential futuresExercise 2: Naive forecasting methodsExercise 3: Fitted values and residualsExercise 4: Checking time series residualsExercise 5: Training and test setsExercise 6: Evaluating forecast accuracy of non-seasonal methodsExercise 7: Evaluating forecast accuracy of seasonal methodsExercise 8: Do I have a good forecasting model?Exercise 9: Time series cross-validationExercise 10: Using tsCV() for time series cross-validationWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.