library(foreign)
datos_regresion <- read.dta("https://stats.idre.ucla.edu/stat/data/crime.dta")
modelo_estimado<-lm(crime~pctmetro+pctwhite+poverty+single,data=datos_regresion)
print(modelo_estimado)
##
## Call:
## lm(formula = crime ~ pctmetro + pctwhite + poverty + single,
## data = datos_regresion)
##
## Coefficients:
## (Intercept) pctmetro pctwhite poverty single
## -1191.689 7.404 -3.509 16.665 120.358
library(stargazer)
options(scipen = 999)
modelo_estimado_1<-lm(crime~pctmetro+pctwhite+poverty+single,data=datos_regresion)
stargazer(modelo_estimado_1, type = "text", title = "modelo estimado")
##
## modelo estimado
## ===============================================
## Dependent variable:
## ---------------------------
## crime
## -----------------------------------------------
## pctmetro 7.404***
## (1.285)
##
## pctwhite -3.509
## (2.639)
##
## poverty 16.665**
## (6.927)
##
## single 120.358***
## (17.850)
##
## Constant -1,191.689***
## (386.009)
##
## -----------------------------------------------
## Observations 51
## R2 0.846
## Adjusted R2 0.832
## Residual Std. Error 180.599 (df = 46)
## F Statistic 63.068*** (df = 4; 46)
## ===============================================
## Note: *p<0.1; **p<0.05; ***p<0.01
library(stargazer)
X_mat<-model.matrix(modelo_estimado_1)
stargazer(head(X_mat,n=6),type="text")
##
## ==============================================
## (Intercept) pctmetro pctwhite poverty single
## ----------------------------------------------
## 1 1 41.800 75.200 9.100 14.300
## 2 1 67.400 73.500 17.400 11.500
## 3 1 44.700 82.900 20 10.700
## 4 1 84.700 88.600 15.400 12.100
## 5 1 96.700 79.300 18.200 12.500
## 6 1 81.800 92.500 9.900 12.100
## ----------------------------------------------
XX_matrix<-t(X_mat)%*%X_mat
stargazer(XX_matrix,type = "text")
##
## =====================================================================
## (Intercept) pctmetro pctwhite poverty single
## ---------------------------------------------------------------------
## (Intercept) 51 3,436.900 4,289.900 727.200 577.600
## pctmetro 3,436.900 255,719.100 284,188.700 48,701.470 39,529.700
## pctwhite 4,289.900 284,188.700 369,637.100 59,985.870 47,662.020
## poverty 727.200 48,701.470 59,985.870 11,419.780 8,502.660
## single 577.600 39,529.700 47,662.020 8,502.660 6,766.640
## ---------------------------------------------------------------------
library(stargazer)
options(scipen = 999)
Sn<-solve(diag(sqrt(diag(XX_matrix))))
stargazer(Sn,type = "text")
##
## =============================
## 0.140 0 0 0 0
## 0 0.002 0 0 0
## 0 0 0.002 0 0
## 0 0 0 0.009 0
## 0 0 0 0 0.012
## -----------------------------
library(stargazer)
XX_norm<-(Sn%*%XX_matrix)%*%Sn
stargazer(XX_norm,type = "text",digits = 4)
##
## ==================================
## 1 0.9517 0.9880 0.9529 0.9832
## 0.9517 1 0.9244 0.9012 0.9503
## 0.9880 0.9244 1 0.9233 0.9530
## 0.9529 0.9012 0.9233 1 0.9673
## 0.9832 0.9503 0.9530 0.9673 1
## ----------------------------------
library(stargazer)
#autovalores
lambdas<-eigen(XX_norm,symmetric = TRUE)
stargazer(lambdas$values,type = "text")
##
## =============================
## 4.799 0.100 0.078 0.020 0.003
## -----------------------------
K<-sqrt(max(lambdas$values)/min(lambdas$values))
print(K)
## [1] 40.06455
library(stargazer)
Zn<-scale(X_mat[,-1])
stargazer(head(Zn,n=6),type = "text")
##
## ==================================
## pctmetro pctwhite poverty single
## ----------------------------------
## 1 -1.165 -0.672 -1.125 1.402
## 2 0.0004 -0.801 0.685 0.082
## 3 -1.033 -0.092 1.252 -0.295
## 4 0.788 0.338 0.249 0.365
## 5 1.335 -0.363 0.860 0.554
## 6 0.656 0.632 -0.951 0.365
## ----------------------------------
library(stargazer)
n<-nrow(Zn)
R<-(t(Zn)%*%Zn)*(1/(n-1))
#También se puede calcular R a través de cor(X_mat[,-1])
stargazer(R,type = "text",digits = 4)
##
## ==========================================
## pctmetro pctwhite poverty single
## ------------------------------------------
## pctmetro 1 -0.3372 -0.0605 0.2598
## pctwhite -0.3372 1 -0.3893 -0.6564
## poverty -0.0605 -0.3893 1 0.5486
## single 0.2598 -0.6564 0.5486 1
## ------------------------------------------
determinante_R<-det(R)
print(determinante_R)
## [1] 0.3253133
m<-ncol(X_mat[,-1])
n<-nrow(X_mat[,-1])
chi_FG<--(n-1-(2*m+5)/6)*log(determinante_R)
print(chi_FG)
## [1] 53.71524
gl<-m*(m-1)/2
VC<-qchisq(p = 0.95,df = gl)
print(VC)
## [1] 12.59159
inversa_R<-solve(R)
print(inversa_R)
## pctmetro pctwhite poverty single
## pctmetro 1.2202728 0.3763671 0.3701879 -0.2730588
## pctwhite 0.3763671 1.8770428 0.1876815 1.0314172
## poverty 0.3701879 0.1876815 1.5458828 -0.8210318
## single -0.2730588 1.0314172 -0.8210318 2.1984133
VIFs<-diag(inversa_R)
print(VIFs)
## pctmetro pctwhite poverty single
## 1.220273 1.877043 1.545883 2.198413
library(mctest)
mc.plot(x = X_mat[,-1],y = datos_regresion$crime,vif = 3,)
## Si se usa el umbral de 3 en teoria deberian excluirse todas las variables por lo que es necesario reestimar el modelo
library(fitdistrplus)
library(stargazer)
fit_normal<-fitdist(data = modelo_estimado_1$residuals,distr = "norm")
plot(fit_normal)
## [6] Verifique a traves de las pruebas de JB, KS y SW el supuesto de normalidad ¿Se contradicen los resultados de las 3 pruebas entre si? Explique ### Jarque Bera
library(normtest)
jb.norm.test(modelo_estimado_1$residuals)
##
## Jarque-Bera test for normality
##
## data: modelo_estimado_1$residuals
## JB = 4.1309, p-value = 0.0635
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.11291, df = 1, p-value = 0.7368
library(lmtest)
library(sandwich)
#Sin corregir:
coeftest(modelo_estimado_1)
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1191.6892 386.0089 -3.0872 0.003418 **
## pctmetro 7.4041 1.2849 5.7622 0.00000065706 ***
## pctwhite -3.5091 2.6392 -1.3296 0.190210
## poverty 16.6655 6.9271 2.4058 0.020212 *
## single 120.3576 17.8502 6.7426 0.00000002218 ***
## ---
## 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) -1191.6892 408.5059 -2.9172 0.005446 **
## pctmetro 7.4041 1.6296 4.5436 0.000039925 ***
## pctwhite -3.5091 2.8916 -1.2135 0.231124
## poverty 16.6655 7.0547 2.3623 0.022446 *
## single 120.3576 21.9495 5.4834 0.000001709 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
El modelo se ve explicado por las cuatro variables, en si los crimenes violentos se ven mas explicados por la pobreza y el hecho de ser padre soltero.