Get Started

Removing trends in level by differencing

The first difference transformation of a time series \(z[t]\) consists of the differences (changes) between successive observations over time, that is \(z[t] - z[t-1]\).

Differencing a time series can remove a time trend. The function diff() will calculate the first difference or change series. A difference series lets you examine the increments or changes in a given time series. It always has one fewer observations than the original series.

The time series z has already been loaded, and is shown in the figure on the right.

This is a part of the course

“Time Series Analysis in R”

View Course

Exercise instructions

  • Apply the diff() function to z, saving the result as dz.
  • Use ts.plot() to view a time series plot of the transformed series dz.
  • Use two calls of length() to calculate the number of observations in z and dz, respectively.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Generate the first difference of z
dz <- 
  
# Plot dz


# View the length of z and dz, respectively


Edit and Run Code