Get Started

Simulate the random walk model

The random walk (RW) model is also a basic time series model. It is the cumulative sum (or integration) of a mean zero white noise (WN) series, such that the first difference series of a RW is a WN series. Note for reference that the RW model is an ARIMA(0, 1, 0) model, in which the middle entry of 1 indicates that the model's order of integration is 1.

The arima.sim() function can be used to simulate data from the RW by including the model = list(order = c(0, 1, 0)) argument. We also need to specify a series length n. Finally, you can specify a sd for the series (increments), where the default value is 1.

This is a part of the course

“Time Series Analysis in R”

View Course

Exercise instructions

  • Use arima.sim() to generate a 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. Save this to random_walk.
  • Use ts.plot() to plot your random_walk data.
  • Use diff() to calculate the first difference of your random_walk data. Save this as random_walk_diff.
  • Use another call to ts.plot() to plot random_walk_diff.

Hands-on interactive exercise

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

# Generate a RW model using arima.sim
random_walk <- arima.sim(model = ___, n = ___)

# Plot random_walk


# Calculate the first difference series
random_walk_diff <- 

# Plot random_walk_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 model
Exercise 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