Today we are going to discuss autocorrelation problem which is a violation of one of the Gauss-Markov assumptions. Outline of the lecture is presented below.
To investigate auto-correlation we will focus on US macro data and try to obtain a consumption function. To be able to do this we will deal with a time series model. Since autocorrelation is much more common in time series data we have to resort to a time series model. In our model we will use USMacroG dataset from AER package.
install.packages("dynlm", repos = "https://cran.r-project.org/")
##
## The downloaded binary packages are in
## /var/folders/_8/f2m9fdt162ng6nj45842hhsc0000gn/T//RtmpOjv9tJ/downloaded_packages
library(dynlm)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(AER)
## Loading required package: car
## Loading required package: carData
## Loading required package: lmtest
## Loading required package: sandwich
## Loading required package: survival
data("USMacroG")
consump1 <- dynlm(consumption ~ dpi + L(dpi), data = USMacroG)
So far we have constructed a DL(1) model for the investigation of autocorrelation. Lagging component is obtained by using L() command. If we had specified as L(,x), x would be the order of lag. To conduct durbin watson test we can use two packages: “lmtest” and “car”. In car package command for Durbin-Watson test is durbinWatsonTest() and in car package function for conducting Durbin Watson test is dwtest().
install.packages("car", repos = "https://cran.r-project.org/")
##
## The downloaded binary packages are in
## /var/folders/_8/f2m9fdt162ng6nj45842hhsc0000gn/T//RtmpOjv9tJ/downloaded_packages
library(car)
durbinWatsonTest(consump1)
## lag Autocorrelation D-W Statistic p-value
## 1 0.9244708 0.0866355 0
## Alternative hypothesis: rho != 0
durbinWatsonTest(consump1, max.lag = 4)
## lag Autocorrelation D-W Statistic p-value
## 1 0.9244708 0.0866355 0
## 2 0.8634632 0.1342431 0
## 3 0.7947730 0.2123351 0
## 4 0.7183643 0.2914617 0
## Alternative hypothesis: rho[lag] != 0
install.packages("lmtest", repos = "https://cran.r-project.org/")
##
## The downloaded binary packages are in
## /var/folders/_8/f2m9fdt162ng6nj45842hhsc0000gn/T//RtmpOjv9tJ/downloaded_packages
library(lmtest)
dwtest(consump1)
##
## Durbin-Watson test
##
## data: consump1
## DW = 0.086636, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
For the arguments of the function you can consult to help segment of the R Studio.
Durbin Watson test fails to capture AC when a lagged term of the dependent variable included. In other words, BG test is applicable in AR(p), ARDL(p,q), MA(q), and DL(q) models. BG test is implemented as bgtest() function of lmtest package.
library(lmtest)
bgtest(consump1)
##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: consump1
## LM test = 192.97, df = 1, p-value < 2.2e-16
To see the difference between DW and BG tests we can construct an ARDL(2,1) model.
consump2 <- dynlm(consumption ~ dpi + L(dpi) + L(consumption, 2)+ L(consumption), data = USMacroG)
durbinWatsonTest(consump2, max.lag = 4)
## lag Autocorrelation D-W Statistic p-value
## 1 -0.10692838 2.184723 0.190
## 2 0.21865835 1.500568 0.000
## 3 0.16513471 1.594958 0.002
## 4 -0.05008718 1.987786 0.976
## Alternative hypothesis: rho[lag] != 0
bgtest(consump2)
##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: consump2
## LM test = 11.215, df = 1, p-value = 0.0008116
For investigation of the robust standard errors one should be familiar with the sandwich package. vcovHC(x, …) function provides us White’s HC consistent standard errors. Usage of the function is provided below.
install.packages("sandwich", repos = "https://cran.r-project.org/")
##
## The downloaded binary packages are in
## /var/folders/_8/f2m9fdt162ng6nj45842hhsc0000gn/T//RtmpOjv9tJ/downloaded_packages
library(sandwich)
coeftest(consump1, vcov = vcovHC(consump1, type = "HC1"))
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -81.079588 15.527398 -5.2217 4.429e-07 ***
## dpi 0.891168 0.215252 4.1401 5.116e-05 ***
## L(dpi) 0.030913 0.215612 0.1434 0.8861
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(consump1)
##
## Time series regression with "ts" data:
## Start = 1950(2), End = 2000(4)
##
## Call:
## dynlm(formula = consumption ~ dpi + L(dpi), data = USMacroG)
##
## Residuals:
## Min 1Q Median 3Q Max
## -190.02 -56.68 1.58 49.91 323.94
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -81.07959 14.50814 -5.589 7.43e-08 ***
## dpi 0.89117 0.20625 4.321 2.45e-05 ***
## L(dpi) 0.03091 0.20754 0.149 0.882
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 87.58 on 200 degrees of freedom
## Multiple R-squared: 0.9964, Adjusted R-squared: 0.9964
## F-statistic: 2.785e+04 on 2 and 200 DF, p-value: < 2.2e-16
Let us delve deeper into arguments of vcovHC function from the URL provided below. https://cran.r-project.org/web/packages/sandwich/vignettes/sandwich.pdf
Method is pretty similar with the those of White standard errors but a minor alteration is needed. Instead of using vcovHC function while specifying the method for variance-covariance matrix, we are going to use NeweyWest() function.
coeftest(consump1, vcov = NeweyWest(consump1))
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -81.079588 100.825730 -0.8042 0.42226
## dpi 0.891168 0.423003 2.1068 0.03638 *
## L(dpi) 0.030913 0.398930 0.0775 0.93831
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
coeftest(consump1)
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -81.079588 14.508136 -5.5886 7.430e-08 ***
## dpi 0.891168 0.206252 4.3208 2.448e-05 ***
## L(dpi) 0.030913 0.207542 0.1490 0.8817
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Execution of Cochrane Orcutt iterative EGLS method requires orcutt package. Reference manual of the orcutt package can be found at the URL provided below. https://cran.r-project.org/web/packages/orcutt/orcutt.pdf
Let us construct a new model by following the footsteps of Mr. Eruygur. Our new model again focuses on consumption function and obtained from USConsump1993 dataset.
install.packages("orcutt", repos = "https://cran.r-project.org/")
##
## The downloaded binary packages are in
## /var/folders/_8/f2m9fdt162ng6nj45842hhsc0000gn/T//RtmpOjv9tJ/downloaded_packages
library(orcutt)
library(AER)
data("USConsump1993")
consump93 <- lm(expenditure ~ income, data = USConsump1993)
summary(consump93)
##
## Call:
## lm(formula = expenditure ~ income, data = USConsump1993)
##
## Residuals:
## Min 1Q Median 3Q Max
## -294.52 -67.02 4.64 90.02 325.84
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -65.795821 90.990824 -0.723 0.474
## income 0.915623 0.008648 105.874 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 153.6 on 42 degrees of freedom
## Multiple R-squared: 0.9963, Adjusted R-squared: 0.9962
## F-statistic: 1.121e+04 on 1 and 42 DF, p-value: < 2.2e-16
cochrane.orcutt(consump93)
## Cochrane-orcutt estimation for first order autocorrelation
##
## Call:
## lm(formula = expenditure ~ income, data = USConsump1993)
##
## number of interaction: 4
## rho 0.782893
##
## Durbin-Watson statistic
## (original): 0.46078 , p-value: 3.274e-11
## (transformed): 1.95670 , p-value: 3.832e-01
##
## coefficients:
## (Intercept) income
## -170.317934 0.926381
This method requires prais package and it is quiet similar to Cochrane-Orcutt estimation method. Following code showa its application.
install.packages("prais", repos = "https://cran.r-project.org/")
##
## The downloaded binary packages are in
## /var/folders/_8/f2m9fdt162ng6nj45842hhsc0000gn/T//RtmpOjv9tJ/downloaded_packages
library(prais)
library(AER)
data("USConsump1993")
prais_winsten(consump93, data = USConsump1993)
## Iteration 0: rho = 0
## Iteration 1: rho = 0.7921
## Iteration 2: rho = 0.8021
## Iteration 3: rho = 0.8037
## Iteration 4: rho = 0.804
## Iteration 5: rho = 0.804
## Iteration 6: rho = 0.804
## Iteration 7: rho = 0.804
## Iteration 8: rho = 0.804
##
## Call:
## prais_winsten(formula = consump93, data = USConsump1993)
##
## Coefficients:
## (Intercept) income
## -15.1436 0.9142
##
## AR(1) coefficient rho: 0.804