Estimating AutoRegressive (AR) Model in R
We will now see how we can fit an AR model to a given time series using the arima()
function in R. Recall that AR model is an ARIMA(1, 0, 0)
model.
We can use the arima()
function in R to fit the AR model by specifying the order = c(1, 0, 0)
.
We will perform the estimation using the msft_ts
time series that we created earlier in the first lesson. If you don't have the msft_ts
loaded in your R session, please follow the steps to create it as specified in the first lesson.
Let's start by creating a plot of the original data using the plot.ts()
function.
1> plot.ts(msft_ts, main="MSFT prices", ylab="Prices")
2

As of now we are not worried about whether an AR model is best suited for this data or not. Our objective is to understand the process of fitting the AR model to this data.
We will fit the AR model to this data using the following command:
msft_ar <- arima(msft_ts , order = c(1, 0, 0))
The output contains many things including the estimated slope (ar1), mean (intercept), and innovation variance (sigma^2) as shown below:
1> msft_ar
2Call:
3arima(x = msft_ts, order = c(1, 0, 0))
4Coefficients:
5 ar1 intercept
6 0.9815 56.2260
7s.e. 0.0113 2.2359
8sigma^2 estimated as 0.5891: log likelihood = -292.55, aic = 591.1
9>
10
The msft_ar
object also contains the residuals (εt ). Using the summary()
function, you can see that the object contains a time series of residuals.
1\> summary(msft\_ar)
2 Length Class Mode
3coef 2 -none- numeric
4sigma2 1 -none- numeric
5var.coef 4 -none- numeric
6mask 2 -none- logical
7loglik 1 -none- numeric
8aic 1 -none- numeric
9arma 7 -none- numeric
10residuals 252 ts numeric
11call 3 -none- call
12series 1 -none- character
13code 1 -none- numeric
14n.cond 1 -none- numeric
15nobs 1 -none- numeric
16model 10 -none- list
17>
18
You can extract the residuals using the residuals()
function in R.
residuals <- residuals(msft\_ar)
Once you find the residuals εt, the fitted values are just X̂t\=Xt−εt
. In R, we can do it as follows:
msft\_fitted <- msft\_ts - residuals
We can now plot both the original and the fitted time series to see how close the fit is.
1ts.plot(msft\_ts)
2points(msft\_fitted, type = "l", col = 2, lty = 2)
3

Calculating Residuals Manually from the Fitted Model (Advanced Content)
The AR(1) model is expressed as follows:
**Xt = Ar \* Xt-1 + εt**
So,
**εt = Xt - Ar \* Xt-1**
Where Ar is the estimated autoregressive part in the fitted model.
1n=252
2e=rep(1,n)
3e\[1\]=0
4for (t in (2 : n)){
5 e\[t\] = msft\_ts \[t\]-coef(msft\_ar )\[1\]\*msft\_ts \[t-1\]
6}
7
Suppose the model was ARIMA(1,0,1)
1**εt = Xt - Ar \* Xt-1 - Ma \* Xt-1**
2
3\> n=252
4> e=rep(1,n)
5> e\[1\]=0
6for (t in (2 : n)){
7 e\[t\] = msft\_ts \[t\]-coef(msft\_ar )\[1\]\*msft\_ts \[t-1\]-coef(msft\_ar )\[2\]\*e\[t-1\]
8}
9