Time Series Models

Time series models are a little different from other models that we’ve seen in R. With most other models, the goal is to predict a value (the response variable) from a set of other variables (the predictor variables). Usually, we explicitly assume that there is no autocorrelation: that the sequence of observations does not matter.

With time series, we assume the opposite: we assume that previous observations help predict future observations (see Figure 23-1).

Extrapolating times series

Figure 23-1. Extrapolating times series

To fit an autoregressive model to a time series, use the function ar:

ar(x, aic = TRUE, order.max = NULL,
   method=c("yule-walker", "burg", "ols", "mle", "yw"),
   na.action, series, ...)

Here is a description of the arguments to ar.

ArgumentDescription 
xA time series. 
aicA logical value that specifies whether the Akaike information criterion is used to choose the order of the model.TRUE
order.maxA numeric value specifying the maximum order of the model to fit.NULL
methodA character value that specifies the method to use for fitting the model. Specify method="yw" (or method="yule-walker") for the Yule-Walker method, method="burg" for the Burg method, method="ols" for ordinary least squares, or method="mle" for maximum likelihood estimation.c(“yule-walker”, “burg”, “ols”, “mle”, “yw”)
na.actionA function that specifies how to handle missing values. 
seriesA character vector of names ...

Get R in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.