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
library(stargazer)
options(scipen = 999999)
modelo_hprice1<-lm(formula = price~bdrms+lotsize+sqrft,data = hprice1)
stargazer(modelo_hprice1,title = "Modelo hprice1",type = "text",digits = 8)
##
## Modelo hprice1
## ===============================================
## Dependent variable:
## ---------------------------
## price
## -----------------------------------------------
## bdrms 13.85252000
## (9.01014500)
##
## lotsize 0.00206771***
## (0.00064213)
##
## sqrft 0.12277820***
## (0.01323741)
##
## Constant -21.77031000
## (29.47504000)
##
## -----------------------------------------------
## Observations 88
## R2 0.67236220
## Adjusted R2 0.66066090
## Residual Std. Error 59.83348000 (df = 84)
## F Statistic 57.46023000*** (df = 3; 84)
## ===============================================
## Note: *p<0.1; **p<0.05; ***p<0.01
#Usando libreria "lmtest"
library(stargazer)
library(lmtest)
dwtest(modelo_hprice1,alternative = "two.sided",iterations = 1000)
##
## Durbin-Watson test
##
## data: modelo_hprice1
## DW = 2.1098, p-value = 0.6218
## alternative hypothesis: true autocorrelation is not 0
#Usando libreria "car"
library(stargazer)
library(car)
durbinWatsonTest(modelo_hprice1,simulate = TRUE,reps = 1000)
## lag Autocorrelation D-W Statistic p-value
## 1 -0.05900522 2.109796 0.652
## Alternative hypothesis: rho != 0
INTERPRETACIÓN: En ambos casos, se puede rechazar la presencia de autocorrelación (No se rechaza la H0), ya que el pvalue>0.05
#Preparacion de datos:
library(dplyr)
library(tidyr)
library(kableExtra)
u_i<-modelo_hprice1$residuals
cbind(u_i,hprice1$bdrms, hprice1$lotsize, hprice1$sqrft) %>%
as.data.frame() %>%
mutate(Lag_1=dplyr::lag(u_i,1),
Lag_2=dplyr::lag(u_i,2),
Lag_3=dplyr::lag(u_i,3)) %>%
replace_na(list(Lag_1=0,Lag_2=0,Lag_3=0))->data_prueba_BG
kable(head(data_prueba_BG,6))
| u_i | V2 | V3 | V4 | Lag_1 | Lag_2 | Lag_3 |
|---|---|---|---|---|---|---|
| -45.639765 | 4 | 6126 | 2438 | 0.000000 | 0.000000 | 0.000000 |
| 74.848732 | 3 | 9903 | 2076 | -45.639765 | 0.000000 | 0.000000 |
| -8.236558 | 3 | 5200 | 1374 | 74.848732 | -45.639765 | 0.000000 |
| -12.081520 | 3 | 4600 | 1448 | -8.236558 | 74.848732 | -45.639765 |
| 18.093192 | 4 | 6095 | 2514 | -12.081520 | -8.236558 | 74.848732 |
| 62.939597 | 5 | 8566 | 2754 | 18.093192 | -12.081520 | -8.236558 |
#Calculando la regresión auxiliar y el estadístico LMBP
regresion_auxiliar_BG<-lm(u_i~hprice1$lotsize+hprice1$sqrft+hprice1$bdrms+Lag_1+Lag_2+Lag_3,data = data_prueba_BG)
sumario_BG<-summary(regresion_auxiliar_BG)
R_2_BG<-sumario_BG$r.squared
n<-nrow(data_prueba_BG)
LM_BG<-n*R_2_BG
gl=2
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 = "Resultados de la prueba de Breusch Godfrey",type = "text",digits = 6)
##
## Resultados de la prueba de Breusch Godfrey
## ===============================
## LMbg Valor Crítico p value
## -------------------------------
## 3.989284 5.991465 0.136062
## -------------------------------
INTERPRETACION: Como pvalue>0.05 No se rechaza H0, por lo tanto puede concluirse que los residuos del modelo, no siguen autocorrelación de orden “2”.
library(stargazer)
library(lmtest)
bgtest(modelo_hprice1,order = 2)
##
## Breusch-Godfrey test for serial correlation of order up to 2
##
## data: modelo_hprice1
## LM test = 3.0334, df = 2, p-value = 0.2194
iNTERPRETACIÓN: Como pvalue>0.05 No se rechaza H0, por lo tanto puede concluirse que los residuos del modelo, no siguen autocorrelación de orden “2”
library(stargazer)
library(lmtest)
bgtest(modelo_hprice1,order = 1)
##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: modelo_hprice1
## LM test = 0.39362, df = 1, p-value = 0.5304
INTERPRETACIÓN: Como pvalue>0.05 No se rechaza H0, por lo tanto puede concluirse que los residuos del modelo, no siguen autocorrelación de 1° orden