\Header{ar}{Fit Autoregressive Models to Time Series}
\alias{ar.burg}{ar}
\methalias{ar.burg.default}{ar}
\methalias{ar.burg.mts}{ar}
\alias{ar.mle}{ar}
\alias{ar.yw}{ar}
\methalias{ar.yw.default}{ar}
\methalias{ar.yw.mts}{ar}
\alias{predict.ar}{ar}
\alias{print.ar}{ar}
\keyword{ts}{ar}
\begin{Description}\relax
Fit an autoregressive time series model to the data, by default
selecting the complexity by AIC.\end{Description}
\begin{Usage}
\begin{verbatim}
ar(x, aic = TRUE, order.max = NULL,
   method=c("yule-walker", "burg", "ols", "mle", "yw"), na.action,
   series, ...)
ar.burg(x, aic = TRUE, order.max = NULL,
        na.action = na.fail, demean = TRUE, series, var.method = 1, ...)
ar.yw(x, aic = TRUE, order.max = NULL,
      na.action = na.fail, demean = TRUE, series, ...)
ar.mle(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE,
       series, ...)

predict(object, newdata, n.ahead = 1, se.fit = TRUE, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] A univariate or multivariate time series.
\item[\code{aic}] Logical flag.  If \code{TRUE} then the Akaike Information
Criterion is used to choose the order of the autoregressive
model. If \code{FALSE}, the model of order \code{order.max} is
fitted.
\item[\code{order.max}] Maximum order (or order) of model to fit. Defaults
to \eqn{10\log_{10}(N)}{10*log10(N)} where \eqn{N}{} is the number
of observations except for \code{method="mle"} where it is the
minimum of this quantity and 12.
\item[\code{method}] Character string giving the method used to fit the
model.  Must be one of the strings in the default argument
(the first few characters are sufficient).  Defaults to
\code{"yule-walker"}.
\item[\code{na.action}] function to be called to handle missing values.
\item[\code{demean}] should a mean be estimated during fitting?
\item[\code{series}] names for the series.  Defaults to
\code{deparse(substitute(x))}.
\item[\code{var.method}] the method to estimate the innovations variance
(see Details).
\item[\code{...}] additional arguments for specific methods.
\item[\code{object}] a fit from \code{ar}.
\item[\code{newdata}] data to which to apply the prediction.
\item[\code{n.ahead}] number of steps ahead at which to predict.
\item[\code{se.fit}] logical: return estimated standard errors of the
prediction error?
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
For definiteness, note that the AR coefficients have the sign in

\deqn{x_t - \mu = a_1(x_{t-1} - \mu) + \cdots +  a_p(x_{t-p} - \mu) + e_t}{\code{(x[t] - m) = a[1]*(x[t-1] - m) + ... +  a[p]*(x[t-p] - m) + e[t]}}

\code{ar} is just a wrapper for the functions \code{ar.yw},
\code{ar.burg}, \code{\Link{ar.ols}} and \code{ar.mle}.

Order selection is done by AIC if \code{aic} is true. This is
problematic, as of the methods here only \code{ar.mle} performs
true maximum likelihood estimation. The AIC is computed as if the variance
estimate were the MLE, omitting the determinant term from the
likelihood. Note that this is not the same as the Gaussian likelihood
evaluated at the estimated parameter values. In \code{ar.yw} the
variance matrix of the innovations is computed from the fitted
coefficients and the autocovariance of \code{x}.

\code{ar.burg} allows two methods to estimate the innovations
variance and hence AIC. Method 1 is to use the update given by
the Levinson-Durbin recursion (Brockwell and Davis, 1991, (8.2.6)
on page 242), and follows S-PLUS. Method 2 is the mean of the sum
of squares of the forward and backward prediction errors
(as in Brockwell and Davis, 1996, page 145). Percival and Walden
(1998) discuss both. In the multivariate case the estimated
coefficients will depend (slightly) on the variance estimation method.

Remember that \code{ar} includes by default a constant in the model, by
removing the overall mean of \code{x} before fitting the AR model,
or (\code{ar.mle}) estimating a constant to subtract.\end{Details}
\begin{Value}
For \code{ar} and its methods a list of class \code{"ar"} with
the following elements:
\begin{ldescription}
\item[\code{order}] The order of the fitted model.  This is chosen by
minimizing the AIC if \code{aic=TRUE}, otherwise it is \code{order.max}.
\item[\code{ar}] Estimated autoregression coefficients for the fitted model.
\item[\code{var.pred}] The prediction variance: an estimate of the portion of the
variance of the time series that is not explained by the
autoregressive model.
\item[\code{x.mean}] The estimated mean of the series used in fitting and for
use in prediction.
\item[\code{x.intercept}] (\code{ar.ols} only.) The intercept in the model for
\code{x - x.mean}.
\item[\code{aic}] The value of the \code{aic} argument.
\item[\code{n.used}] The number of observations in the time series.
\item[\code{order.max}] The value of the \code{order.max} argument.
\item[\code{partialacf}] The estimate of the partial autocorrelation function
up to lag \code{order.max}.
\item[\code{resid}] residuals from the fitted model, conditioning on the
first \code{order} observations. The first \code{order} residuals
are set to \code{NA}. If \code{x} is a time series, so is \code{resid}.
\item[\code{method}] The value of the \code{method} argument.
\item[\code{series}] The name(s) of the time series.
\item[\code{asy.var.coef}] (univariate case.) The asymptotic-theory
variance matrix of the coefficient estimates.
\end{ldescription}


For \code{predict.ar}, a time series of predictions, or if
\code{se.fit = TRUE}, a list with components \code{pred}, the
predictions, and \code{se}, the estimated standard errors. Both
components are time series.\end{Value}
\begin{Note}\relax
Only the univariate case of \code{ar.mle} is implemented.

Fitting by \code{method="mle"} to long series can be very slow.\end{Note}
\begin{Author}\relax
Martyn Plummer. Univariate case of \code{ar.yw}, \code{ar.mle}
and C code for univariate case of \code{ar.burg} by B. D. Ripley.\end{Author}
\begin{References}\relax
Brockwell, P. J. and Davis, R. A. (1991) \emph{Time
Series and Forecasting Methods.}  Second edition. Springer, New
York. Section 11.4.

Brockwell, P. J. and Davis, R. A. (1996) \emph{Introduction to Time
Series and Forecasting.} Springer, New York. Sections 5.1 and 7.6.

Percival, D. P. and Walden, A. T. (1998) \emph{Spectral Analysis
for Physical Applications.} Cambridge University Press.

Whittle, P. (1963) On the fitting of multivariate autoregressions
and the approximate canonical factorization of a spectral density
matrix. \emph{Biometrika} \bold{40}, 129--134.\end{References}
\begin{SeeAlso}\relax
\code{\Link{ar.ols}}, \code{\Link{arima0}} for ARMA models.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
data(lh)
ar(lh)
ar(lh, method="burg")
ar(lh, method="ols")
ar(lh, FALSE, 4) # fit ar(4)

data(sunspot)
(sunspot.ar <- ar(sunspot.year))
predict(sunspot.ar, n.ahead=25)
## try the other methods too

data(BJsales)
ar(ts.union(BJsales, BJsales.lead))
## Burg is quite different here, as is OLS (see ar.ols)
ar(ts.union(BJsales, BJsales.lead), method="burg")
\end{ExampleCode}
\end{Examples}

