rm(list=ls())
setwd("/home/daniel/Dropbox/Papers-ponencias/Segregacion2020-isaforum/Analisis")
library(lavaan)
library(psych)
library(dplyr)
library(semPlot)
Abrir base de datos.
load("EC2016.rdata")
Preparar variables.
#Preparar variables
nosabe <- function(x){
na_if(x,"No sabe")
} # Función para reemplazar "No sabe" por NA
noresponde <- function(x){
na_if(x,"No responde")
} # Función para reemplazar "No responde" por NA
#Loop para repetir funciones desde la columna 25 a la 31
for(i in 25:31){
EC2016[i] <- nosabe(EC2016[i]) # Reemplazar no sabe por NA
EC2016[i] <- noresponde(EC2016[i]) # Reemplazar no responde por NA
EC2016[i] <- recode(EC2016[,i], 'Muy Malo' = "Muy Mal",
'Malo' = "Mal",
'Ni Bueno/Ni Malo' = "Ni bien ni mal",
'Bueno' = "Bien",
'Muy Bueno' = "Muy Bien") #Recodificar
EC2016[i]<-droplevels(EC2016[i]) # Borrar niveles no usados
}
#Eliminar casos NA's en variables de evaluacion policias y seleccionar variables
round(prop.table(table(is.na(EC2016$D6_1)))*100,2) # 2.25% con información faltante
##
## FALSE TRUE
## 97.75 2.25
round(prop.table(table(is.na(EC2016$D6_2)))*100,2) # 10.04% con información faltante
##
## FALSE TRUE
## 89.96 10.04
round(prop.table(table(is.na(EC2016$D6_3)))*100,2) # 6.17% con información faltante
##
## FALSE TRUE
## 93.83 6.17
round(prop.table(table(is.na(EC2016$D6_4)))*100,2) # 7.80% con información faltante
##
## FALSE TRUE
## 92.2 7.8
round(prop.table(table(is.na(EC2016$D6_5)))*100,2) # 23.04% con información faltante
##
## FALSE TRUE
## 76.96 23.04
round(prop.table(table(is.na(EC2016$D6_6)))*100,2) # 15.08% con información faltante
##
## FALSE TRUE
## 84.92 15.08
round(prop.table(table(is.na(EC2016$D6_7)))*100,2) # 12.25% con información faltante
##
## FALSE TRUE
## 87.75 12.25
EC2016 <- EC2016[complete.cases(EC2016[,25:31]),] #Eliminar casos con información faltante (20,649 casos)
ev.policial <- select(EC2016, c(D6_1, D6_2, D6_3, D6_4, D6_5, D6_6, D6_7))
#Transformar variables en vectores numéricos
ev.policial <- as.data.frame(sapply(ev.policial, as.numeric))
Calcular matriz de correlaciones.
# Matriz de correlaciones de Pearson
cor.ev.pol <- cor(ev.policial)
cor.plot(cor.ev.pol)
# Matriz de correlaciones policóricas
polycor.ev.pol <- polychoric(ev.policial)
rho <- polycor.ev.pol$rho
cor.plot(polycor.ev.pol$rho)
Test de Barlett.
cortest.bartlett(cor.ev.pol, n = 53392) #chisq: 72562.16 p: 0 df: 6 . No es una matriz de identidad, es apropiado realizar EFA
## $chisq
## [1] 209854.2
##
## $p.value
## [1] 0
##
## $df
## [1] 21
Test KMO.
KMO(ev.policial) # 0.79, indica adecuación de EFA (baja correlación parcial entre variables)
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = ev.policial)
## Overall MSA = 0.9
## MSA for each item =
## D6_1 D6_2 D6_3 D6_4 D6_5 D6_6 D6_7
## 0.90 0.90 0.93 0.92 0.88 0.88 0.91
Determinante de la matriz de correlaciones.
det(cor.ev.pol) # Mayor que 0.00001 (0.2568851). Baja colinealidad, es apropiado realizar EFA.
## [1] 0.01962896
Scree plot para examinar cuantos factores extraer.
fa.parallel(rho, n.obs=53392, fm="pa", fa="fa", main = "Scree Plot") # Explorar 1 a 4 factores
## Parallel analysis suggests that the number of factors = 4 and the number of components = NA
Estimar EFA sin rotacion.
f1 <- fa(ev.policial, nfactor=1, cor="poly", fm="mle", rotate = "none")
f2 <- fa(ev.policial, nfactor=2, cor="poly", fm="mle", rotate = "none")
f3 <- fa(ev.policial, nfactor=3, cor="poly", fm="mle", rotate = "none")
f4 <- fa(ev.policial, nfactor=4, cor="poly", fm="mle", rotate = "none") # No hay grados de libertad
¿Cual solución ajusta mejor? El de 3 factores
f1$BIC
## [1] 14239.43
f2$BIC
## [1] 4286.294
f3$BIC
## [1] 1030.131
print(f3)
## Factor Analysis using method = ml
## Call: fa(r = ev.policial, nfactors = 3, rotate = "none", fm = "mle",
## cor = "poly")
## Standardized loadings (pattern matrix) based upon correlation matrix
## ML1 ML2 ML3 h2 u2 com
## D6_1 0.59 0.51 0.35 0.72 0.277 2.6
## D6_2 0.63 0.46 0.32 0.71 0.285 2.4
## D6_3 0.43 0.52 0.09 0.47 0.535 2.0
## D6_4 0.72 0.41 0.01 0.68 0.321 1.6
## D6_5 0.71 0.54 -0.24 0.85 0.146 2.1
## D6_6 0.72 0.48 -0.17 0.77 0.225 1.9
## D6_7 1.00 -0.04 0.00 1.00 0.005 1.0
##
## ML1 ML2 ML3
## SS loadings 3.46 1.43 0.32
## Proportion Var 0.49 0.20 0.05
## Cumulative Var 0.49 0.70 0.74
## Proportion Explained 0.66 0.27 0.06
## Cumulative Proportion 0.66 0.94 1.00
##
## Mean item complexity = 1.9
## Test of the hypothesis that 3 factors are sufficient.
##
## The degrees of freedom for the null model are 21 and the objective function was 4.9 with Chi Square of 210322.3
## The degrees of freedom for the model are 3 and the objective function was 0.02
##
## The root mean square of the residuals (RMSR) is 0.01
## The df corrected root mean square of the residuals is 0.03
##
## The harmonic number of observations is 42965 with the empirical chi square 216.14 with prob < 1.4e-46
## The total number of observations was 42965 with Likelihood Chi Square = 1062.14 with prob < 6e-230
##
## Tucker Lewis Index of factoring reliability = 0.965
## RMSEA index = 0.091 and the 90 % confidence intervals are 0.086 0.095
## BIC = 1030.13
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy
## ML1 ML2 ML3
## Correlation of (regression) scores with factors 1.00 0.93 0.76
## Multiple R square of scores with factors 1.00 0.86 0.57
## Minimum correlation of possible factor scores 0.99 0.71 0.14
print(f3$loadings,cutoff = 0.3)
##
## Loadings:
## ML1 ML2 ML3
## D6_1 0.587 0.507 0.347
## D6_2 0.631 0.461 0.321
## D6_3 0.432 0.521
## D6_4 0.717 0.406
## D6_5 0.712 0.538
## D6_6 0.716 0.482
## D6_7 0.997
##
## ML1 ML2 ML3
## SS loadings 3.457 1.430 0.317
## Proportion Var 0.494 0.204 0.045
## Cumulative Var 0.494 0.698 0.744
names(ev.policial)[1]="Patrullaje"
names(ev.policial)[2]="Oportunidad"
names(ev.policial)[3]="Trato"
names(ev.policial)[4]="Eficacia"
names(ev.policial)[5]="Municipio"
names(ev.policial)[6]="Vecinos"
names(ev.policial)[7]="Narcotráfico"
cfa_evpol <- '
F1 =~ Patrullaje + Oportunidad + Trato + Eficacia + Municipio + Vecinos + Narcotráfico
'
fit.evpol <- cfa(cfa_evpol, data=ev.policial,
ordered=c("Patrullaje", "Oportunidad", "Trato", "Eficacia", "Municipio", "Vecinos", "Narcotráfico"))
summary(fit.evpol,fit.measures=TRUE,standardized=TRUE)
## lavaan 0.6-7 ended normally after 23 iterations
##
## Estimator DWLS
## Optimization method NLMINB
## Number of free parameters 35
##
## Number of observations 42965
##
## Model Test User Model:
## Standard Robust
## Test Statistic 7675.484 18146.819
## Degrees of freedom 14 14
## P-value (Chi-square) 0.000 0.000
## Scaling correction factor 0.423
## Shift parameter 1.601
## simple second-order correction
##
## Model Test Baseline Model:
##
## Test statistic 1394241.197 631783.212
## Degrees of freedom 21 21
## P-value 0.000 0.000
## Scaling correction factor 2.207
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 0.995 0.971
## Tucker-Lewis Index (TLI) 0.992 0.957
##
## Robust Comparative Fit Index (CFI) NA
## Robust Tucker-Lewis Index (TLI) NA
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.113 0.174
## 90 Percent confidence interval - lower 0.111 0.172
## 90 Percent confidence interval - upper 0.115 0.176
## P-value RMSEA <= 0.05 0.000 0.000
##
## Robust RMSEA NA
## 90 Percent confidence interval - lower NA
## 90 Percent confidence interval - upper NA
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.044 0.044
##
## Parameter Estimates:
##
## Standard errors Robust.sem
## Information Expected
## Information saturated (h1) model Unstructured
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## F1 =~
## Patrullaje 1.000 0.772 0.772
## Oportunidad 1.021 0.003 344.759 0.000 0.788 0.788
## Trato 0.836 0.004 232.997 0.000 0.645 0.645
## Eficacia 1.071 0.003 370.133 0.000 0.826 0.826
## Municipio 1.152 0.003 377.499 0.000 0.889 0.889
## Vecinos 1.136 0.003 373.430 0.000 0.876 0.876
## Narcotráfico 1.018 0.003 331.204 0.000 0.785 0.785
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .Patrullaje 0.000 0.000 0.000
## .Oportunidad 0.000 0.000 0.000
## .Trato 0.000 0.000 0.000
## .Eficacia 0.000 0.000 0.000
## .Municipio 0.000 0.000 0.000
## .Vecinos 0.000 0.000 0.000
## .Narcotráfico 0.000 0.000 0.000
## F1 0.000 0.000 0.000
##
## Thresholds:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## Patrullaje|t1 -1.335 0.008 -157.491 0.000 -1.335 -1.335
## Patrullaje|t2 -0.394 0.006 -63.302 0.000 -0.394 -0.394
## Patrullaje|t3 0.283 0.006 46.094 0.000 0.283 0.283
## Patrullaje|t4 1.669 0.010 161.066 0.000 1.669 1.669
## Oportunidad|t1 -1.048 0.007 -141.156 0.000 -1.048 -1.048
## Oportunidad|t2 -0.134 0.006 -22.113 0.000 -0.134 -0.134
## Oportunidad|t3 0.584 0.006 90.792 0.000 0.584 0.584
## Oportunidad|t4 1.862 0.012 156.198 0.000 1.862 1.862
## Trato|t1 -1.688 0.010 -160.799 0.000 -1.688 -1.688
## Trato|t2 -0.939 0.007 -131.862 0.000 -0.939 -0.939
## Trato|t3 -0.136 0.006 -22.470 0.000 -0.136 -0.136
## Trato|t4 1.456 0.009 160.665 0.000 1.456 1.456
## Eficacia|t1 -1.234 0.008 -153.160 0.000 -1.234 -1.234
## Eficacia|t2 -0.268 0.006 -43.672 0.000 -0.268 -0.268
## Eficacia|t3 0.503 0.006 79.379 0.000 0.503 0.503
## Eficacia|t4 1.873 0.012 155.799 0.000 1.873 1.873
## Municipio|t1 -1.317 0.008 -156.833 0.000 -1.317 -1.317
## Municipio|t2 -0.349 0.006 -56.506 0.000 -0.349 -0.349
## Municipio|t3 0.588 0.006 91.224 0.000 0.588 0.588
## Municipio|t4 1.977 0.013 151.240 0.000 1.977 1.977
## Vecinos|t1 -1.298 0.008 -156.098 0.000 -1.298 -1.298
## Vecinos|t2 -0.330 0.006 -53.448 0.000 -0.330 -0.330
## Vecinos|t3 0.540 0.006 84.697 0.000 0.540 0.540
## Vecinos|t4 1.901 0.012 154.675 0.000 1.901 1.901
## Narcotráfico|t1 -0.822 0.007 -120.003 0.000 -0.822 -0.822
## Narcotráfico|t2 0.062 0.006 10.261 0.000 0.062 0.062
## Narcotráfico|t3 0.796 0.007 117.059 0.000 0.796 0.796
## Narcotráfico|t4 2.025 0.014 148.779 0.000 2.025 2.025
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .Patrullaje 0.404 0.404 0.404
## .Oportunidad 0.379 0.379 0.379
## .Trato 0.584 0.584 0.584
## .Eficacia 0.317 0.317 0.317
## .Municipio 0.209 0.209 0.209
## .Vecinos 0.232 0.232 0.232
## .Narcotráfico 0.383 0.383 0.383
## F1 0.596 0.003 188.546 0.000 1.000 1.000
##
## Scales y*:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## Patrullaje 1.000 1.000 1.000
## Oportunidad 1.000 1.000 1.000
## Trato 1.000 1.000 1.000
## Eficacia 1.000 1.000 1.000
## Municipio 1.000 1.000 1.000
## Vecinos 1.000 1.000 1.000
## Narcotráfico 1.000 1.000 1.000
semPaths(fit.evpol)
## Warning in abbreviate(Labels, nCharNodes): abreviatura utilizada con caracteres
## no ASCII
modin=modificationIndices(fit.evpol)
modin
## lhs op rhs mi epc sepc.lv sepc.all sepc.nox
## 59 Patrullaje ~~ Oportunidad 4046.408 0.190 0.190 0.485 0.485
## 60 Patrullaje ~~ Trato 309.027 0.065 0.065 0.134 0.134
## 61 Patrullaje ~~ Eficacia 48.951 -0.024 -0.024 -0.067 -0.067
## 62 Patrullaje ~~ Municipio 983.614 -0.110 -0.110 -0.380 -0.380
## 63 Patrullaje ~~ Vecinos 838.854 -0.101 -0.101 -0.330 -0.330
## 64 Patrullaje ~~ Narcotráfico 197.320 -0.052 -0.052 -0.132 -0.132
## 65 Oportunidad ~~ Trato 36.873 0.024 0.024 0.051 0.051
## 66 Oportunidad ~~ Eficacia 5.991 -0.008 -0.008 -0.023 -0.023
## 67 Oportunidad ~~ Municipio 1104.359 -0.116 -0.116 -0.411 -0.411
## 68 Oportunidad ~~ Vecinos 912.149 -0.104 -0.104 -0.350 -0.350
## 69 Oportunidad ~~ Narcotráfico 7.939 -0.010 -0.010 -0.026 -0.026
## 70 Trato ~~ Eficacia 44.631 0.025 0.025 0.058 0.058
## 71 Trato ~~ Municipio 82.527 -0.034 -0.034 -0.099 -0.099
## 72 Trato ~~ Vecinos 1.735 0.005 0.005 0.013 0.013
## 73 Trato ~~ Narcotráfico 656.203 -0.114 -0.114 -0.240 -0.240
## 74 Eficacia ~~ Municipio 0.547 -0.002 -0.002 -0.008 -0.008
## 75 Eficacia ~~ Vecinos 432.171 -0.064 -0.064 -0.236 -0.236
## 76 Eficacia ~~ Narcotráfico 735.880 0.081 0.081 0.232 0.232
## 77 Municipio ~~ Vecinos 2852.571 0.144 0.144 0.653 0.653
## 78 Municipio ~~ Narcotráfico 24.442 -0.016 -0.016 -0.055 -0.055
## 79 Vecinos ~~ Narcotráfico 16.589 0.012 0.012 0.042 0.042
#Ajustes según índice de modificación
cfa_evpol <- '
F1 =~ Patrullaje + Oportunidad + Trato + Eficacia + Municipio + Vecinos + Narcotráfico
Patrullaje ~~ Oportunidad
Municipio ~~ Vecinos
Oportunidad ~~ Municipio
Trato ~~ Narcotráfico
Patrullaje ~~ Trato
Eficacia ~~ Vecinos
'
fit.evpol <- cfa(cfa_evpol, data=ev.policial,
ordered=c("Patrullaje", "Oportunidad", "Trato", "Eficacia", "Municipio", "Vecinos", "Narcotráfico"))
summary(fit.evpol,fit.measures=TRUE,standardized=TRUE)
## lavaan 0.6-7 ended normally after 32 iterations
##
## Estimator DWLS
## Optimization method NLMINB
## Number of free parameters 41
##
## Number of observations 42965
##
## Model Test User Model:
## Standard Robust
## Test Statistic 199.372 562.444
## Degrees of freedom 8 8
## P-value (Chi-square) 0.000 0.000
## Scaling correction factor 0.355
## Shift parameter 0.509
## simple second-order correction
##
## Model Test Baseline Model:
##
## Test statistic 1394241.197 631783.212
## Degrees of freedom 21 21
## P-value 0.000 0.000
## Scaling correction factor 2.207
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 1.000 0.999
## Tucker-Lewis Index (TLI) 1.000 0.998
##
## Robust Comparative Fit Index (CFI) NA
## Robust Tucker-Lewis Index (TLI) NA
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.024 0.040
## 90 Percent confidence interval - lower 0.021 0.037
## 90 Percent confidence interval - upper 0.026 0.043
## P-value RMSEA <= 0.05 1.000 1.000
##
## Robust RMSEA NA
## 90 Percent confidence interval - lower NA
## 90 Percent confidence interval - upper NA
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.009 0.009
##
## Parameter Estimates:
##
## Standard errors Robust.sem
## Information Expected
## Information saturated (h1) model Unstructured
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## F1 =~
## Patrullaje 1.000 0.714 0.714
## Oportunidad 1.052 0.003 310.352 0.000 0.751 0.751
## Trato 0.927 0.004 220.638 0.000 0.662 0.662
## Eficacia 1.205 0.004 283.872 0.000 0.860 0.860
## Municipio 1.189 0.004 280.643 0.000 0.849 0.849
## Vecinos 1.187 0.005 261.895 0.000 0.848 0.848
## Narcotráfico 1.138 0.004 277.439 0.000 0.813 0.813
##
## Covariances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .Patrullaje ~~
## .Oportunidad 0.183 0.002 73.437 0.000 0.183 0.395
## .Municipio ~~
## .Vecinos 0.102 0.002 48.662 0.000 0.102 0.364
## .Oportunidad ~~
## .Municipio -0.015 0.002 -9.076 0.000 -0.015 -0.044
## .Trato ~~
## .Narcotráfico -0.128 0.003 -44.832 0.000 -0.128 -0.292
## .Patrullaje ~~
## .Trato 0.075 0.003 29.561 0.000 0.075 0.144
## .Eficacia ~~
## .Vecinos -0.042 0.002 -25.707 0.000 -0.042 -0.154
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .Patrullaje 0.000 0.000 0.000
## .Oportunidad 0.000 0.000 0.000
## .Trato 0.000 0.000 0.000
## .Eficacia 0.000 0.000 0.000
## .Municipio 0.000 0.000 0.000
## .Vecinos 0.000 0.000 0.000
## .Narcotráfico 0.000 0.000 0.000
## F1 0.000 0.000 0.000
##
## Thresholds:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## Patrullaje|t1 -1.335 0.008 -157.491 0.000 -1.335 -1.335
## Patrullaje|t2 -0.394 0.006 -63.302 0.000 -0.394 -0.394
## Patrullaje|t3 0.283 0.006 46.094 0.000 0.283 0.283
## Patrullaje|t4 1.669 0.010 161.066 0.000 1.669 1.669
## Oportunidad|t1 -1.048 0.007 -141.156 0.000 -1.048 -1.048
## Oportunidad|t2 -0.134 0.006 -22.113 0.000 -0.134 -0.134
## Oportunidad|t3 0.584 0.006 90.792 0.000 0.584 0.584
## Oportunidad|t4 1.862 0.012 156.198 0.000 1.862 1.862
## Trato|t1 -1.688 0.010 -160.799 0.000 -1.688 -1.688
## Trato|t2 -0.939 0.007 -131.862 0.000 -0.939 -0.939
## Trato|t3 -0.136 0.006 -22.470 0.000 -0.136 -0.136
## Trato|t4 1.456 0.009 160.665 0.000 1.456 1.456
## Eficacia|t1 -1.234 0.008 -153.160 0.000 -1.234 -1.234
## Eficacia|t2 -0.268 0.006 -43.672 0.000 -0.268 -0.268
## Eficacia|t3 0.503 0.006 79.379 0.000 0.503 0.503
## Eficacia|t4 1.873 0.012 155.799 0.000 1.873 1.873
## Municipio|t1 -1.317 0.008 -156.833 0.000 -1.317 -1.317
## Municipio|t2 -0.349 0.006 -56.506 0.000 -0.349 -0.349
## Municipio|t3 0.588 0.006 91.224 0.000 0.588 0.588
## Municipio|t4 1.977 0.013 151.240 0.000 1.977 1.977
## Vecinos|t1 -1.298 0.008 -156.098 0.000 -1.298 -1.298
## Vecinos|t2 -0.330 0.006 -53.448 0.000 -0.330 -0.330
## Vecinos|t3 0.540 0.006 84.697 0.000 0.540 0.540
## Vecinos|t4 1.901 0.012 154.675 0.000 1.901 1.901
## Narcotráfico|t1 -0.822 0.007 -120.003 0.000 -0.822 -0.822
## Narcotráfico|t2 0.062 0.006 10.261 0.000 0.062 0.062
## Narcotráfico|t3 0.796 0.007 117.059 0.000 0.796 0.796
## Narcotráfico|t4 2.025 0.014 148.779 0.000 2.025 2.025
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .Patrullaje 0.490 0.490 0.490
## .Oportunidad 0.436 0.436 0.436
## .Trato 0.562 0.562 0.562
## .Eficacia 0.260 0.260 0.260
## .Municipio 0.279 0.279 0.279
## .Vecinos 0.281 0.281 0.281
## .Narcotráfico 0.339 0.339 0.339
## F1 0.510 0.004 142.969 0.000 1.000 1.000
##
## Scales y*:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## Patrullaje 1.000 1.000 1.000
## Oportunidad 1.000 1.000 1.000
## Trato 1.000 1.000 1.000
## Eficacia 1.000 1.000 1.000
## Municipio 1.000 1.000 1.000
## Vecinos 1.000 1.000 1.000
## Narcotráfico 1.000 1.000 1.000
semPaths(fit.evpol)
## Warning in abbreviate(Labels, nCharNodes): abreviatura utilizada con caracteres
## no ASCII
Guardar puntajes factoriales y normalización (valores de 0 a 1).
EC2016$ev.policial <- predict(fit.evpol)
EC2016$ev.policial <- round(((EC2016$ev.policial -min(EC2016$ev.policial))/
(max(EC2016$ev.policial)-min(EC2016$ev.policial))), 2)
Guardar base de datos.
save(EC2016, file = "datos.rdata")