library(foreign)
library(haven)
crime <- read_dta("C:/Users/USUARIO/Downloads/crime.dta")
modelo_crime<-lm(crime~poverty+single, data = crime)
print(modelo_crime)

Call: lm(formula = crime ~ poverty + single, data = crime)

Coefficients: (Intercept) poverty single
-1368.189 6.787 166.373

Prueba de white(Prueba de Breusch Pagan)

library(lmtest)
white_test<-bptest(modelo_crime,~I(poverty^2)+I(single^2)+poverty*single, data=crime)
print(white_test)
## 
##  studentized Breusch-Pagan test
## 
## data:  modelo_crime
## BP = 10.73, df = 5, p-value = 0.057

hay evidencia de heterocedasticidad ya que pvalues<0.05

Prueba de multiplicador de Lagrange(Breusch Godfrey)

Autocorrelacion de 2° orden

library(lmtest)
prueba_LM<-bgtest(modelo_crime, order=2)
print(prueba_LM)
## 
##  Breusch-Godfrey test for serial correlation of order up to 2
## 
## data:  modelo_crime
## LM test = 0.27165, df = 2, p-value = 0.873

No hay evidencia de autocorrelacion de 2° orden, ya que pvalue>0.05

Autocorrelacion de 1° orden(Prueba de Durbin Watson)

library(car)
durbinWatsonTest(modelo_crime)
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.07014421      2.040007   0.938
##  Alternative hypothesis: rho != 0

no hay evidencia de autocorrelacion de 1° orden, ya que pvalue>0.05

Estimacion Robusta(uso del estimador HAC)

sin corregir

options(scipen=9999999)
library(lmtest)
coeftest(modelo_crime)
## 
## t test of coefficients:
## 
##               Estimate Std. Error t value         Pr(>|t|)    
## (Intercept) -1368.1887   187.2052 -7.3085 0.00000000247861 ***
## poverty         6.7874     8.9885  0.7551           0.4539    
## single        166.3727    19.4229  8.5658 0.00000000003117 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Corregido(usando estimador HAC)

options(scipen=9999999)
library(lmtest)
library(sandwich)
estimacion_omega<-vcovHC(modelo_crime, type="HC0")
coeftest(modelo_crime, vcov.=estimacion_omega)
## 
## t test of coefficients:
## 
##               Estimate Std. Error t value      Pr(>|t|)    
## (Intercept) -1368.1887   276.4111 -4.9498 0.00000956181 ***
## poverty         6.7874    10.6010  0.6403        0.5251    
## single        166.3727    25.4510  6.5370 0.00000003774 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Estimador HAC de NeweyWest

library(lmtest)
library(sandwich)
# corregido
estimacion_omega<-NeweyWest(modelo_crime, lag = 2)
coeftest(modelo_crime, vcov. = estimacion_omega)
## 
## t test of coefficients:
## 
##               Estimate Std. Error t value      Pr(>|t|)    
## (Intercept) -1368.1887   303.8466 -4.5029 0.00004279768 ***
## poverty         6.7874    10.5943  0.6407        0.5248    
## single        166.3727    25.9154  6.4198 0.00000005708 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Estimacion robusta

options(scipen=999999)
library(robustbase)
library(stargazer)
modelo_crime_robust<-lmrob(crime~poverty+single, data=crime)
stargazer(modelo_crime, modelo_crime_robust, type="html", title="comparativa")
comparativa
Dependent variable:
crime
OLS MM-type
linear
(1) (2)
poverty 6.787 11.466
(8.989) (9.263)
single 166.373*** 176.569***
(19.423) (23.223)
Constant -1,368.189*** -1,539.640***
(187.205) (235.765)
Observations 51 51
R2 0.707 0.795
Adjusted R2 0.695 0.787
Residual Std. Error (df = 48) 243.610 191.864
F Statistic 57.964*** (df = 2; 48)
Note: p<0.1; p<0.05; p<0.01