usando wooldridge

library(wooldridge)
data(hprice1)
head(force(hprice1),n=5)
##   price assess bdrms lotsize sqrft colonial   lprice  lassess llotsize   lsqrft
## 1   300  349.1     4    6126  2438        1 5.703783 5.855359 8.720297 7.798934
## 2   370  351.5     3    9903  2076        1 5.913503 5.862210 9.200593 7.638198
## 3   191  217.7     3    5200  1374        0 5.252274 5.383118 8.556414 7.225482
## 4   195  231.8     3    4600  1448        1 5.273000 5.445875 8.433811 7.277938
## 5   373  319.1     4    6095  2514        1 5.921578 5.765504 8.715224 7.829630

modelo estimado

library(stargazer)
modelo_price <- lm( formula = price ~ lotsize + sqrft + bdrms, data = hprice1)
stargazer(modelo_price, title = "modelo estimado", type = "text")
## 
## modelo estimado
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                price           
## -----------------------------------------------
## lotsize                      0.002***          
##                               (0.001)          
##                                                
## sqrft                        0.123***          
##                               (0.013)          
##                                                
## bdrms                         13.853           
##                               (9.010)          
##                                                
## Constant                      -21.770          
##                              (29.475)          
##                                                
## -----------------------------------------------
## Observations                    88             
## R2                             0.672           
## Adjusted R2                    0.661           
## Residual Std. Error      59.833 (df = 84)      
## F Statistic           57.460*** (df = 3; 84)   
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

prueba de DW

library(lmtest)
## Warning: package 'lmtest' was built under R version 4.2.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.3
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
dwtest(modelo_price,alternative = "two.sided",iterations = 1000)
## 
##  Durbin-Watson test
## 
## data:  modelo_price
## DW = 2.1098, p-value = 0.6218
## alternative hypothesis: true autocorrelation is not 0

no se rechaza la hipotesis porque p_values=0.6218>0.05 por lo tanto no hay evidencia de autocorrelacion

usando car

library(car)
durbinWatsonTest(modelo_price,simulate = TRUE,rep = 1000)
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.05900522      2.109796    0.61
##  Alternative hypothesis: rho != 0

no se rechaza la hipotesis nula nivel de significancia<p_value no hay evidencia de autocorrelcion

prueba de lagranje

library(stargazer)
library(dplyr)
library(tidyr)
library(kableExtra)
residuos<- modelo_price$residuals
cbind(residuos,hprice1) %>% 
  as.data.frame() %>%
  mutate(Lag_1=dplyr::lag(residuos,1),
         Lag_2=dplyr::lag(residuos,2),
         Lag_3=dplyr::lag(residuos,3)) %>%
  replace_na(list(Lag_1=0,Lag_2=0,Lag_3=0))->data_BG
kable(head(data_BG,n=6))
residuos price assess bdrms lotsize sqrft colonial lprice lassess llotsize lsqrft Lag_1 Lag_2 Lag_3
-45.639765 300.000 349.1 4 6126 2438 1 5.703783 5.855359 8.720297 7.798934 0.000000 0.000000 0.000000
74.848732 370.000 351.5 3 9903 2076 1 5.913503 5.862210 9.200593 7.638198 -45.639765 0.000000 0.000000
-8.236558 191.000 217.7 3 5200 1374 0 5.252274 5.383118 8.556414 7.225481 74.848732 -45.639765 0.000000
-12.081520 195.000 231.8 3 4600 1448 1 5.273000 5.445875 8.433811 7.277938 -8.236558 74.848732 -45.639765
18.093192 373.000 319.1 4 6095 2514 1 5.921578 5.765504 8.715224 7.829630 -12.081520 -8.236558 74.848732
62.939597 466.275 414.5 5 8566 2754 1 6.144775 6.027073 9.055556 7.920810 18.093192 -12.081520 -8.236558

estadistico LBMP

library(stargazer)
regresion_aux_BG<-lm(residuos~lotsize+sqrft+bdrms+Lag_1+Lag_2+Lag_3,data = data_BG)
sumario_BG<-summary(regresion_aux_BG)
R_2_BG<-sumario_BG$r.squared
n<-nrow(data_BG)
LM_BG<-n*R_2_BG
gl=3
p_value<-1-pchisq(q = LM_BG,df = gl)
VC<-qchisq(p = 0.95,df = gl)
salida_BG<-c(LM_BG,VC,p_value)
names(salida_BG)<-c("LMBG","Valor Crítico","p value")
stargazer(salida_BG,title = " prueba de Breusch Godfrey",type = "text",digits = 6)
## 
## prueba de Breusch Godfrey
## ===============================
## LMBG     Valor Crítico p value 
## -------------------------------
## 3.989284   7.814728    0.262624
## -------------------------------

no hay evidencia de autorrelacion de orden 2 porque pvalue>0.05

usando lmtest

library(lmtest)
bgtest(modelo_price,order=2)
## 
##  Breusch-Godfrey test for serial correlation of order up to 2
## 
## data:  modelo_price
## LM test = 3.0334, df = 2, p-value = 0.2194

no hay evidencia de autorrelacion de orden 2 porque pvalue=0.2194>0.05