Removing trend and season with differencing
Today let's understand differencing: how to remove a trend and a season from a time series, so that what is left behaves in a way a model can actually work with.
The running example is AirPassengers, a dataset built into R: the monthly count of international airline passengers, in thousands, from January 1949 through December 1960, 144 months in total. It was first published by Box and Jenkins, and time series teaching has used it ever since.
Here is the whole series, plotted month by month.
The line climbs across the whole twelve years, and the summer bump riding on top of it grows a little wider every year. Removing both of those, the climb and the growing bump, is exactly what differencing does.
Removing a trend with an ordinary difference
Differencing means subtracting each value in a series from the value some fixed number of steps before it. That fixed gap is called the lag. The simplest case, lag = 1, subtracts each month's value from the month right before it. That is called an ordinary difference.
Build the AirPassengers tsibble, then compute an ordinary difference and compare its mean and standard deviation against the raw series.
The raw series averages 280.30, with a standard deviation of 119.97, both in thousands of passengers a month. d1 holds 143 values instead of 144, since the very first month has no earlier month to subtract. Its mean is 2.24, close to zero instead of climbing steadily upward the way the raw series does. Its standard deviation, 33.75, is a different kind of number: it measures how much the series moves from one month to the next, not how spread out the raw levels are, so it is not meant to be compared directly against 119.97. What matters here is the mean collapsing toward zero.
Plot the differenced series to see the trend disappear.
The line now wanders flatly around zero instead of climbing across the twelve years. That flat wandering, not an exact zero mean, is what a model needs: no more systematic drift upward or downward.