library(rio)
mortalidad = import("https://docs.google.com/spreadsheets/d/14atPTrwM6x5j49Ui7dKApHOqR1rNT2y5U4PeX29GRbc/edit#gid=1122338962")
head(mortalidad)
##      sex padreSector fechaNacimiento edadDejaEstudio muere naceFueraMatri
## 1   male Agricultura      1853-05-23          15.000     0             no
## 2   male Agricultura      1853-07-19          15.000     0             no
## 3   male      NoFijo      1861-11-17          15.000     0             no
## 4   male Agricultura      1872-11-16          15.000     0             no
## 5 female      NoFijo      1855-07-19           0.559     1             no
## 6   male Agricultura      1855-09-29           0.315     1             no
##   madreEdad
## 1    35.009
## 2    30.609
## 3    29.320
## 4    41.183
## 5    42.138
## 6    32.931
dataAdmi = import("https://docs.google.com/spreadsheets/d/1aq7pz3W7QOjmMkmz2T5-qEOAX8y1t99F2bdDIfw94OA/edit#gid=59881833")
head(dataAdmi)
##   admitido gre  gpa prestigio
## 1       no 380 3.61      Bajo
## 2       si 660 3.67      Bajo
## 3       si 800 4.00   MuyAlto
## 4       si 640 3.19   MuyBajo
## 5       no 520 2.93   MuyBajo
## 6       si 760 3.00      Alto
library(rio)
GPI <- import("GPI.xlsx")
head(GPI)
##       Country iso3c Safety Milita Conflicto
## 1 Afghanistan   AFG  4.127  2.472     3.650
## 2     Albania   ALB  2.120  1.666     1.403
## 3     Algeria   DZA  2.302  2.041     2.068
## 4      Angola   AGO  2.413  1.706     1.666
## 5   Argentina   ARG  2.656  1.611     1.201
## 6     Armenia   ARM  1.977  2.041     1.990
library(rio)
et <- import("Ecological threat.xlsx")
## New names:
## * `` -> `...6`
## * `` -> `...7`
## * `` -> `...8`
## * `` -> `...9`
## * `` -> `...10`
## * `` -> `...11`
## * `` -> `...12`
## * `` -> `...13`
## * `` -> `...14`
## * `` -> `...15`
## * `` -> `...16`
## * `` -> `...17`
## * `` -> `...18`
## * `` -> `...19`
## * `` -> `...20`
## * `` -> `...21`
## * `` -> `...22`
## * `` -> `...23`
## * `` -> `...24`
## * `` -> `...25`
head(et)
##                    Country Food Risk Score Natural Disasters Score
## 1                  Burundi               5                       5
## 2 Central African Republic               5                       5
## 3    Republic of the Congo               5                       5
## 4                    Kenya               5                       5
## 5               Mozambique               5                       5
## 6                   Malawi               5                       5
##   Rapid Population Growth Score Water Risk Score ...6 ...7 ...8 ...9 ...10
## 1                             5                5   NA   NA   NA   NA    NA
## 2                             5                5   NA   NA   NA   NA    NA
## 3                             5                5   NA   NA   NA   NA    NA
## 4                             5                5   NA   NA   NA   NA    NA
## 5                             5                5   NA   NA   NA   NA    NA
## 6                             5                5   NA   NA   NA   NA    NA
##   ...11 ...12 ...13 ...14 ...15 ...16 ...17 ...18 ...19 ...20 ...21 ...22 ...23
## 1    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
## 2    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
## 3    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
## 4    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
## 5    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
## 6    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
##   ...24 ...25
## 1    NA    NA
## 2    NA    NA
## 3    NA    NA
## 4    NA    NA
## 5    NA    NA
## 6    NA    NA
control3=merge(et,GPI)
str(control3)
## 'data.frame':    163 obs. of  29 variables:
##  $ Country                      : chr  "Afghanistan" "Albania" "Algeria" "Angola" ...
##  $ Food Risk Score              : num  4 2 2 5 2 2 1 1 2 2 ...
##  $ Natural Disasters Score      : num  5 4 5 3 4 2 4 3 1 1 ...
##  $ Rapid Population Growth Score: num  5 1 3 5 2 1 3 1 1 3 ...
##  $ Water Risk Score             : num  5 3 3 5 3 4 3 1 5 3 ...
##  $ ...6                         : logi  NA NA NA NA NA NA ...
##  $ ...7                         : logi  NA NA NA NA NA NA ...
##  $ ...8                         : logi  NA NA NA NA NA NA ...
##  $ ...9                         : logi  NA NA NA NA NA NA ...
##  $ ...10                        : logi  NA NA NA NA NA NA ...
##  $ ...11                        : logi  NA NA NA NA NA NA ...
##  $ ...12                        : logi  NA NA NA NA NA NA ...
##  $ ...13                        : logi  NA NA NA NA NA NA ...
##  $ ...14                        : logi  NA NA NA NA NA NA ...
##  $ ...15                        : logi  NA NA NA NA NA NA ...
##  $ ...16                        : logi  NA NA NA NA NA NA ...
##  $ ...17                        : logi  NA NA NA NA NA NA ...
##  $ ...18                        : logi  NA NA NA NA NA NA ...
##  $ ...19                        : logi  NA NA NA NA NA NA ...
##  $ ...20                        : logi  NA NA NA NA NA NA ...
##  $ ...21                        : logi  NA NA NA NA NA NA ...
##  $ ...22                        : logi  NA NA NA NA NA NA ...
##  $ ...23                        : logi  NA NA NA NA NA NA ...
##  $ ...24                        : logi  NA NA NA NA NA NA ...
##  $ ...25                        : logi  NA NA NA NA NA NA ...
##  $ iso3c                        : chr  "AFG" "ALB" "DZA" "AGO" ...
##  $ Safety                       : num  4.13 2.12 2.3 2.41 2.66 ...
##  $ Milita                       : num  2.47 1.67 2.04 1.71 1.61 ...
##  $ Conflicto                    : num  3.65 1.4 2.07 1.67 1.2 ...
control3= control3[,-c(6:26)]
str(control3)
## 'data.frame':    163 obs. of  8 variables:
##  $ Country                      : chr  "Afghanistan" "Albania" "Algeria" "Angola" ...
##  $ Food Risk Score              : num  4 2 2 5 2 2 1 1 2 2 ...
##  $ Natural Disasters Score      : num  5 4 5 3 4 2 4 3 1 1 ...
##  $ Rapid Population Growth Score: num  5 1 3 5 2 1 3 1 1 3 ...
##  $ Water Risk Score             : num  5 3 3 5 3 4 3 1 5 3 ...
##  $ Safety                       : num  4.13 2.12 2.3 2.41 2.66 ...
##  $ Milita                       : num  2.47 1.67 2.04 1.71 1.61 ...
##  $ Conflicto                    : num  3.65 1.4 2.07 1.67 1.2 ...
dontselect=c("Country")
select=setdiff(names(control3),dontselect) 
theData=control3[,select]
library(polycor)
library(ggcorrplot)
## Loading required package: ggplot2
corMatrix=polycor::hetcor(theData)$correlations
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
psych::KMO(corMatrix)
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix)
## Overall MSA =  0.76
## MSA for each item = 
##               Food Risk Score       Natural Disasters Score 
##                          0.79                          0.87 
## Rapid Population Growth Score              Water Risk Score 
##                          0.76                          0.79 
##                        Safety                        Milita 
##                          0.73                          0.69 
##                     Conflicto 
##                          0.71
library(matrixcalc)
cortest.bartlett(corMatrix,n=nrow(theData))$p.value>0.05
## [1] FALSE
is.singular.matrix(corMatrix)
## [1] FALSE
fa.parallel(theData, 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.
## Parallel analysis suggests that the number of factors =  2  and the number of components =  NA
library(GPArotation)
resfa <- fa(theData,
            nfactors = 3,
            cor = 'mixed',
            rotate = "varimax",
            fm="minres")
## 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   
## Food Risk Score                0.690  0.577  0.129
## Natural Disasters Score        0.116  0.431       
## Rapid Population Growth Score  0.943  0.204  0.266
## Water Risk Score               0.668  0.720       
## Safety                         0.308  0.735  0.502
## Milita                                       0.586
## Conflicto                      0.210  0.402  0.749
## 
##                  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
print(resfa$loadings,cutoff = 0.5)
## 
## Loadings:
##                               MR3    MR1    MR2   
## Food Risk Score                0.690  0.577       
## Natural Disasters Score                           
## Rapid Population Growth Score  0.943              
## Water Risk Score               0.668  0.720       
## Safety                                0.735  0.502
## Milita                                       0.586
## Conflicto                                    0.749
## 
##                  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
fa.diagram(resfa,main = "Resultados del EFA")