## AR(2) phi0 <- 0 phi1 <- 1 #0.7 phi2 <- 0 #-0.3 y <- rep(NA, 100) y[1] <- rnorm(1) y[2] <- rnorm(1) for(t in 3:100) { y[t] <- phi0 + phi1*y[t-1] + phi2*y[t-2] + rnorm(1) } plot(y, type="l") acf(y) pacf(y) y.1 <- c(NA, y[1:99]) y.2 <- c(NA, NA, y[1:98]) dati <- as.data.frame(cbind(y, y.1, y.2)) ar2mod <- lm(y~y.1+y.2, dati) summary(ar2mod) ## MA(2) n <- 100 theta1 <- 0.7 theta2 <- -0.8 y <- rep(NA, n) e <- rep(NA, n) y[1] <- e[1] <- rnorm(1) y[2] <- e[2] <- rnorm(1) for(i in 3:n) { e[i] <- rnorm(1) y[i] <- e[i] - theta1*e[i-1] - theta2*e[i-2] } plot(y, type="l") mean(y) acf(y) pacf(y)