Error Correction Models

Table of Contents

We'll get the data from the FRED API using the quantmod R package, and then we'll estimate error-correction models in both Python and R. For some reason the python package to access FRED (fredapi) requires an API key whereas quantmod does not.

library(quantmod)
library(tseries)
library(EIAdata)

1. 1 vs 2 Month Crude Oil Futures

key <- source("~/eia_key")$value
one_month <- getEIA('PET.RCLC1.W', key = key)
two_month <- getEIA('PET.RCLC2.W', key = key)
oil_data <- merge.xts(one_month, two_month, join="inner")
oil_spurious_reg <- lm(I(log(oil_data$PET.RCLC2.W)) ~ I(log(oil_data$PET.RCLC1.W)))
summary(oil_spurious_reg)

Call:
lm(formula = I(log(oil_data$PET.RCLC2.W)) ~ I(log(oil_data$PET.RCLC1.W)))

Residuals:
     Min       1Q   Median       3Q      Max 
-0.12913 -0.01096  0.00131  0.00853  1.58039 

Coefficients:
                             Estimate Std. Error t value Pr(>|t|)    
(Intercept)                  0.002357   0.005397   0.437    0.662    
I(log(oil_data$PET.RCLC1.W)) 0.999474   0.001473 678.355   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.04251 on 1965 degrees of freedom
Multiple R-squared:  0.9957,	Adjusted R-squared:  0.9957 
F-statistic: 4.602e+05 on 1 and 1965 DF,  p-value: < 2.2e-16
adf.test(oil_spurious_reg$resid)

	Augmented Dickey-Fuller Test

data:  oil_spurious_reg$resid
Dickey-Fuller = -9.0417, Lag order = 12, p-value = 0.01
alternative hypothesis: stationary

Warning message:
In adf.test(oil_spurious_reg$resid) : p-value smaller than printed p-value

We can reject the null of a unit root in the residuals, which is evidence that the two series are cointegrated.

Author: Matt Brigida, Ph.D.

Created: 2022-09-17 Sat 12:06

Validate