Importación de datos

library(readr)
ejemplo_regresion <- read_csv("C:/Users/Jacqueline Vanessa/Desktop/UES/Ciclo I - 2022/EMA118/TAREAS/UNIDAD 2/Prueba clase - sesion asincronica 5 - 6/ejemplo_regresion.csv")
head(ejemplo_regresion,n=6)
## # A tibble: 6 x 3
##      X1    X2     Y
##   <dbl> <dbl> <dbl>
## 1  3.92  7298  0.75
## 2  3.61  6855  0.71
## 3  3.32  6636  0.66
## 4  3.07  6506  0.61
## 5  3.06  6450  0.7 
## 6  3.11  6402  0.72
modelo_ejemplo<-lm(formula = Y~X1+X2,data = ejemplo_regresion)



Heterocedasticidad

Prueba white

library(stargazer)
resid<-modelo_ejemplo$residuals
data_reg_aux<-as.data.frame(cbind(resid,ejemplo_regresion))
reg_aux<-lm(formula=I(resid**2)~X1+X2+I(X1**2)+I(X2**2)+X1*X2,
            data=data_reg_aux)
resumen<-summary(reg_aux)
R_2<-resumen$r.squared
n<-nrow(data_reg_aux)
LM_w<-n*R_2
gl<-2+2+1
VC<-qchisq(p=0.95,df=gl) #o también: VC<-qchisq(p=0.05, lower.tail = FALSE,df=gl)
p_value<-1-pchisq(q=LM_w, df=gl)
salida_prueba<-c(LM_w,VC,p_value)
names(salida_prueba)<-c("LMw", "Valor Crítico", "P value")
stargazer(salida_prueba, title = "Prueba de White", type = "html", digits = 6)
Prueba de White
LMw Valor Crítico P value
3.690182 11.070500 0.594826


Prueba white con librería skedastic

library(skedastic)
white_lm(modelo_ejemplo,interactions=FALSE)
## # A tibble: 1 x 5
##   statistic p.value parameter method       alternative
##       <dbl>   <dbl>     <dbl> <chr>        <chr>      
## 1      3.58   0.466         4 White's Test greater



Autocorrelación

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