ANÁLISIS FACTORIAL

library(rio)
dataprincipal=import("criminal_eap.xlsx")
str(dataprincipal)
## 'data.frame':    140 obs. of  10 variables:
##  $ Country                                                           : chr  "Afghanistan" "Albania" "Algeria" "Angola" ...
##  $ Year                                                              : chr  "2022" "2022" "2022" "2022" ...
##  $ Factor 8: Criminal Justice                                        : num  0.261 0.4 0.397 0.346 0.519 ...
##  $ 8.1 Criminal investigation system is effective                    : num  0.298 0.424 0.266 0.354 0.456 ...
##  $ 8.2 Criminal adjudication system is timely and effective          : num  0.412 0.362 0.438 0.353 0.503 ...
##  $ 8.3 Correctional system is effective in reducing criminal behavior: num  0.212 0.37 0.466 0.235 0.408 ...
##  $ 8.4 Criminal system is impartial                                  : num  0.169 0.493 0.567 0.376 0.457 ...
##  $ 8.5 Criminal system is free of corruption                         : num  0.27 0.334 0.42 0.497 0.692 ...
##  $ 8.6 Criminal system is free of improper government influence      : num  0.186 0.303 0.153 0.292 0.546 ...
##  $ 8.7. Due process of the law and rights of the accused             : num  0.278 0.515 0.471 0.316 0.574 ...
set.seed(123)
names(dataprincipal)
##  [1] "Country"                                                           
##  [2] "Year"                                                              
##  [3] "Factor 8: Criminal Justice"                                        
##  [4] "8.1 Criminal investigation system is effective"                    
##  [5] "8.2 Criminal adjudication system is timely and effective"          
##  [6] "8.3 Correctional system is effective in reducing criminal behavior"
##  [7] "8.4 Criminal system is impartial"                                  
##  [8] "8.5 Criminal system is free of corruption"                         
##  [9] "8.6 Criminal system is free of improper government influence"      
## [10] "8.7. Due process of the law and rights of the accused"
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
dontselect=c("Country","Year","Factor 8: Criminal Justice")
select=setdiff(names(dataprincipal),dontselect) 
data=dataprincipal[,select]

# usaremos:
library(magrittr)
head(data,10)%>%
    rmarkdown::paged_table()
