Removing seasonal trends with seasonal differencing
For time series exhibiting seasonal trends, seasonal differencing can be applied to remove these periodic patterns. For example, monthly data may exhibit a strong twelve month pattern. In such situations, changes in behavior from year to year may be of more interest than changes from month to month, which may largely follow the overall seasonal pattern.
The function diff(..., lag = s)
will calculate the lag s
difference or length s
seasonal change series. For monthly or quarterly data, an appropriate value of s
would be 12 or 4, respectively. The diff()
function has lag = 1
as its default for first differencing. Similar to before, a seasonally differenced series will have s
fewer observations than the original series.
This exercise is part of the course
Time Series Analysis in R
Exercise instructions
- The time series
x
has already been loaded, and is shown in the adjoining figure ranging below -10 to above +10. Apply thediff(..., lag = 4)
function tox
, saving the result asdx
. - Use
ts.plot()
to show the transformed seriesdx
and note the condensed vertical range of the transformed data. - Use two calls of
length()
to calculate the number of observations inx
anddx
, respectively.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Generate a diff of x with lag = 4. Save this to dx
dx <-
# Plot dx
# View the length of x and dx, respectively