Cargamos de los datos

#Cargamos los datos
library(wooldridge)
## Warning: package 'wooldridge' was built under R version 4.0.5
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
  1. Estimamos el modelo
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
model_lote<-lm(formula = price~lotsize+sqrft+bdrms,data = hprice1)
stargazer(model_lote,title = "Lotes Precio",type = "text")
## 
## Lotes Precio
## ===============================================
##                         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
  1. Verificacion de la independencia de los residuos (no autocorrelacion)
  1. Prueba de Durbin Watson.

-Usando la libreria “lmtest”.

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

-Usando la libreria “car”

library(car)
## Warning: package 'car' was built under R version 4.0.5
## Loading required package: carData
durbinWatsonTest(model_lote,simulate = TRUE,reps = 1000)
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.05900522      2.109796    0.67
##  Alternative hypothesis: rho != 0

Interpretacion: Para ambos casos podemos observar que el estadistico Durbin Watson tiene un valor muy cercano a 2, por lo que podemos considerar que no hay presencia de autocorrelacion de primer orden.

  1. PRUEBA DEL MULTIPLICADOR DE LAGRANGE

Preparacion de los datos

library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.5
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.0.5
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.0.5
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
u_i<-model_lote$residuals
datos_lote<-as.data.frame(cbind(hprice1$price,hprice1$lotsize,hprice1$sqrft,hprice1$bdrms))
names(datos_lote)<-c("Y","X1","X2","X3")
cbind(u_i,datos_lote) %>% 
 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 Y X1 X2 X3 lag_1 lag_2 lag_3
-45.639765 300.000 6126 2438 4 0.000000 0.000000 0.000000
74.848732 370.000 9903 2076 3 -45.639765 0.000000 0.000000
-8.236558 191.000 5200 1374 3 74.848732 -45.639765 0.000000
-12.081520 195.000 4600 1448 3 -8.236558 74.848732 -45.639765
18.093192 373.000 6095 2514 4 -12.081520 -8.236558 74.848732
62.939597 466.275 8566 2754 5 18.093192 -12.081520 -8.236558

Calculo de la regresion auxiliar y estadistico LM_BP

library(stargazer)
regresion_auxiliar_BG<-lm(u_i~X1+X2+X3+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 Critico", "Valor P")
stargazer(salida_bg,title = "Resultados de la prueba de BG",type = "text")
## 
## Resultados de la prueba de BG
## ===========================
## LMbg  Valor Critico Valor P
## ---------------------------
## 3.989     5.991      0.136 
## ---------------------------

PRUEBA DE BREUSCH GODFREY PARA AUTOCORELACION DE PRIMER ORDEN

library(lmtest)
bgtest(model_lote,order = 1)
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  model_lote
## LM test = 0.39362, df = 1, p-value = 0.5304

Interpretacion:Basandonos en el P_value obtenido, no rechazamos la hipotesis nula, por lo tanto podemos concluir que los resisduos del modelo no siguen autocorrelacion de orden 1.

PRUEBA DE BREUSCH GODFREY PARA AUTOCORELACION DE SEGUNDO ORDEN

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

Interpretacion:En base al valor p observado concluimos que no rechazamos la hipotesis nula lo cual nos sugiere que los residuos del modelo no siguen autocorrelacion de orden 2.