library(rio)
p3=import("p1.xlsx")
## New names:
## • `` -> `...4`
p3=p3[-1,]
p3=na.omit(p3)
names2=c("safe","mili ","co ","Country")
names(p3)=names2
P3.1=import("ETR.xlsx")
## New names:
## • `` -> `...1`
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
## • `` -> `...9`
## • `` -> `...10`
## • `` -> `...11`
## • `` -> `...12`
## • `` -> `...13`
## • `` -> `...14`
## • `` -> `...15`
## • `` -> `...16`
## • `` -> `...17`
## • `` -> `...18`
## • `` -> `...19`
## • `` -> `...20`
## • `` -> `...21`
## • `` -> `...22`
## • `` -> `...23`
## • `` -> `...24`
## • `` -> `...25`
P3.1[,c(6:25)]=NULL
names1=c("Country","Food ","Natural ","Rapid ","Water ")
names(P3.1)=names1
P3.1=P3.1[-1,]
setdiff(p3$Country,P3.1$Country)
## character(0)
merge(p3,P3.1)
##                              Country  safe mili    co  Food  Natural  Rapid 
## 1                        Afghanistan 4.127 2.472 3.650   4.0      5.0    5.0
## 2                            Albania 2.120 1.666 1.403   2.0      4.0    1.0
## 3                            Algeria 2.302 2.041 2.068   2.0      5.0    3.0
## 4                             Angola 2.413 1.706 1.666   5.0      3.0    5.0
## 5                          Argentina 2.656 1.611 1.201   2.0      4.0    2.0
## 6                            Armenia 1.977 2.041 1.990   2.0      2.0    1.0
## 7                          Australia 1.657 1.848 1.266   1.0      4.0    3.0
## 8                            Austria 1.452 1.331 1.097   1.0      3.0    1.0
## 9                         Azerbaijan 2.528 2.108 2.579   2.0      1.0    1.0
## 10                           Bahrain 2.405 1.717 1.955   2.0      1.0    3.0
## 11                        Bangladesh 2.438 1.534 2.004   4.0      4.0    2.0
## 12                           Belarus 2.526 1.896 2.208   2.0      1.0    1.0
## 13                           Belgium 1.806 1.602 1.141   1.0      2.0    1.0
## 14                             Benin 2.574 2.022 1.666   5.0      4.0    5.0
## 15                            Bhutan 1.601 1.336 1.436   2.0      5.0    3.0
## 16                           Bolivia 2.503 1.934 1.403   3.0      4.0    3.0
## 17            Bosnia and Herzegovina 2.044 1.526 1.835   2.0      2.0    1.0
## 18                          Botswana 2.441 1.812 1.000   4.0      5.0    3.0
## 19                            Brazil 3.284 1.965 1.853   2.0      4.0    1.0
## 20                          Bulgaria 1.929 1.633 1.000   2.0      4.0    1.0
## 21                      Burkina Faso 2.993 2.189 3.023   4.0      3.0    5.0
## 22                           Burundi 3.024 1.556 2.443   5.0      5.0    5.0
## 23                          Cambodia 2.080 1.933 1.638   4.0      5.0    3.0
## 24                          Cameroon 3.014 1.877 2.967   5.0      4.0    5.0
## 25                            Canada 1.637 1.465 1.030   1.0      4.0    2.0
## 26          Central African Republic 3.579 2.273 2.874   5.0      5.0    5.0
## 27                              Chad 2.903 2.189 2.552   5.0      5.0    5.0
## 28                             Chile 2.361 1.585 1.403   1.0      4.0    1.0
## 29                             China 2.107 2.022 1.858   1.0      4.0    2.0
## 30                          Colombia 3.596 2.052 2.099   3.0      4.0    1.0
## 31                        Costa Rica 2.229 1.614 1.201   2.0      4.0    1.0
## 32                    Cote d' Ivoire 2.764 1.724 1.694   4.0      3.0    5.0
## 33                           Croatia 1.564 1.565 1.201   1.0      5.0    1.0
## 34                              Cuba 2.614 1.533 1.805   3.0      5.0    1.0
## 35                            Cyprus 2.251 1.605 1.604   1.0      2.0    1.0
## 36                    Czech Republic 1.566 1.263 1.034   1.0      3.0    1.0
## 37  Democratic Republic of the Congo 3.747 1.881 3.413   5.0      4.0    5.0
## 38                           Denmark 1.295 1.368 1.242   1.0      1.0    1.0
## 39                          Djibouti 2.561 2.140 1.856   3.0      5.0    3.0
## 40                Dominican Republic 2.614 1.720 1.403   3.0      5.0    2.0
## 41                           Ecuador 2.509 1.866 1.426   4.0      5.0    3.0
## 42                             Egypt 2.503 2.014 2.426   3.0      5.0    4.0
## 43                       El Salvador 3.062 1.879 1.436   4.0      5.0    1.0
## 44                 Equatorial Guinea 2.200 1.638 1.604   5.0      1.0    5.0
## 45                           Eritrea 3.359 1.914 1.705   5.0      1.0    5.0
## 46                           Estonia 1.703 1.607 1.638   1.0      2.0    1.0
## 47                          Eswatini 2.576 1.706 1.604   5.0      2.0    3.0
## 48                          Ethiopia 2.917 1.809 3.457   4.0      4.0    5.0
## 49                           Finland 1.349 1.598 1.436   1.0      1.0    1.0
## 50                            France 1.867 2.773 1.305   1.0      2.0    1.0
## 51                             Gabon 2.378 1.807 1.604   5.0      1.0    4.0
## 52                           Georgia 2.413 1.790 1.805   2.0      5.0    1.0
## 53                           Germany 1.619 1.750 1.062   1.0      2.0    1.0
## 54                             Ghana 2.026 1.726 1.472   4.0      3.0    5.0
## 55                            Greece 1.773 2.036 1.805   1.0      4.0    1.0
## 56                         Guatemala 2.819 1.698 1.626   4.0      5.0    4.0
## 57                            Guinea 2.587 2.069 2.262   5.0      3.0    5.0
## 58                     Guinea-Bissau 2.586 2.218 1.615   5.0      4.0    5.0
## 59                            Guyana 2.895 1.551 1.604   2.0      5.0    1.0
## 60                             Haiti 2.735 1.740 2.077   5.0      5.0    3.0
## 61                          Honduras 3.064 1.812 1.604   4.0      4.0    3.0
## 62                           Hungary 1.528 1.184 1.421   1.0      2.0    1.0
## 63                           Iceland 1.237 1.020 1.000   1.0      1.0    1.0
## 64                             India 2.373 2.461 2.982   3.0      4.0    2.0
## 65                         Indonesia 2.107 1.496 1.644   2.0      5.0    2.0
## 66                              Iran 2.838 2.123 2.923   3.0      5.0    1.0
## 67                              Iraq 3.646 2.298 3.207   3.0      5.0    5.0
## 68                           Ireland 1.525 1.259 1.015   1.0      3.0    2.0
## 69                            Israel 2.237 3.813 2.167   1.0      3.0    4.0
## 70                             Italy 1.927 2.013 1.034   1.0      3.0    1.0
## 71                           Jamaica 2.791 1.721 1.201   3.0      3.0    1.0
## 72                             Japan 1.283 1.308 1.403   1.0      5.0    1.0
## 73                            Jordan 2.037 1.964 1.542   3.0      3.0    3.0
## 74                        Kazakhstan 2.492 1.781 1.805   2.0      4.0    2.0
## 75                             Kenya 2.599 1.794 2.345   5.0      5.0    5.0
## 76                            Kosovo 2.266 1.575 1.805   2.0      1.0    1.0
## 77                            Kuwait 1.762 2.184 1.431   2.0      1.0    3.0
## 78                   Kyrgyz Republic 2.282 1.671 2.006   2.0      4.0    3.0
## 79                              Laos 2.076 1.949 1.403   4.0      5.0    3.0
## 80                            Latvia 1.798 1.479 1.634   1.0      2.0    1.0
## 81                           Lebanon 2.748 2.317 2.701   3.0      4.0    1.0
## 82                           Lesotho 2.528 1.748 1.805   5.0      4.0    3.0
## 83                           Liberia 2.491 1.804 1.462   5.0      1.0    5.0
## 84                             Libya 3.093 2.433 3.160   3.0      5.0    5.0
## 85                         Lithuania 1.775 1.735 1.638   1.0      1.0    1.0
## 86                        Madagascar 2.612 1.444 1.630   5.0      3.0    5.0
## 87                            Malawi 2.144 1.723 1.739   5.0      5.0    5.0
## 88                          Malaysia 1.973 1.205 1.015   2.0      4.0    3.0
## 89                              Mali 3.589 1.873 2.861   4.0      3.0    5.0
## 90                        Mauritania 2.782 1.969 1.638   4.0      4.0    5.0
## 91                         Mauritius 2.132 1.373 1.000   3.0      4.0    1.0
## 92                            Mexico 3.158 1.736 2.642   2.0      4.0    2.0
## 93                           Moldova 2.142 1.243 2.006   2.0      3.0    1.0
## 94                          Mongolia 2.459 1.348 1.201   2.0      5.0    3.0
## 95                        Montenegro 2.243 1.602 1.403   2.0      2.0    1.0
## 96                           Morocco 2.158 1.752 1.891   3.0      4.0    3.0
## 97                        Mozambique 2.803 1.677 2.215   5.0      5.0    5.0
## 98                           Myanmar 3.116 1.744 2.700   4.0      5.0    2.0
## 99                           Namibia 2.569 1.755 1.201   5.0      5.0    4.0
## 100                            Nepal 2.493 1.706 1.436   4.0      5.0    2.0
## 101                      Netherlands 1.525 2.206 1.045   1.0      2.0    1.0
## 102                      New Zealand 1.530 1.168 1.008   1.0      4.0    2.0
## 103                        Nicaragua 2.966 1.723 2.006   4.0      4.0    2.0
## 104                            Niger 2.957 1.957 2.839   5.0      5.0    5.0
## 105                          Nigeria 3.107 2.059 2.806   5.0      4.0    5.0
## 106                      North Korea 3.113 3.120 2.610   2.0      4.0    1.0
## 107                  North Macedonia 1.928 1.771 1.403   2.0      5.0    1.0
## 108                           Norway 1.266 2.184 1.235   1.0      4.0    2.0
## 109                             Oman 1.892 2.651 1.403   2.0      4.0    3.0
## 110                         Pakistan 2.653 2.581 3.172   3.0      5.0    4.0
## 111                        Palestine 2.950 1.991 2.612   3.0      1.0    5.0
## 112                           Panama 2.524 1.547 1.403   2.0      3.0    3.0
## 113                 Papua New Guinea 2.802 1.697 1.605   2.0      5.0    4.0
## 114                         Paraguay 2.534 2.004 1.403   3.0      4.0    2.0
## 115                             Peru 2.625 1.938 1.420   3.0      5.0    3.0
## 116                      Philippines 2.855 1.700 2.410   4.0      5.0    3.0
## 117                           Poland 1.933 1.527 1.403   1.0      3.0    1.0
## 118                         Portugal 1.390 1.312 1.007   1.0      4.0    1.0
## 119                            Qatar 1.495 2.196 1.246   2.0      1.0    3.0
## 120            Republic of the Congo 2.903 2.194 1.620   5.0      5.0    5.0
## 121                          Romania 1.837 1.773 1.007   2.0      5.0    1.0
## 122                           Russia 2.912 3.234 2.844   2.0      5.0    1.0
## 123                           Rwanda 2.374 1.512 1.858   5.0      3.0    5.0
## 124                     Saudi Arabia 2.254 2.614 2.425   2.0      3.0    3.0
## 125                          Senegal 2.337 1.701 1.418   4.0      4.0    5.0
## 126                           Serbia 1.976 1.796 1.604   2.0      4.0    1.0
## 127                     Sierra Leone 2.313 1.605 1.413   5.0      4.0    4.0
## 128                        Singapore 1.238 1.943 1.000   1.0      1.0    1.0
## 129                         Slovakia 1.835 1.274 1.403   2.0      2.0    1.0
## 130                         Slovenia 1.407 1.129 1.403   1.0      5.0    1.0
## 131                          Somalia 3.624 2.155 3.474   5.0      5.0    5.0
## 132                     South Africa 3.234 1.758 1.613   3.0      2.0    3.0
## 133                      South Korea 1.591 2.370 1.805   2.0      4.0    1.0
## 134                      South Sudan 3.957 2.594 3.267   5.0      5.0    5.0
## 135                            Spain 1.808 1.898 1.218   1.0      3.0    1.0
## 136                        Sri Lanka 2.377 2.047 1.855   4.0      5.0    1.0
## 137                            Sudan 3.303 2.312 3.040   4.0      4.0    5.0
## 138                           Sweden 1.475 1.802 1.221   1.0      4.0    1.0
## 139                      Switzerland 1.236 1.929 1.005   1.0      4.0    1.0
## 140                            Syria 3.741 2.287 3.828   5.0      5.0    5.0
## 141                           Taiwan 1.731 1.704 1.604   2.0      3.0    1.0
## 142                       Tajikistan 2.428 1.707 2.057   4.0      3.0    5.0
## 143                         Tanzania 2.353 1.687 1.479   4.0      4.0    5.0
## 144                         Thailand 2.863 1.562 1.916   2.0      5.0    1.0
## 145                       The Gambia 2.373 1.830 1.409   4.0      5.0    5.0
## 146                      Timor-Leste 2.369 1.667 1.403   2.0      5.0    5.0
## 147                             Togo 2.615 2.020 1.818   4.0      3.0    5.0
## 148              Trinidad and Tobago 2.736 2.098 1.201   3.0      3.0    1.0
## 149                          Tunisia 2.508 1.769 1.820   2.0      4.0    2.0
## 150                           Turkey 3.217 2.058 3.159   3.0      5.0    1.0
## 151                     Turkmenistan 2.501 2.383 1.604   1.0      1.0    3.0
## 152                           Uganda 2.762 1.760 1.765   5.0      5.0    5.0
## 153                          Ukraine 3.230 2.044 2.765   2.0      4.0    1.0
## 154             United Arab Emirates 1.712 2.652 1.464   2.0      2.0    3.0
## 155                   United Kingdom 1.729 2.489 1.042   1.0      1.0    1.0
## 156         United States of America 2.225 3.172 1.770   1.0      4.0    2.0
## 157                          Uruguay 2.323 1.582 1.000   2.0      5.0    1.0
## 158                       Uzbekistan 2.215 2.213 1.805   3.0      2.0    3.0
## 159                        Venezuela 4.007 2.002 2.103   5.0      4.0    3.0
## 160                          Vietnam 2.289 1.876 1.403   2.0      5.0    2.0
## 161                            Yemen 3.771 2.272 3.559   5.0      5.0    5.0
## 162                           Zambia 2.511 1.519 1.604   5.0      3.0    5.0
## 163                         Zimbabwe 2.990 2.044 2.112   5.0      4.0    5.0
##     Water 
## 1      5.0
## 2      3.0
## 3      3.0
## 4      5.0
## 5      3.0
## 6      4.0
## 7      3.0
## 8      1.0
## 9      5.0
## 10     3.0
## 11     2.0
## 12     2.0
## 13     1.0
## 14     5.0
## 15     4.0
## 16     5.0
## 17     1.0
## 18     5.0
## 19     5.0
## 20     1.0
## 21     5.0
## 22     5.0
## 23     5.0
## 24     5.0
## 25     2.0
## 26     5.0
## 27     5.0
## 28     3.0
## 29     3.0
## 30     5.0
## 31     4.0
## 32     5.0
## 33     1.0
## 34     4.0
## 35     1.0
## 36     2.0
## 37     5.0
## 38     1.0
## 39     5.0
## 40     5.0
## 41     5.0
## 42     2.0
## 43     4.0
## 44     5.0
## 45     5.0
## 46     2.0
## 47     5.0
## 48     5.0
## 49     2.0
## 50     1.0
## 51     5.0
## 52     4.0
## 53     1.0
## 54     5.0
## 55     2.0
## 56     4.0
## 57     5.0
## 58     5.0
## 59     4.0
## 60     5.0
## 61     5.0
## 62     1.0
## 63     1.0
## 64     4.0
## 65     5.0
## 66     4.0
## 67     5.0
## 68     1.0
## 69     2.0
## 70     1.0
## 71     4.0
## 72     1.0
## 73     3.0
## 74     4.0
## 75     5.0
## 76     1.0
## 77     2.0
## 78     4.0
## 79     5.0
## 80     2.0
## 81     1.0
## 82     5.0
## 83     5.0
## 84     3.0
## 85     4.0
## 86     5.0
## 87     5.0
## 88     4.0
## 89     5.0
## 90     5.0
## 91     5.0
## 92     3.0
## 93     5.0
## 94     5.0
## 95     1.0
## 96     5.0
## 97     5.0
## 98     5.0
## 99     5.0
## 100    5.0
## 101    1.0
## 102    3.0
## 103    5.0
## 104    5.0
## 105    5.0
## 106    2.0
## 107    2.0
## 108    1.0
## 109    1.0
## 110    4.0
## 111    3.0
## 112    5.0
## 113    5.0
## 114    4.0
## 115    5.0
## 116    5.0
## 117    3.0
## 118    1.0
## 119    1.0
## 120    5.0
## 121    3.0
## 122    4.0
## 123    5.0
## 124    3.0
## 125    5.0
## 126    1.0
## 127    5.0
## 128    1.0
## 129    2.0
## 130    1.0
## 131    5.0
## 132    5.0
## 133    1.0
## 134    5.0
## 135    1.0
## 136    4.0
## 137    5.0
## 138    1.0
## 139    1.0
## 140    2.0
## 141    3.0
## 142    5.0
## 143    5.0
## 144    4.0
## 145    5.0
## 146    5.0
## 147    5.0
## 148    4.0
## 149    3.0
## 150    3.0
## 151    2.0
## 152    5.0
## 153    3.0
## 154    3.0
## 155    1.0
## 156    3.0
## 157    3.0
## 158    3.0
## 159    5.0
## 160    5.0
## 161    5.0
## 162    5.0
## 163    5.0
DATA=merge(p3,P3.1)
str(DATA)
## 'data.frame':    163 obs. of  8 variables:
##  $ Country : chr  "Afghanistan" "Albania" "Algeria" "Angola" ...
##  $ safe    : num  4.13 2.12 2.3 2.41 2.66 ...
##  $ mili    : num  2.47 1.67 2.04 1.71 1.61 ...
##  $ co      : num  3.65 1.4 2.07 1.67 1.2 ...
##  $ Food    : chr  "4.0" "2.0" "2.0" "5.0" ...
##  $ Natural : chr  "5.0" "4.0" "5.0" "3.0" ...
##  $ Rapid   : chr  "5.0" "1.0" "3.0" "5.0" ...
##  $ Water   : chr  "5.0" "3.0" "3.0" "5.0" ...
DATA$`Food `=as.numeric(DATA$`Food `)
DATA$`Natural `=as.numeric(DATA$`Natural `)
DATA$`Rapid `=as.numeric(DATA$`Rapid `)
DATA$`Water `=as.numeric(DATA$`Water `)
dontselect=c("Country")
select=setdiff(names(DATA),dontselect) 
theData=DATA[,select]
theData[!complete.cases(theData),]
## [1] safe     mili     co       Food     Natural  Rapid    Water   
## <0 rows> (or 0-length row.names)
library(polycor)
corMatrix=polycor::hetcor(theData)$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
psych::KMO(corMatrix)
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix)
## Overall MSA =  0.76
## MSA for each item = 
##     safe    mili       co     Food  Natural    Rapid    Water  
##     0.73     0.69     0.71     0.79     0.87     0.76     0.79
cortest.bartlett(corMatrix,n=nrow(theData))$p.value>0.05
## [1] FALSE
library(matrixcalc)

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.
## 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.
## 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   
## safe      0.308  0.735  0.502
## mili                    0.586
## co        0.210  0.402  0.749
## Food      0.690  0.577  0.129
## Natural   0.116  0.431       
## Rapid     0.943  0.204  0.266
## Water     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
print(resfa$loadings,cutoff = 0.5)
## 
## Loadings:
##          MR3    MR1    MR2   
## safe             0.735  0.502
## mili                    0.586
## co                      0.749
## Food      0.690  0.577       
## Natural                      
## Rapid     0.943              
## Water     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
fa.diagram(resfa,main = "Resultados del EFA")