# R source code for generating data from AR(1) processes. # arsim1: # Function to generate observations from a stationary AR(1) # process. Supply the correlation parameter and the number # of observations to generate. Here, we generate the process # by looping. ar1sim <- function(n, phi=0) { if (n < 1) stop(message="n < 1.") if (abs(phi) > 1) stop(message="abs(phi) > 1.") z <- rnorm(n) y <- z y[1] <- z[1]/sqrt(1 - phi^2) if (n > 1) for (i in seq(2,n)) y[i] <- phi * y[i-1] + z[i] y }