• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Finance Train

Finance Train

High Quality tutorials for finance, risk, data science

  • Home
  • Data Science
  • CFA® Exam
  • PRM Exam
  • Tutorials
  • Careers
  • Products
  • Login

Estimating AutoRegressive (AR) Model in R

Data Science, Statistics

This lesson is part 19 of 27 in the course Financial Time Series Analysis 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.

> plot.ts(msft_ts, main="MSFT prices", ylab="Prices")

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:

> msft_ar
Call:
arima(x = msft_ts, order = c(1, 0, 0))
Coefficients:
         ar1  intercept
      0.9815    56.2260
s.e.  0.0113     2.2359
sigma^2 estimated as 0.5891:  log likelihood = -292.55,  aic = 591.1
>

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.

> summary(msft_ar)
          Length Class  Mode
coef        2    -none- numeric
sigma2      1    -none- numeric
var.coef    4    -none- numeric
mask        2    -none- logical
loglik      1    -none- numeric
aic         1    -none- numeric
arma        7    -none- numeric
residuals 252    ts     numeric
call        3    -none- call
series      1    -none- character
code        1    -none- numeric
n.cond      1    -none- numeric
nobs        1    -none- numeric
model      10    -none- list
>

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.

ts.plot(msft_ts)
points(msft_fitted, type = "l", col = 2, lty = 2)

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.

n=252
e=rep(1,n)
e[1]=0
for (t in (2 : n)){
  e[t] = msft_ts [t]-coef(msft_ar )[1]*msft_ts [t-1]
}

Suppose the model was ARIMA(1,0,1)

εt = Xt – Ar * Xt-1 – Ma * Xt-1

> n=252
> e=rep(1,n)
> e[1]=0
for (t in (2 : n)){
  e[t] = msft_ts [t]-coef(msft_ar )[1]*msft_ts [t-1]-coef(msft_ar )[2]*e[t-1]
}
Previous Lesson

‹ AutoRegressive (AR) Model in R

Next Lesson

Forecasting with AutoRegressive (AR) Model in R ›

Join Our Facebook Group - Finance, Risk and Data Science

Posts You May Like

How to Improve your Financial Health

CFA® Exam Overview and Guidelines (Updated for 2021)

Changing Themes (Look and Feel) in ggplot2 in R

Coordinates in ggplot2 in R

Facets for ggplot2 Charts in R (Faceting Layer)

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

In this Course

  • Financial Time Series Data
  • Exploring Time Series Data in R
  • Plotting Time Series in R
  • Handling Missing Values in Time Series
  • Creating a Time Series Object in R
  • Check if an object is a time series object in R
  • Plotting Financial Time Series Data (Multiple Columns) in R
  • Characteristics of Time Series
  • Stationary Process in Time Series
  • Transforming a Series to Stationary
  • Time Series Transformation in R
  • Differencing and Log Transformation
  • Autocorrelation in R
  • Time Series Models
  • ARIMA Modeling
  • Simulate White Noise (WN) in R
  • Simulate Random Walk (RW) in R
  • AutoRegressive (AR) Model in R
  • Estimating AutoRegressive (AR) Model in R
  • Forecasting with AutoRegressive (AR) Model in R
  • Moving Average (MA) Model in R
  • Estimating Moving Average (MA) Model in R
  • ARIMA Modelling in R
  • ARIMA Modelling – Identify Model for a Time Series
  • Forecasting with ARIMA Modeling in R – Case Study
  • Automatic Identification of Model Using auto.arima() Function in R
  • Financial Time Series in R – Course Conclusion

Latest Tutorials

    • Data Visualization with R
    • Derivatives with R
    • Machine Learning in Finance Using Python
    • Credit Risk Modelling in R
    • Quantitative Trading Strategies in R
    • Financial Time Series Analysis in R
    • VaR Mapping
    • Option Valuation
    • Financial Reporting Standards
    • Fraud
Facebook Group

Membership

Unlock full access to Finance Train and see the entire library of member-only content and resources.

Subscribe

Footer

Recent Posts

  • How to Improve your Financial Health
  • CFA® Exam Overview and Guidelines (Updated for 2021)
  • Changing Themes (Look and Feel) in ggplot2 in R
  • Coordinates in ggplot2 in R
  • Facets for ggplot2 Charts in R (Faceting Layer)

Products

  • Level I Authority for CFA® Exam
  • CFA Level I Practice Questions
  • CFA Level I Mock Exam
  • Level II Question Bank for CFA® Exam
  • PRM Exam 1 Practice Question Bank
  • All Products

Quick Links

  • Privacy Policy
  • Contact Us

CFA Institute does not endorse, promote or warrant the accuracy or quality of Finance Train. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.

Copyright © 2021 Finance Train. All rights reserved.

  • About Us
  • Privacy Policy
  • Contact Us