library(psych)
library(corpcor)
corMatrix=polycor::hetcor(data)$correlations
round(corMatrix,2)
##                                                                    8.1 Criminal investigation system is effective
## 8.1 Criminal investigation system is effective                                                               1.00
## 8.2 Criminal adjudication system is timely and effective                                                     0.82
## 8.3 Correctional system is effective in reducing criminal behavior                                           0.79
## 8.4 Criminal system is impartial                                                                             0.62
## 8.5 Criminal system is free of corruption                                                                    0.80
## 8.6 Criminal system is free of improper government influence                                                 0.65
## 8.7. Due process of the law and rights of the accused                                                        0.78
##                                                                    8.2 Criminal adjudication system is timely and effective
## 8.1 Criminal investigation system is effective                                                                         0.82
## 8.2 Criminal adjudication system is timely and effective                                                               1.00
## 8.3 Correctional system is effective in reducing criminal behavior                                                     0.84
## 8.4 Criminal system is impartial                                                                                       0.68
## 8.5 Criminal system is free of corruption                                                                              0.78
## 8.6 Criminal system is free of improper government influence                                                           0.66
## 8.7. Due process of the law and rights of the accused                                                                  0.79
##                                                                    8.3 Correctional system is effective in reducing criminal behavior
## 8.1 Criminal investigation system is effective                                                                                   0.79
## 8.2 Criminal adjudication system is timely and effective                                                                         0.84
## 8.3 Correctional system is effective in reducing criminal behavior                                                               1.00
## 8.4 Criminal system is impartial                                                                                                 0.72
## 8.5 Criminal system is free of corruption                                                                                        0.79
## 8.6 Criminal system is free of improper government influence                                                                     0.64
## 8.7. Due process of the law and rights of the accused                                                                            0.85
##                                                                    8.4 Criminal system is impartial
## 8.1 Criminal investigation system is effective                                                 0.62
## 8.2 Criminal adjudication system is timely and effective                                       0.68
## 8.3 Correctional system is effective in reducing criminal behavior                             0.72
## 8.4 Criminal system is impartial                                                               1.00
## 8.5 Criminal system is free of corruption                                                      0.71
## 8.6 Criminal system is free of improper government influence                                   0.69
## 8.7. Due process of the law and rights of the accused                                          0.80
##                                                                    8.5 Criminal system is free of corruption
## 8.1 Criminal investigation system is effective                                                          0.80
## 8.2 Criminal adjudication system is timely and effective                                                0.78
## 8.3 Correctional system is effective in reducing criminal behavior                                      0.79
## 8.4 Criminal system is impartial                                                                        0.71
## 8.5 Criminal system is free of corruption                                                               1.00
## 8.6 Criminal system is free of improper government influence                                            0.82
## 8.7. Due process of the law and rights of the accused                                                   0.87
##                                                                    8.6 Criminal system is free of improper government influence
## 8.1 Criminal investigation system is effective                                                                             0.65
## 8.2 Criminal adjudication system is timely and effective                                                                   0.66
## 8.3 Correctional system is effective in reducing criminal behavior                                                         0.64
## 8.4 Criminal system is impartial                                                                                           0.69
## 8.5 Criminal system is free of corruption                                                                                  0.82
## 8.6 Criminal system is free of improper government influence                                                               1.00
## 8.7. Due process of the law and rights of the accused                                                                      0.81
##                                                                    8.7. Due process of the law and rights of the accused
## 8.1 Criminal investigation system is effective                                                                      0.78
## 8.2 Criminal adjudication system is timely and effective                                                            0.79
## 8.3 Correctional system is effective in reducing criminal behavior                                                  0.85
## 8.4 Criminal system is impartial                                                                                    0.80
## 8.5 Criminal system is free of corruption                                                                           0.87
## 8.6 Criminal system is free of improper government influence                                                        0.81
## 8.7. Due process of the law and rights of the accused                                                               1.00
library(ggcorrplot)
## Warning: package 'ggcorrplot' was built under R version 4.3.3
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
ggcorrplot(corMatrix)

library(psych)
psych::KMO(corMatrix) 
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix)
## Overall MSA =  0.91
## MSA for each item = 
##                     8.1 Criminal investigation system is effective 
##                                                               0.92 
##           8.2 Criminal adjudication system is timely and effective 
##                                                               0.92 
## 8.3 Correctional system is effective in reducing criminal behavior 
##                                                               0.90 
##                                   8.4 Criminal system is impartial 
##                                                               0.95 
##                          8.5 Criminal system is free of corruption 
##                                                               0.91 
##       8.6 Criminal system is free of improper government influence 
##                                                               0.90 
##              8.7. Due process of the law and rights of the accused 
##                                                               0.89
correlation_matrix <- cor(data, use = "pairwise.complete.obs")

#valor KMO
kmo_resultados <- KMO(correlation_matrix)
kmo_resultados
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = correlation_matrix)
## Overall MSA =  0.91
## MSA for each item = 
##                     8.1 Criminal investigation system is effective 
##                                                               0.92 
##           8.2 Criminal adjudication system is timely and effective 
##                                                               0.92 
## 8.3 Correctional system is effective in reducing criminal behavior 
##                                                               0.90 
##                                   8.4 Criminal system is impartial 
##                                                               0.95 
##                          8.5 Criminal system is free of corruption 
##                                                               0.91 
##       8.6 Criminal system is free of improper government influence 
##                                                               0.90 
##              8.7. Due process of the law and rights of the accused 
##                                                               0.89
#BARRET

