Get Started

Simulate the random walk model with a drift

A random walk (RW) need not wander about zero, it can have an upward or downward trajectory, i.e., a drift or time trend. This is done by including an intercept in the RW model, which corresponds to the slope of the RW time trend.

For an alternative formulation, you can take the cumulative sum of a constant mean white noise (WN) series, such that the mean corresponds to the slope of the RW time trend.

To simulate data from the RW model with a drift you again use the arima.sim() function with the model = list(order = c(0, 1, 0)) argument. This time, you should add the additional argument mean = ... to specify the drift variable, or the intercept.

This is a part of the course

“Time Series Analysis in R”

View Course

Exercise instructions

  • Use arima.sim() to generate another RW model. Set the model argument equal to list(order = c(0, 1, 0)) to generate a RW-type model and set n equal to 100 to produce 100 observations. Set the mean argument to 1 to produce a drift. Save this to rw_drift.
  • Use ts.plot() to plot your rw_drift data.
  • Use diff() to calculate the first difference of your rw_drift data. Save this as rw_drift_diff.
  • Use another call to ts.plot() to plot rw_drift_diff.

Hands-on interactive exercise

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

# Generate a RW model with a drift uing arima.sim
rw_drift <- arima.sim(model = ___, n = ___, mean = ___)

# Plot rw_drift


# Calculate the first difference series
rw_drift_diff <- 

# Plot rw_drift_diff

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 drift
Exercise 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