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
___