cortest.bartlett(corMatrix,n=nrow(data))$p.value>0.05
## [1] FALSE
library(matrixcalc)
## 
## Attaching package: 'matrixcalc'
## The following object is masked from 'package:corpcor':
## 
##     is.positive.definite
is.singular.matrix(corMatrix)
## [1] FALSE

variables latentes que puede dar el analisis

fa.parallel(data, fa = 'fa',correct = T,plot = F) #salen muchos errores
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Parallel analysis suggests that the number of factors =  1  and the number of components =  NA
efa_result <- fa(data, nfactors = 1, rotate = "varimax", fm = "ml")
print(efa_result)
## Factor Analysis using method =  ml
## Call: fa(r = data, nfactors = 1, rotate = "varimax", fm = "ml")
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                                                     ML1   h2
## 8.1 Criminal investigation system is effective                     0.85 0.73
## 8.2 Criminal adjudication system is timely and effective           0.86 0.75
## 8.3 Correctional system is effective in reducing criminal behavior 0.89 0.80
## 8.4 Criminal system is impartial                                   0.81 0.65
## 8.5 Criminal system is free of corruption                          0.92 0.84
## 8.6 Criminal system is free of improper government influence       0.82 0.67
## 8.7. Due process of the law and rights of the accused              0.95 0.90
##                                                                      u2 com
## 8.1 Criminal investigation system is effective                     0.27   1
## 8.2 Criminal adjudication system is timely and effective           0.25   1
## 8.3 Correctional system is effective in reducing criminal behavior 0.20   1
## 8.4 Criminal system is impartial                                   0.35   1
## 8.5 Criminal system is free of corruption                          0.16   1
## 8.6 Criminal system is free of improper government influence       0.33   1
## 8.7. Due process of the law and rights of the accused              0.10   1
## 
##                 ML1
## SS loadings    5.33
## Proportion Var 0.76
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## df null model =  21  with the objective function =  7.87 with Chi Square =  1068.41
## df of  the model are 14  and the objective function was  0.65 
## 
## The root mean square of the residuals (RMSR) is  0.04 
## The df corrected root mean square of the residuals is  0.05 
## 
## The harmonic n.obs is  140 with the empirical chi square  11.45  with prob <  0.65 
## The total n.obs was  140  with Likelihood Chi Square =  87.5  with prob <  1.1e-12 
## 
## Tucker Lewis Index of factoring reliability =  0.894
## RMSEA index =  0.194  and the 90 % confidence intervals are  0.156 0.234
## BIC =  18.32
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                    ML1
## Correlation of (regression) scores with factors   0.98
## Multiple R square of scores with factors          0.97
## Minimum correlation of possible factor scores     0.93
library(GPArotation)
## Warning: package 'GPArotation' was built under R version 4.3.3
## 
## Attaching package: 'GPArotation'
## The following objects are masked from 'package:psych':
## 
##     equamax, varimin
resfa <- fa(data,
            nfactors = 1,
            cor = 'mixed',
            rotate = "varimax", #oblimin?
            fm="minres")
print(resfa$loadings)
## 
## Loadings:
##                                                                    MR1  
## 8.1 Criminal investigation system is effective                     0.852
## 8.2 Criminal adjudication system is timely and effective           0.874
## 8.3 Correctional system is effective in reducing criminal behavior 0.892
## 8.4 Criminal system is impartial                                   0.797
## 8.5 Criminal system is free of corruption                          0.922
## 8.6 Criminal system is free of improper government influence       0.807
## 8.7. Due process of the law and rights of the accused              0.952
## 
##                  MR1
## SS loadings    5.328
## Proportion Var 0.761
fa.diagram(resfa,main = "Resultados del EFA")

#valores que aportaron más

