Learn R Programming

desk (version 1.1.2)

ar1sim: Simulate AR(1) Process

Description

Simulates an autoregressive process of order 1.

Usage

ar1sim(n = 50, rho, u0 = 0, var.e = 1, details = FALSE, seed = NULL)

Value

A list object including:

u.simvector of simulated AR(1) values.
ntotal number of simulated AR(1) values.
rhotrue rho value of AR(1) process.
e.simnormal errors in AR(1) process.

Arguments

n

total number of observations to be generated (one predetermined start value u0 and n-1 random values)

rho

true rho value of the AR(1) process to be simulated.

u0

start value of the process in t = 0.

var.e

variance of the random error. If zero, no random error is added.

details

logical value indicating whether details should be printed.

seed

optionally set a custom random seed for reproducing results.

Examples

Run this code
## Generate 30 positively autocorrelated errors
my.ar1 <- ar1sim(n = 30, rho = 0.9, var.e = 0.1, seed = 511)
my.ar1
plot(my.ar1$u.sim, type = 'l')

## Illustrate the effect of Rho on the AR(1)
set.seed(12)
parOrg = par(c("mfrow", "mar"))
par(mfrow = c(2,4), mar = c(1,1,1,1))
rhovalues <- c(0.1, 0.5, 0.8, 0.99)
for (i in c(0, 0.3)){
  for (rho in rhovalues){
    u.data <- ar1sim(n = 20, u0 = 2, rho = rho, var.e = i)
    plot(u.data$u.sim, plot.what = "lag", cex.legend = 0.7, xlim = c(-2.5,2.5), ylim = c(-2.5,2.5),
         acc.line = TRUE, ols.line = TRUE)
  }
}
par(mfrow = parOrg$"mfrow", mar = parOrg$"mar")

## Illustrate the effect of Rho on the (non-)stationarity of the AR(1)
set.seed(1324)
parOrg = par(c("mfrow", "mar"))
par(mfrow = c(2, 4), mar = c(1,1,1,1))
for (rho in c(0.1, 0.9, 1, 1.04, -0.1, -0.9, -1, -1.04)){
  u.data <- ar1sim(n = 25, u0 = 5, rho = rho, var.e = 0)
  plot(u.data$u.sim, plot.what = "time", ylim = c(-8,8))
}
par(mfrow = parOrg$"mfrow", mar = parOrg$"mar")

Run the code above in your browser using DataLab