head(eco)
##                       Pais Comida Desastre Poblacion Agua
## 1                  Burundi      5        5         5    5
## 2 Central African Republic      5        5         5    5
## 3    Republic of the Congo      5        5         5    5
## 4                    Kenya      5        5         5    5
## 5               Mozambique      5        5         5    5
## 6                   Malawi      5        5         5    5
head(paz)
##          Pais Seguridad Militar Conflictos
## 1 Afghanistan     4.127   2.472      3.650
## 2     Albania     2.120   1.666      1.403
## 3     Algeria     2.302   2.041      2.068
## 4      Angola     2.413   1.706      1.666
## 5   Argentina     2.656   1.611      1.201
## 6     Armenia     1.977   2.041      1.990
archivo=merge(paz,eco)
archivo=archivo[,-c(1)]
#Calculo de matriz de correlación:
library(polycor) 
corMatrix=polycor::hetcor(archivo)$correlations

library(ggcorrplot)
## Loading required package: ggplot2
ggcorrplot(corMatrix)

library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## The following object is masked from 'package:polycor':
## 
##     polyserial
## The following objects are masked from 'package:DescTools':
## 
##     AUC, ICC, SD
psych::KMO(corMatrix) #KMO > 0.6 --> Overall MSA (lo contrario sugiere cambiar esas variables)
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix)
## Overall MSA =  0.76
## MSA for each item = 
##  Seguridad    Militar Conflictos     Comida   Desastre  Poblacion       Agua 
##       0.73       0.69       0.71       0.79       0.87       0.76       0.79
#Busca saber si la data se adecua a un análisis factorial. El Overall MSA en este caso es 0.89, es aceptable. el tamano de los datos es suficiente para comenzar con el EFA,  respecto a la data
cortest.bartlett(corMatrix,n=nrow(archivo))$p.value>0.05
## [1] FALSE
library(matrixcalc)

is.singular.matrix(corMatrix)
## [1] FALSE
fa.parallel(archivo, fa = 'fa',correct = T,plot = F)
## 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 =  3  and the number of components =  NA
library(GPArotation)
resfa <- fa(archivo,
            nfactors = 3, #dos factores
            cor = 'mixed', #Matriz policórica
            rotate = "varimax", #Rotación varimax. Asegura que se traten de dos variables latentes diferentes #en el caso que se use 1 factor, no es relevante
            fm="minres") #tecnica de factorizacion
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected. Examine the results carefully
print(resfa$loadings)
## 
## Loadings:
##            MR3    MR1    MR2   
## Seguridad   0.308  0.735  0.502
## Militar                   0.586
## Conflictos  0.210  0.402  0.749
## Comida      0.690  0.577  0.129
## Desastre    0.116  0.431       
## Poblacion   0.943  0.204  0.266
## Agua        0.668  0.720       
## 
##                  MR3   MR1   MR2
## SS loadings    1.965 1.780 1.255
## Proportion Var 0.281 0.254 0.179
## Cumulative Var 0.281 0.535 0.714
sort(resfa$communality) #sort lo ordena en orden ascendente #communality: podemos advertir su fuerza. para identificar cuales son las variables que han contruibuido mas
##   Desastre    Militar Conflictos     Comida  Seguridad       Agua  Poblacion 
##  0.2075146  0.3454628  0.7669409  0.8262744  0.8867507  0.9664009  1.0011528
sort(resfa$complexity) #Se espera que este cerca a 1. advierte su comportamiento cualitativo. en este caso, indica que la cultura politica (1.983764) se relaciona mas con otras variables 
##    Militar   Desastre  Poblacion Conflictos       Agua     Comida  Seguridad 
##   1.013845   1.242071   1.258197   1.715919   1.998442   2.017992   2.160659