Get Started

Estimate the random walk model

For a given time series y we can fit the random walk model with a drift by first differencing the data, then fitting the white noise (WN) model to the differenced data using the arima() command with the order = c(0, 0, 0)) argument.

The arima() command displays information or output about the fitted model. Under the Coefficients: heading is the estimated drift variable, named the intercept. Its approximate standard error (or s.e.) is provided directly below it. The variance of the WN part of the model is also estimated under the label sigma^2.

This is a part of the course

“Time Series Analysis in R”

View Course

Exercise instructions

  • The time series random_walk has already been loaded, and is shown in the adjoining figure. Use diff() to generate the first difference of the data. Save this to rw_diff.
  • Use ts.plot() to plot your differenced data
  • Use arima() to fit the WN model for the differenced data. To do so, set the x argument to rw_diff and set the order argument to c(0, 0, 0). Store the model in model_wn.
  • Store the intercept value of model_wn in int_wn. You can obtain this value using model_wn$coef.
  • Use ts.plot() to reproduce your original plot of random_walk.
  • Add the estimated time trend to the adjoining plot with the function abline(). You can use int_wn as the second argument.

Hands-on interactive exercise

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

# Difference your random_walk data
rw_diff <- 

# Plot rw_diff


# Now fit the WN model to the differenced data
model_wn <-

# Store the value of the estimated time trend (intercept)
int_wn <- 

# Plot the original random_walk data


# Use abline(0, ...) to add time trend to the figure

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 differencingExercise 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 model
Exercise 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