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

This exercise is part of the course

Time Series Analysis in R

IntermediateSkill Level
4.5+
8 reviews

Learn the core techniques necessary to extract meaningful insights from time series data.

In this chapter, you will conduct some trend spotting, and learn the white noise (WN) model, the random walk (RW) model, and the definition of stationary processes.

Exercise 1: Trend spotting!Exercise 2: Random or not random?Exercise 3: Name that trendExercise 4: Removing trends in variability via the logarithmic transformationExercise 5: Removing trends in level by differencing
Exercise 6: Removing seasonal trends with seasonal differencingExercise 7: The white noise (WN) modelExercise 8: Simulate the white noise modelExercise 9: Estimate the white noise modelExercise 10: The random walk (RW) modelExercise 11: Simulate the random walk modelExercise 12: Simulate the random walk model with a driftExercise 13: Estimate the random walk modelExercise 14: Stationary processesExercise 15: Stationary or not?Exercise 16: Are the white noise model or the random walk model stationary?

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free