Seasonal differencing for stationarity
With seasonal data, differences are often taken between observations in the same season of consecutive years, rather than in consecutive periods. For example, with quarterly data, one would take the difference between Q1 in one year and Q1 in the previous year. This is called seasonal differencing.
Sometimes you need to apply both seasonal differences and lag-1 differences to the same series, thus, calculating the differences in the differences.
In this exercise, you will use differencing and transformations simultaneously to make a time series look stationary. The data set here is h02
, which contains 17 years of monthly corticosteroid drug sales in Australia. It has been loaded into your workspace.
This is a part of the course
“Forecasting in R”
Exercise instructions
- Plot the data to observe the trend and seasonality.
- Take the
log()
of theh02
data and then apply seasonal differencing by using an appropriatelag
value indiff()
. Assign this todifflogh02
. - Plot the resulting logged and differenced data.
- Because
difflogh02
still looks non-stationary, take another lag-1 difference by applyingdiff()
to itself and save this toddifflogh02
. Plot the resulting series. - Plot the ACF of the final
ddifflogh02
series using the appropriate function.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Plot the data
___
# Take logs and seasonal differences of h02
difflogh02 <- diff(log(___), lag = ___)
# Plot difflogh02
___
# Take another difference and plot
ddifflogh02 <- ___
___
# Plot ACF of ddifflogh02
___
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.
ARIMA models provide another approach to time series forecasting. Exponential smoothing and ARIMA models are the two most widely-used approaches to time series forecasting, and provide complementary approaches to the problem. While exponential smoothing models are based on a description of the trend and seasonality in the data, ARIMA models aim to describe the autocorrelations in the data.
Exercise 1: Transformations for variance stabilizationExercise 2: Box-Cox transformations for time seriesExercise 3: Non-seasonal differencing for stationarityExercise 4: Seasonal differencing for stationarityExercise 5: ARIMA modelsExercise 6: Automatic ARIMA models for non-seasonal time seriesExercise 7: Forecasting with ARIMA modelsExercise 8: Comparing auto.arima() and ets() on non-seasonal dataExercise 9: Seasonal ARIMA modelsExercise 10: Automatic ARIMA models for seasonal time seriesExercise 11: Exploring auto.arima() optionsExercise 12: Comparing auto.arima() and ets() on seasonal dataWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.