sort(resfa$communality)
##                                   8.4 Criminal system is impartial 
##                                                          0.6350573 
##       8.6 Criminal system is free of improper government influence 
##                                                          0.6518719 
##                     8.1 Criminal investigation system is effective 
##                                                          0.7266429 
##           8.2 Criminal adjudication system is timely and effective 
##                                                          0.7638836 
## 8.3 Correctional system is effective in reducing criminal behavior 
##                                                          0.7953470 
##                          8.5 Criminal system is free of corruption 
##                                                          0.8494171 
##              8.7. Due process of the law and rights of the accused 
##                                                          0.9056341
valores adecuados para el analisis factorial
# matriz de correlación
correlation_matrix1 <- cor(data, use = "pairwise.complete.obs")

# Redondear la matriz de correlación a 2 decimales
rounded_correlation_matrix1 <- round(correlation_matrix1, 2)

#matriz redondeada
print(rounded_correlation_matrix1)
##                                                                    8.1 Criminal investigation system is effective
## 8.1 Criminal investigation system is effective                                                               1.00
## 8.2 Criminal adjudication system is timely and effective                                                     0.82
## 8.3 Correctional system is effective in reducing criminal behavior                                           0.79
## 8.4 Criminal system is impartial                                                                             0.62
## 8.5 Criminal system is free of corruption                                                                    0.80
## 8.6 Criminal system is free of improper government influence                                                 0.65
## 8.7. Due process of the law and rights of the accused                                                        0.78
##                                                                    8.2 Criminal adjudication system is timely and effective
## 8.1 Criminal investigation system is effective                                                                         0.82
## 8.2 Criminal adjudication system is timely and effective                                                               1.00
## 8.3 Correctional system is effective in reducing criminal behavior                                                     0.84
## 8.4 Criminal system is impartial                                                                                       0.68
## 8.5 Criminal system is free of corruption                                                                              0.78
## 8.6 Criminal system is free of improper government influence                                                           0.66
## 8.7. Due process of the law and rights of the accused                                                                  0.79
##                                                                    8.3 Correctional system is effective in reducing criminal behavior
## 8.1 Criminal investigation system is effective                                                                                   0.79
## 8.2 Criminal adjudication system is timely and effective                                                                         0.84
## 8.3 Correctional system is effective in reducing criminal behavior                                                               1.00
## 8.4 Criminal system is impartial                                                                                                 0.72
## 8.5 Criminal system is free of corruption                                                                                        0.79
## 8.6 Criminal system is free of improper government influence                                                                     0.64
## 8.7. Due process of the law and rights of the accused                                                                            0.85
##                                                                    8.4 Criminal system is impartial
## 8.1 Criminal investigation system is effective                                                 0.62
## 8.2 Criminal adjudication system is timely and effective                                       0.68
## 8.3 Correctional system is effective in reducing criminal behavior                             0.72
## 8.4 Criminal system is impartial                                                               1.00
## 8.5 Criminal system is free of corruption                                                      0.71
## 8.6 Criminal system is free of improper government influence                                   0.69
## 8.7. Due process of the law and rights of the accused                                          0.80
##                                                                    8.5 Criminal system is free of corruption
## 8.1 Criminal investigation system is effective                                                          0.80
## 8.2 Criminal adjudication system is timely and effective                                                0.78
## 8.3 Correctional system is effective in reducing criminal behavior                                      0.79
## 8.4 Criminal system is impartial                                                                        0.71
## 8.5 Criminal system is free of corruption                                                               1.00
## 8.6 Criminal system is free of improper government influence                                            0.82
## 8.7. Due process of the law and rights of the accused                                                   0.87
##                                                                    8.6 Criminal system is free of improper government influence
## 8.1 Criminal investigation system is effective                                                                             0.65
## 8.2 Criminal adjudication system is timely and effective                                                                   0.66
## 8.3 Correctional system is effective in reducing criminal behavior                                                         0.64
## 8.4 Criminal system is impartial                                                                                           0.69
## 8.5 Criminal system is free of corruption                                                                                  0.82
## 8.6 Criminal system is free of improper government influence                                                               1.00
## 8.7. Due process of the law and rights of the accused                                                                      0.81
##                                                                    8.7. Due process of the law and rights of the accused
## 8.1 Criminal investigation system is effective                                                                      0.78
## 8.2 Criminal adjudication system is timely and effective                                                            0.79
## 8.3 Correctional system is effective in reducing criminal behavior                                                  0.85
## 8.4 Criminal system is impartial                                                                                    0.80
## 8.5 Criminal system is free of corruption                                                                           0.87
## 8.6 Criminal system is free of improper government influence                                                        0.81
## 8.7. Due process of the law and rights of the accused                                                               1.00
#valor KMO
kmo_result <- KMO(correlation_matrix1)
print(kmo_result)
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = correlation_matrix1)
## Overall MSA =  0.91
## MSA for each item = 
##                     8.1 Criminal investigation system is effective 
##                                                               0.92 
##           8.2 Criminal adjudication system is timely and effective 
##                                                               0.92 
## 8.3 Correctional system is effective in reducing criminal behavior 
##                                                               0.90 
##                                   8.4 Criminal system is impartial 
##                                                               0.95 
##                          8.5 Criminal system is free of corruption 
##                                                               0.91 
##       8.6 Criminal system is free of improper government influence 
##                                                               0.90 
##              8.7. Due process of the law and rights of the accused 
##                                                               0.89
fa_parallel <- fa.parallel(data, fa = 'fa', correct = TRUE, plot = FALSE)
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Parallel analysis suggests that the number of factors =  1  and the number of components =  NA
n_factors <- fa_parallel$nfact

