Se procede a realiza la carga de datos alojadas en una página web y posteriormente a la estimación del modelo.
options(scipen = 999999)
library(foreign)
library(equatiomatic)
library(stargazer)
datos_regresion <- read.dta("https://stats.idre.ucla.edu/stat/data/crime.dta")
modelo_crime<-lm(crime~poverty+single,data=datos_regresion)
extract_eq(modelo_crime,wrap = TRUE)
\[ \begin{aligned} \operatorname{crime} &= \alpha + \beta_{1}(\operatorname{poverty}) + \beta_{2}(\operatorname{single}) + \epsilon \end{aligned} \]
stargazer(modelo_crime, title = "Estimación Modelo Crime", type = "html", digits = 3)
| Dependent variable: | |
| crime | |
| poverty | 6.787 |
| (8.989) | |
| single | 166.373*** |
| (19.423) | |
| Constant | -1,368.189*** |
| (187.205) | |
| Observations | 51 |
| R2 | 0.707 |
| Adjusted R2 | 0.695 |
| Residual Std. Error | 243.610 (df = 48) |
| F Statistic | 57.964*** (df = 2; 48) |
| Note: | p<0.1; p<0.05; p<0.01 |
library(lmtest)
white_test<-bptest(modelo_crime,~I(poverty^2)+I(single^2)+poverty*single,data = datos_regresion)
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 p-value < 0.05
library(car)
durbinWatsonTest(model = modelo_crime)
## lag Autocorrelation D-W Statistic p-value
## 1 -0.07014421 2.040007 0.938
## Alternative hypothesis: rho != 0
No hay evidencia de Autocorrelación de 1º orden ya que p-value > 0.05
Autocorrelación 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 Autocorrelación de 1º orden ya que p-value > 0.05
Al tener evidencia de homocedasticidad, es necesario realizar la correción en el modelo y para ello se utilizan los estimadores HAC.
Verificando inicialmente el modelo sin corregir
options(scipen = 99999)
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
Verificando la correción del modelo utilizando el estimador HAC
options(scipen = 99999)
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
Inicialmente para comparar ambos modelo se debe crear el objeto que identifique al modelo con estimación robusta en este caso es: modelo_crime_robust:
options(scipen = 999999)
library(robustbase)
library(stargazer)
modelo_crime_robust<-lmrob(crime~poverty+single,data=datos_regresion)
Posteriormente con la librería stargazer anteriormente cargada, se crea el codigo con el comando stargazer que de forma estética presentará la comparación entre ambos modelos:
library(sandwich)
cov<-vcovHC(modelo_crime, type = "HC")
robust.se<-sqrt(diag(cov))
stargazer(modelo_crime,modelo_crime, se=list(NULL,robust.se), title = "Comparación Modelo Normal y Modelo Corregido", column.labels=c("Modelo Normal","Modelo Corregido"),type = "html", align=TRUE)
| Dependent variable: | ||
| crime | ||
| Modelo Normal | Modelo Corregido | |
| (1) | (2) | |
| poverty | 6.787 | 6.787 |
| (8.989) | (10.601) | |
| single | 166.373*** | 166.373*** |
| (19.423) | (25.451) | |
| Constant | -1,368.189*** | -1,368.189*** |
| (187.205) | (276.411) | |
| Observations | 51 | 51 |
| R2 | 0.707 | 0.707 |
| Adjusted R2 | 0.695 | 0.695 |
| Residual Std. Error (df = 48) | 243.610 | 243.610 |
| F Statistic (df = 2; 48) | 57.964*** | 57.964*** |
| Note: | p<0.1; p<0.05; p<0.01 | |