Simulate AR(1) Process
Simulates an autoregressive process of order 1.
ar1sim(n = 50, rho, u0 = 0, var.e = 1, details = FALSE, seed = NULL)
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.A list object including:
u.sim | vector of simulated AR(1) values. |
n | total number of simulated AR(1) values. |
rho | true rho value of AR(1) process. |
e.sim | normal errors in AR(1) process. |
Objects generated by ar1sim()
can be plotted using the regular plot()
command.
plot.what = "time"
plots simulated AR(1) values over time. Available options are
... | other arguments that plot() understands. |
plot.what = "lag"
plots simulated AR(1) values over its lagged values. Available options are
true.line | logical value (default: TRUE). Should the true line be plotted? |
acc.line | logical value (default: FALSE). Should the autocorrelation coefficient line be plotted? |
ols.line | logical value (default: FALSE). Should the ols regression line be plotted? |
... | other arguments that plot() understands. |
## 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")