efa_result <- fa(data, nfactors = n_factors, rotate = "varimax", fm = "ml", scores = "regression")
print(efa_result)
## Factor Analysis using method =  ml
## Call: fa(r = data, nfactors = n_factors, rotate = "varimax", scores = "regression", 
##     fm = "ml")
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                                                     ML1   h2
## 8.1 Criminal investigation system is effective                     0.85 0.73
## 8.2 Criminal adjudication system is timely and effective           0.86 0.75
## 8.3 Correctional system is effective in reducing criminal behavior 0.89 0.80
## 8.4 Criminal system is impartial                                   0.81 0.65
## 8.5 Criminal system is free of corruption                          0.92 0.84
## 8.6 Criminal system is free of improper government influence       0.82 0.67
## 8.7. Due process of the law and rights of the accused              0.95 0.90
##                                                                      u2 com
## 8.1 Criminal investigation system is effective                     0.27   1
## 8.2 Criminal adjudication system is timely and effective           0.25   1
## 8.3 Correctional system is effective in reducing criminal behavior 0.20   1
## 8.4 Criminal system is impartial                                   0.35   1
## 8.5 Criminal system is free of corruption                          0.16   1
## 8.6 Criminal system is free of improper government influence       0.33   1
## 8.7. Due process of the law and rights of the accused              0.10   1
## 
##                 ML1
## SS loadings    5.33
## Proportion Var 0.76
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## df null model =  21  with the objective function =  7.87 with Chi Square =  1068.41
## df of  the model are 14  and the objective function was  0.65 
## 
## The root mean square of the residuals (RMSR) is  0.04 
## The df corrected root mean square of the residuals is  0.05 
## 
## The harmonic n.obs is  140 with the empirical chi square  11.45  with prob <  0.65 
## The total n.obs was  140  with Likelihood Chi Square =  87.5  with prob <  1.1e-12 
## 
## Tucker Lewis Index of factoring reliability =  0.894
## RMSEA index =  0.194  and the 90 % confidence intervals are  0.156 0.234
## BIC =  18.32
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                    ML1
## Correlation of (regression) scores with factors   0.98
## Multiple R square of scores with factors          0.97
## Minimum correlation of possible factor scores     0.93

CONCLUSIÓN: El unico factor sí explica un 76% de la varianza observada por lo que sale en el proportion var.

REGRESIÓN LOGÍSTICA

