Autocorrelación de primer orden
Prueba de Durbin Watson
Librería lmtest
library(lmtest)
dwtest(modelo_ejemplo,alternative = "two.sided",iterations = 1000)
##
## Durbin-Watson test
##
## data: modelo_ejemplo
## DW = 1.9483, p-value = 0.5649
## alternative hypothesis: true autocorrelation is not 0
Librería car
library(car)
durbinWatsonTest(modelo_ejemplo,simulate = TRUE, reps = 1000)
## lag Autocorrelation D-W Statistic p-value
## 1 -0.04366918 1.948305 0.528
## Alternative hypothesis: rho != 0
Autocorrelación de orden superior
library(dplyr)
library(tidyr)
library(stargazer)
library(kableExtra)
cbind(resid, ejemplo_regresion) %>% as.data.frame() %>%
mutate(Lag_1=dplyr::lag(resid,1),
Lag_2=dplyr::lag(resid,2)) %>%
replace_na(list(Lag_1=0,Lag_2=0)) -> data_prueba_BG
kable(head(data_prueba_BG,6))
resid
|
X1
|
X2
|
Y
|
Lag_1
|
Lag_2
|
0.0734697
|
3.92
|
7298
|
0.75
|
0.0000000
|
0.0000000
|
-0.0033412
|
3.61
|
6855
|
0.71
|
0.0734697
|
0.0000000
|
-0.0391023
|
3.32
|
6636
|
0.66
|
-0.0033412
|
0.0734697
|
-0.0621832
|
3.07
|
6506
|
0.61
|
-0.0391023
|
-0.0033412
|
0.0162403
|
3.06
|
6450
|
0.70
|
-0.0621832
|
-0.0391023
|
0.0124247
|
3.11
|
6402
|
0.72
|
0.0162403
|
-0.0621832
|
# Calculando la regresión auxiliar y el estadístico LMBP
reg_aux_BG<-lm(resid~X1+X2+Lag_1+Lag_2,data = data_prueba_BG)
resumen_BG<-summary(reg_aux_BG)
n<-nrow(ejemplo_regresion)
gl<-2
R_2_BG<-resumen_BG$r.squared
LM_BG<-n*R_2_BG
VC<-qchisq(p=0.05,lower.tail = FALSE,df=gl)
p_value_BG<-pchisq(q=LM_BG,lower.tail = FALSE,df=gl)
salida_BG<-c(LM_BG,VC,p_value_BG)
names(salida_BG)<-c("LM BG", "Valor Crítico", "Valor P")
stargazer(salida_BG,title = "Prueba de BG", type = "html",digits = 6)
Prueba de BG
|
LM BG
|
Valor Crítico
|
Valor P
|
|
3.305189
|
5.991465
|
0.191552
|
|
Usando lmtest
library(lmtest)
bgtest(modelo_ejemplo,order = 2)
##
## Breusch-Godfrey test for serial correlation of order up to 2
##
## data: modelo_ejemplo
## LM test = 3.3052, df = 2, p-value = 0.1916