Get Started

Simulate the simple moving average model

The simple moving average (MA) model is a parsimonious time series model used to account for very short-run autocorrelation. It does have a regression like form, but here each observation is regressed on the previous innovation, which is not actually observed. Like the autoregressive (AR) model, the MA model includes the white noise (WN) model as special case.

As with previous models, the MA model can be simulated using the arima.sim() command by setting the model argument to list(ma = theta), where theta is a slope parameter from the interval (-1, 1). Once again, you also need to specify the series length using the n argument.

In this exercise, you'll simulate and plot three MA models with slope parameters 0.5, 0.9, and -0.5, respectively.

This is a part of the course

“Time Series Analysis in R”

View Course

Exercise instructions

  • Use arima.sim() to simulate a MA model with the slope parameter set to 0.5, and series length 100. Save this model to x.
  • Use another call to arima.sim() to simulate a MA model with the slope parameter set to 0.9. Save this model to y.
  • Use a third call to arima.sim() to simulate a final MA model with the slope parameter set to -0.5. Save this model to z.
  • Use plot.ts() to display all three models.

Hands-on interactive exercise

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

# Generate MA model with slope 0.5
x <- arima.sim(model = ___, n = ___)

# Generate MA model with slope 0.9
y <- 

# Generate MA model with slope -0.5
z <- 

# Plot all three models together
plot.ts(cbind(___, ___, ___))
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 learn the simple moving average (MA) model and several of its basic properties. You will also practice simulating and estimating the MA model in R, and compare the MA model with the autoregressive (AR) model.

Exercise 1: The simple moving average modelExercise 2: Simulate the simple moving average model
Exercise 3: Estimate the autocorrelation function (ACF) for a moving averageExercise 4: MA model estimation and forecastingExercise 5: Estimate the simple moving average modelExercise 6: Simple forecasts from an estimated MA modelExercise 7: Compare AR and MA modelsExercise 8: AR vs MA modelsExercise 9: Name that model by time series plotExercise 10: Name that model by ACF plotExercise 11: Congratulations!

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