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”
Exercise instructions
- Use
arima.sim()
to simulate 100 observations of an AR model with slope equal to 0.5. To do so, set themodel
argument equal tolist(ar = 0.5)
and set then
argument equal to100
. Save this simulated data tox
. - Use a similar call to
arima.sim()
to simulate 100 observations of an AR model with slope equal to 0.9. Save this data toy
. - Use a third call to
arima.sim()
to simulate 100 observations of an AR model with slope equal to -0.75 Save this data toz
. - Use
plot.ts()
withcbind()
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(___, ___, ___))
This exercise is part of the course
Time Series Analysis in R
Learn the core techniques necessary to extract meaningful insights from time series data.
In this chapter, you will learn the autoregressive (AR) model and several of its basic properties. You will also practice simulating and estimating the AR model in R, and compare the AR model with the random walk (RW) model.
Exercise 1: The autoregressive modelExercise 2: Simulate the autoregressive modelExercise 3: Estimate the autocorrelation function (ACF) for an autoregressionExercise 4: Persistence and anti-persistenceExercise 5: Compare the random walk (RW) and autoregressive (AR) modelsExercise 6: AR model estimation and forecastingExercise 7: Estimate the autoregressive (AR) modelExercise 8: Simple forecasts from an estimated AR modelWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.