Session Ready
Exercise

Calculate a difference of a series using diff()

Another common operation on time series, typically on those that are non-stationary, is to take a difference of the series. The number of differences to take of a series is an application of recursively calling the difference function n times.

A simple way to view a single (or "first order") difference is to see it as x(t) - x(t-k) where k is the number of lags to go back. Higher order differences are simply the reapplication of a difference to each prior result.

In R, the difference operator for xts is made available using the diff() command. This function takes two arguments of note. The first is the lag, which is the number of periods, and the second is differences, which is the order of the difference (e.g. how many times diff() is called).

# These are the same
diff(x, differences = 2)
diff(diff(x))

In this exercise, you will reuse the AirPass data from earlier in this chapter, though this time you will use the full series from 1948 to 1960.

Instructions
100 XP
  • Construct a first order difference of AirPass by hand, using lag() and subtraction. Save this as diff_by_hand.
  • To verify that your result is identical to using diff(AirPass), combine and inspect the first few rows of both in your console. Use merge() and head() for this.
  • Get the first order 12 month difference of series AirPass. Be sure to specify both the lag and differences arguments in diff().