Get Started

Simulate the autoregressive model

The autoregressive (AR) model is arguably the most widely used time series model. It shares the very familiar interpretation of a simple linear regression, but here each observation is regressed on the previous observation. The AR model also includes the white noise (WN) and random walk (RW) models examined in earlier chapters as special cases.

The versatile arima.sim() function used in previous chapters can also be used to simulate data from an AR model by setting the model argument equal to list(ar = phi) , in which phi is a slope parameter from the interval (-1, 1). We also need to specify a series length n.

In this exercise, you will use this command to simulate and plot three different AR models with slope parameters equal to 0.5, 0.9, and -0.75, respectively.

This is a part of the course

“Time Series Analysis in R”

View Course

Exercise instructions

  • Use arima.sim() to simulate 100 observations of an AR model with slope equal to 0.5. To do so, set the model argument equal to list(ar = 0.5) and set the n argument equal to 100. Save this simulated data to x.
  • Use a similar call to arima.sim() to simulate 100 observations of an AR model with slope equal to 0.9. Save this data to y.
  • Use a third call to arima.sim() to simulate 100 observations of an AR model with slope equal to -0.75 Save this data to z.
  • Use plot.ts() with cbind() to plot your three ts objects (x, y, z).

Hands-on interactive exercise

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

# Simulate an AR model with 0.5 slope
x <- arima.sim(model = ___, n = ___)

# Simulate an AR model with 0.9 slope
y <- 

# Simulate an AR model with -0.75 slope
z <- 

# Plot your simulated data
plot.ts(cbind(___, ___, ___))
Edit and Run Code