library(wooldridge)
library(gt)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
data(hprice1)
hprice1 %>%
head(5)%>%
gt()%>%
tab_header("Evidencia empirica de estimacion del modelo")
| Evidencia empirica de estimacion del modelo | |||||||||
| price | assess | bdrms | lotsize | sqrft | colonial | lprice | lassess | llotsize | lsqrft |
|---|---|---|---|---|---|---|---|---|---|
| 300 | 349.1 | 4 | 6126 | 2438 | 1 | 5.703783 | 5.855359 | 8.720297 | 7.798934 |
| 370 | 351.5 | 3 | 9903 | 2076 | 1 | 5.913503 | 5.862210 | 9.200593 | 7.638198 |
| 191 | 217.7 | 3 | 5200 | 1374 | 0 | 5.252274 | 5.383118 | 8.556414 | 7.225482 |
| 195 | 231.8 | 3 | 4600 | 1448 | 1 | 5.273000 | 5.445875 | 8.433811 | 7.277938 |
| 373 | 319.1 | 4 | 6095 | 2514 | 1 | 5.921578 | 5.765504 | 8.715224 | 7.829630 |
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
options(scipen = 9999)
Modeloautocorrelacion <- lm(formula = price~lotsize+sqrft+ bdrms, data = hprice1)
stargazer(Modeloautocorrelacion, title = "Modelo Estimado", type = "text", digits = 7)
##
## Modelo Estimado
## ===============================================
## Dependent variable:
## ---------------------------
## price
## -----------------------------------------------
## lotsize 0.0020677***
## (0.0006421)
##
## sqrft 0.1227782***
## (0.0132374)
##
## bdrms 13.8525200
## (9.0101450)
##
## Constant -21.7703100
## (29.4750400)
##
## -----------------------------------------------
## Observations 88
## R2 0.6723622
## Adjusted R2 0.6606609
## Residual Std. Error 59.8334800 (df = 84)
## F Statistic 57.4602300*** (df = 3; 84)
## ===============================================
## Note: *p<0.1; **p<0.05; ***p<0.01
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
dwtest(Modeloautocorrelacion, alternative = "two.sided",iterations = 1000)
##
## Durbin-Watson test
##
## data: Modeloautocorrelacion
## DW = 2.1098, p-value = 0.6218
## alternative hypothesis: true autocorrelation is not 0
P-value es mayor 0.05 pormel cual no se rechaza la hipotesis nula.
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
durbinWatsonTest(Modeloautocorrelacion, simulate= TRUE, reps=1000)
## lag Autocorrelation D-W Statistic p-value
## 1 -0.05900522 2.109796 0.596
## Alternative hypothesis: rho != 0
En este modelo hay evidencia de no correlacion.
#CÁLCULO MANUAL.
#Preparamos los datos.
library(dplyr)
library(tidyr)
library(kableExtra)
##
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
##
## group_rows
Ui <- Modeloautocorrelacion$residuals
Mat_x <- model.matrix(Modeloautocorrelacion)
MatInformación <- Mat_x[,-1]
Endogena <- hprice1$price
cbind(Ui, Endogena, MatInformación) %>%
as.data.frame()%>%
mutate(Lag1 = dplyr::lag(Ui, 1),
Lag2 = dplyr::lag(Ui, 2))%>%
replace_na(list(Lag1=0,Lag2=0)) -> Data.PruebaBG
#kable(head(x = Data.PruebaBG, n = 7))
#Cálculo de la regresión auxiliar.
Regresion.AuxiliarBG <- lm(Ui~lotsize+sqrft+bdrms + Lag1 + Lag2, data = Data.PruebaBG)
ResumenBG <- summary(Regresion.AuxiliarBG)
#Cálculo del estadístico LMbg.
R2 <- ResumenBG$r.squared
n <- nrow(Data.PruebaBG)
LMbg <- n*R2
#Sacamos los grados de libertad.
gl <- 2 #xq se esta verificando autocorrelación de orden dos (Lag1+Lag2+...+Lagn= gl)
#Sacamos el P Value.
P_Value <- 1-pchisq(q = LMbg, df = gl)
#Sacamos Valor Critico.
VC <- qchisq(p = 0.05,df = gl,lower.tail = FALSE)
#VC <- qchisq(p = 0.95,df = gl,lower.tail = TRUE)
#PRESENTAMOS LOS RESULTADOS.
library(stargazer)
SalidaBG <- c(LMbg, VC,P_Value)
names(SalidaBG) <- c("LMbg", "Valor Crítico","P Value")
stargazer(SalidaBG, title = "Resultado de la prueba de Breusch Godfrey", type = "text", digits = 7 )
##
## Resultado de la prueba de Breusch Godfrey
## =================================
## LMbg Valor Crítico P Value
## ---------------------------------
## 3.0334030 5.9914650 0.2194345
## ---------------------------------
library(lmtest)
PruebaMultiplicadorLagrange1 <- bgtest(Modeloautocorrelacion, order = 1)
PruebaMultiplicadorLagrange1
##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: Modeloautocorrelacion
## LM test = 0.39362, df = 1, p-value = 0.5304
P-value es mayor a 0.05 hace que nos se rechace la Hipotesis nula, es decir que los residuos del modelo no siguen una autocorrelacion de orden uno.
library(lmtest)
PruebaMultiplicadorLagrange2 <- bgtest(Modeloautocorrelacion, order = 2)
PruebaMultiplicadorLagrange2
##
## Breusch-Godfrey test for serial correlation of order up to 2
##
## data: Modeloautocorrelacion
## LM test = 3.0334, df = 2, p-value = 0.2194
Como el P-value es mayor que el 0.05 se hace que no se rechace la hipotesis nula, se concluye que losr residuos no siguen una autocorrelacion de orden dos.