ar(x, aic=T, order.max=<<see below>>, method="yule-walker")
The estimation is performed using the sample mean of each univariate series as the estimate of the mean. Remember that the coefficients in ar are for the series with the mean(s) removed.
a <- ar(log(lynx)) acf.plot(a, conf=T) # Take a look at the partial correlations tsplot(a$aic) # Fit an AR(11) to this time series lynx.fit <- ar(log(lynx), aic=F, order=11, method="b")# function to predict using an ar model: # ahead gives the number of predictions to make
pred.ar <- function(series, ar.est, ahead = 1) { order <- ar.est$order series <- as.matrix(series) pred.out <- array(NA, dim = c(order + ahead, ncol(series)), dimnames = list(NULL, dimnames(series)[[2]])) mean.ser <- apply(series, 2, mean) ser.cent <- sweep(series, 2, mean.ser) pred.out[seq(order), ] <- ser.cent[rev(nrow(series) - seq(order) + 1), ] for(i in (order + 1):nrow(pred.out)) { pred.out[i, ] <- apply(aperm(ar.est$ar, c(1, 3, 2)) * as.vector(pred.out[i - seq(order), ]), 3, sum) } sweep(pred.out[ - seq(order), , drop = F], 2, mean.ser, "+") }
sun.ar <- ar(sunspots) tsplot(sun.ar$resid, main="Residuals from AR(27) Fit of Sunspots") # the structure in the residuals means the model is inadequate