DENIS FERNANDO FLAMENCO NOLASCO
20 de junio de 2019
library(foreign)
datos_regresion <- read.dta("https://stats.idre.ucla.edu/stat/data/crime.dta")
modelo_estimado_1<-lm(crime~poverty+single,data=datos_regresion)
print(modelo_estimado_1)##
## Call:
## lm(formula = crime ~ poverty + single, data = datos_regresion)
##
## Coefficients:
## (Intercept) poverty single
## -1368.189 6.787 166.373
library(stargazer)
residuos<-modelo_estimado_1$residuals
data_prueba_white<-as.data.frame(cbind(residuos,datos_regresion))
regresion_auxiliar<-lm(I(residuos^2)~poverty+single+I(poverty^2)+I(single^2)+poverty*single,data = data_prueba_white)
sumario<-summary(regresion_auxiliar)
n<-nrow(data_prueba_white)
R_2<-sumario$r.squared
BP<-n*R_2
gl=2+2+1
p_value<-1-pchisq(q = BP,df = gl)
VC<-qchisq(p = 0.95,df = gl)
salida_white<-c(BP,gl,VC,p_value)
names(salida_white)<-c("BP","gl","Valor Critico","P-value")
stargazer(salida_white,title = "Resultados de la prueba de White",type = "text",digits = 6)##
## Resultados de la prueba de White
## ===================================
## BP gl Valor Critico P-value
## -----------------------------------
## 10.730090 5 11.070500 0.057002
## -----------------------------------
#Obtenemos p-value<valor de significancia, hay evidencia de heterocedasticidad.library(dplyr)
library(tidyr)
library(kableExtra)
cbind(residuos,datos_regresion) %>%
as.data.frame() %>%
mutate(Lag_1=dplyr::lag(residuos,1),
Lag_2=dplyr::lag(residuos,2)) %>%
replace_na(list(Lag_1=0,Lag_2=0))->data_prueba_BG
kable(head(data_prueba_BG,6))| residuos | sid | state | crime | murder | pctmetro | pctwhite | pcths | poverty | single | Lag_1 | Lag_2 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| -311.70552 | 1 | ak | 761 | 9.0 | 41.8 | 75.2 | 86.6 | 9.1 | 14.3 | 0.00000 | 0.00000 |
| 116.80291 | 2 | al | 780 | 11.6 | 67.4 | 73.5 | 66.9 | 17.4 | 11.5 | -311.70552 | 0.00000 |
| 45.25394 | 3 | ar | 593 | 10.2 | 44.7 | 82.9 | 66.3 | 20.0 | 10.7 | 116.80291 | -311.70552 |
| -34.44604 | 4 | az | 715 | 8.6 | 84.7 | 88.6 | 78.7 | 15.4 | 12.1 | 45.25394 | 116.80291 |
| 243.00035 | 5 | ca | 1078 | 13.1 | 96.7 | 79.3 | 76.2 | 18.2 | 12.5 | -34.44604 | 45.25394 |
| -145.11556 | 6 | co | 567 | 5.8 | 81.8 | 92.5 | 84.4 | 9.9 | 12.1 | 243.00035 | -34.44604 |
library(lmtest)
bgtest(modelo_estimado_1,order = 2)##
## Breusch-Godfrey test for serial correlation of order up to 2
##
## data: modelo_estimado_1
## LM test = 0.27165, df = 2, p-value = 0.873
#No hay evidencia de autocorrelacion de 2° orden ya que (pvalue>0.05)library(lmtest)
bgtest(modelo_estimado_1,order = 1)##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: modelo_estimado_1
## LM test = 0.27156, df = 1, p-value = 0.6023
#No hay evidencia de autocorrelacion de 1° orden ya que (pvalue>0.05)library(lmtest)
library(sandwich)
#Sin corregir:
coeftest(modelo_estimado_1)##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1368.1887 187.2052 -7.3085 2.479e-09 ***
## poverty 6.7874 8.9885 0.7551 0.4539
## single 166.3727 19.4229 8.5658 3.117e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Corregido:
estimacion_omega<-vcovHC(modelo_estimado_1,type = "HC1")
coeftest(modelo_estimado_1,vcov. = estimacion_omega)##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1368.1887 284.9180 -4.8020 1.577e-05 ***
## poverty 6.7874 10.9273 0.6211 0.5374
## single 166.3727 26.2343 6.3418 7.519e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library(lmtest)
library(sandwich)
#Corregido:
estimacion_omega<-NeweyWest(modelo_estimado_1,lag = 2)
coeftest(modelo_estimado_1,vcov. = estimacion_omega)##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1368.1887 303.8466 -4.5029 4.280e-05 ***
## poverty 6.7874 10.5943 0.6407 0.5248
## single 166.3727 25.9154 6.4198 5.708e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1