Simulate an AR (1) model

Viewed 1341

I want to simulate the model enter image description here

I believe that arima.sim function can do this efficiently. How can I simulate this in R using arima.sim function (or another efficient function)?

My attempt

Assuming I want to generate 1000 observations for the model with rho=0.45 and sigma_u^2=0.2,

arima.sim(n=1000,list(ar=0.45),rand.gen=rnorm, sd=sqrt(0.2))

The problem is that I'm not sure if the command initializes exactly as the model above.

1 Answers

Use the rGARMA function in the ts.extend package

You can generate random vectors from any stationary Gaussian ARMA model using the ts.extend package. This package generates random vectors directly form the multivariate normal distribution using the computed autocorrelation matrix for the random vector, so it gives random vectors from the exact distribution and does not require "burn-in" iterations. Here is an example from the AR(1) model you have specified.

#Load the package
library(ts.extend)

#Set parameters
AR       <- 0.45
ERRORVAR <- 0.2
m        <- 1000

#Generate a random vector from this model
set.seed(1)
SERIES <- rGARMA(n = 1, m = m, ar = AR, errorvar = ERRORVAR)

#Plot the series using ggplot2 graphics
library(ggplot2)
plot(SERIES)

enter image description here

Related