baselog=import("Ceplan.xlsx - Hoja 1 (1).csv")
Ceplan = read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vSV8h6jh5O6qlzs2LADiOCgrNEFqaMbUhjenWitP2iRn89Qt09mOLTKOgZq3DEq5Q/pub?output=csv")

library(dplyr)
Ceplan2 = Ceplan%>%filter(TIPO == "Provincia")%>%dplyr::select(c(4:8))

Ceplan2$IVIA = gsub(",", ".", Ceplan2$IVIA)
Ceplan2$IDE = gsub(",", ".", Ceplan2$IDE)
Ceplan2$ABASTOS = gsub(",", ".", Ceplan2$ABASTOS)
Ceplan2$IDH = gsub(",", ".", Ceplan2$IDH)
Ceplan2$POBREZA = gsub(",", ".", Ceplan2$POBREZA)

Ceplan2 = as.data.frame(lapply(Ceplan2, as.numeric))
## Warning in lapply(Ceplan2, as.numeric): NAs introducidos por coerción
Ceplan2 = Ceplan2[complete.cases(Ceplan2),]
Ceplan2$IDH_cat = ifelse(Ceplan2$IDH < 0.430, 0, 1)
Ceplan2$IDH_cat = factor(Ceplan2$IDH_cat, levels = c(0,1), labels = c("No desarrollo humano", "Desarrollo humano"))

HIPOTESIS: El tener desarrollo humano (IDH_CAT) está determinado por el número de mercados de abastos (ABASTOS), la vulnerabilidad alimentaria (IVIA), el Índice de Densidad del Estado (IDE) y el porcentaje de la población en pobreza total (POBREZA)

library(dplyr)
library(psych)
library(corpcor)
library(car)
## Warning: package 'car' was built under R version 4.3.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
## The following object is masked from 'package:dplyr':
## 
##     recode
# Ajustar el modelo de regresión logística
logistic_model <- glm(IDH_cat ~ IVIA + IDE + ABASTOS + POBREZA, data = Ceplan2, family = binomial)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
# Resumen del modelo
summary(logistic_model)
## 
## Call:
## glm(formula = IDH_cat ~ IVIA + IDE + ABASTOS + POBREZA, family = binomial, 
##     data = Ceplan2)
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  28.55107    7.77533   3.672 0.000241 ***
## IVIA        -42.67810   10.24112  -4.167 3.08e-05 ***
## IDE          -7.28485    5.63664  -1.292 0.196215    
## ABASTOS       0.31870    0.17667   1.804 0.071252 .  
## POBREZA      -0.06472    0.05893  -1.098 0.272082    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 253.643  on 182  degrees of freedom
## Residual deviance:  46.614  on 178  degrees of freedom
## AIC: 56.614
## 
## Number of Fisher Scoring iterations: 9
# Calcular pseudo-R² (Nagelkerke R²)
nagelkerke_r2 <- function(logistic_model) {
  ll.null <- logistic_model$null.deviance / -2
  ll.proposed <- logistic_model$deviance / -2
  r2_mcfadden <- 1 - (logistic_model$deviance / logistic_model$null.deviance)
  r2_nagelkerke <- r2_mcfadden / (1 - (exp(2 * ll.null / length(logistic_model$fitted.values))))
  return(r2_nagelkerke)
}

nagelkerke_r2_value <- nagelkerke_r2(logistic_model)
print(paste("Nagelkerke R²:", round(nagelkerke_r2_value, 2)))
## [1] "Nagelkerke R²: 1.09"
# Evaluar multicolinealidad con VIF
vif_values <- vif(logistic_model)
print(vif_values)
##     IVIA      IDE  ABASTOS  POBREZA 
## 1.298690 1.262854 1.116541 1.079062
### first hypothesis
hipolog=formula(IDH_cat ~ IVIA + IDE + ABASTOS + POBREZA)

#regression
rlog1=glm(hipolog, data=Ceplan2,family = binomial)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
modelrl=list('IDHdeterm'=rlog1)