Practica 5

ALEXANDER DANIEL ALVAREZ BERARDI

14/6/2019

PRUEBA DE HETEROCEDASTICIDAD

library(readr)
library(dplyr)
library(stargazer)
library(lmtest)
library(car)
library(tidyr)
library(kableExtra)
ejemplo_regresion<-read.csv("C:\\Users\\AD_be\\Desktop\\Econometria\\ejemplo_regresion.csv")
modelo_lineal<-lm(Y~ï..X1+X2,data = ejemplo_regresion)
stargazer(modelo_lineal,title="modelo estimado",type="text")
## 
## modelo estimado
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                  Y             
## -----------------------------------------------
## ï..X1                        0.237***          
##                               (0.056)          
##                                                
## X2                          -0.0002***         
##                              (0.00003)         
##                                                
## Constant                     1.564***          
##                               (0.079)          
##                                                
## -----------------------------------------------
## Observations                    25             
## R2                             0.865           
## Adjusted R2                    0.853           
## Residual Std. Error       0.053 (df = 22)      
## F Statistic           70.661*** (df = 2; 22)   
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

Prueba de White

u_i<-modelo_lineal$residuals
data_prueba_white<-as.data.frame(cbind(u_i,ejemplo_regresion))
regresion_aux<-lm(I(u_i^2)~ï..X1+X2+I(ï..X1^2)+I(X2^2)+ï..X1*X2,data=data_prueba_white)
sumario<-summary(regresion_aux)
n<-nrow(data_prueba_white)
R_2<-sumario$r.squared
LM_W<-n*R_2
gl=2+2+1
p_value<-1-pchisq(q=LM_W,df=gl)
VC<-qchisq(p=0.95, df=gl)
salida_white<-c(LM_W,VC,p_value)
names(salida_white)<-c("LMw","valor critico","p value")
stargazer(salida_white,title = "Resultados de la prueba de White",type = "text",digits = 6)
## 
## Resultados de la prueba de White
## ===============================
## LMw      valor critico p value 
## -------------------------------
## 3.690182   11.070500   0.594826
## -------------------------------

Prueba de White usando libreria lmtest

prueba_white<-bptest(modelo_lineal,~I(ï..X1^2)+I(X2^2)+ï..X1*X2,data=ejemplo_regresion)
print(prueba_white)
## 
##  studentized Breusch-Pagan test
## 
## data:  modelo_lineal
## BP = 3.6902, df = 5, p-value = 0.5948

Prueba de DW

dwtest(modelo_lineal,alternative = "two.sided",iterations = 1000)
## 
##  Durbin-Watson test
## 
## data:  modelo_lineal
## DW = 1.9483, p-value = 0.5649
## alternative hypothesis: true autocorrelation is not 0
durbinWatsonTest(modelo_lineal,simulate=TRUE,reps=1000)
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.04366918      1.948305   0.524
##  Alternative hypothesis: rho != 0

Autocorrelacion de orden superior

data_prueba_BG<-cbind(u_i,ejemplo_regresion)%>%as.data.frame()%>%mutate(Lag_1=dplyr::lag(u_i,1),lag_2=dplyr::lag(u_i,2))%>% replace_na(list(Lag_1=0,Lag_2=0))
kable(head(data_prueba_BG,6))                                                                    
u_i ï..X1 X2 Y Lag_1 lag_2
0.0734697 3.92 7298 0.75 0.0000000 NA
-0.0033412 3.61 6855 0.71 0.0734697 NA
-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

regresion_aux_BG<-lm(u_i~ï..X1+X2+Lag_1+lag_2,data=data_prueba_BG)
sumario_BG<-summary(regresion_aux_BG)
R_2_BG<-sumario_BG$r.squared
n1<-nrow(data_prueba_BG)
LM_BG<-n1*R_2_BG
gl1=2
p_value1<-1-pchisq(q = LM_BG,df = gl1)
VC1<-qchisq(p = 0.95,df = gl1)
salida_bg<-c(LM_BG,VC1,p_value1)
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.667591   5.991465    0.159806
## -------------------------------

Prueba del Multiplicador de Lagrange Breusch-Godfrey usando libreria lmtest

bgtest(modelo_lineal,order = 2)
## 
##  Breusch-Godfrey test for serial correlation of order up to 2
## 
## data:  modelo_lineal
## LM test = 3.3052, df = 2, p-value = 0.1916

El test BG puede usarse también para verificar la autocorrelación de 1° orden

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