library(rio)
DATAOK = import("DATAOK.xlsx")
## New names:
## • `` -> `...1`
names(DATAOK)
##  [1] "...1"                    "key"                    
##  [3] "Código"                  "pared1_Ladrillo"        
##  [5] "pared2_Piedra"           "pared3_Adobe"           
##  [7] "pared4_Tapia"            "pared5_Quincha"         
##  [9] "pared6_Piedra"           "pared7_Madera"          
## [11] "pared8_Triplay"          "pared9_Otro"            
## [13] "pared10_Total"           "techo1_Concreto"        
## [15] "techo2_Madera"           "techo3_Tejas"           
## [17] "techo4_Planchas"         "techo5_Caña"            
## [19] "techo6_Triplay"          "techo7_Paja"            
## [21] "techo8_Otro"             "techo9_Total"           
## [23] "piso1_Parquet"           "piso2_Láminas"          
## [25] "piso3_Losetas"           "piso4_Madera"           
## [27] "piso5_Cemento"           "piso6_Tierra"           
## [29] "piso7_Otro"              "piso8_Total"            
## [31] "agua1_Red"               "agua2_Red_fueraVivienda"
## [33] "agua3_Pilón"             "agua4_Camión"           
## [35] "agua5_Pozo"              "agua6_Manantial"        
## [37] "agua7_Río"               "agua8_Otro"             
## [39] "agua9_Vecino"            "agua10_Total"           
## [41] "elec1_Sí"                "elec2_No"               
## [43] "elec3_Total"             "departamento"           
## [45] "provincia"               "Castillo"               
## [47] "Keiko"                   "ganaCastillo"           
## [49] "countPositivos"          "countFallecidos"
dontselect=c("...1","key","Código",
             "pared9_Otro","pared10_Total","techo8_Otro","techo9_Total","piso7_Otro","piso8_Total","agua8_Otro","agua10_Total","elec3_Total","elec1_Sí","elec2_No","departamento","provincia","Castillo","Keiko","ganaCastillo","countPositivos","countFallecidos")
select=setdiff(names(DATAOK),dontselect) 
MYDATA=DATAOK[,select]

library(magrittr)
head(MYDATA,10)%>%
    rmarkdown::paged_table()

#CORRELACION

library(polycor)
corMatrix=polycor::hetcor(MYDATA)$correlations
round(corMatrix,2)
##                         pared1_Ladrillo pared2_Piedra pared3_Adobe pared4_Tapia
## pared1_Ladrillo                    1.00          0.65         0.35        -0.03
## pared2_Piedra                      0.65          1.00         0.20        -0.05
## pared3_Adobe                       0.35          0.20         1.00         0.00
## pared4_Tapia                      -0.03         -0.05         0.00         1.00
## pared5_Quincha                     0.22          0.14         0.15        -0.11
## pared6_Piedra                      0.07          0.10         0.18         0.06
## pared7_Madera                      0.89          0.50         0.16        -0.09
## pared8_Triplay                     0.85          0.50         0.27        -0.09
## techo1_Concreto                    1.00          0.65         0.35        -0.02
## techo2_Madera                      0.96          0.55         0.26        -0.04
## techo3_Tejas                       0.13          0.04         0.45         0.41
## techo4_Planchas                    0.92          0.62         0.46        -0.02
## techo5_Caña                        0.44          0.24         0.38        -0.13
## techo6_Triplay                     0.84          0.49         0.29        -0.10
## techo7_Paja                        0.04          0.02         0.08        -0.12
## piso1_Parquet                      0.99          0.59         0.31        -0.02
## piso2_Láminas                      0.99          0.69         0.30        -0.03
## piso3_Losetas                      1.00          0.62         0.36        -0.03
## piso4_Madera                       0.57          0.32         0.12        -0.02
## piso5_Cemento                      1.00          0.67         0.38        -0.03
## piso6_Tierra                       0.69          0.45         0.72         0.14
## agua1_Red                          1.00          0.64         0.39        -0.02
## agua2_Red_fueraVivienda            0.99          0.59         0.40         0.01
## agua3_Pilón                        0.91          0.84         0.38        -0.02
## agua4_Camión                       0.99          0.61         0.33        -0.06
## agua5_Pozo                         0.37          0.25         0.45         0.02
## agua6_Manantial                   -0.05         -0.03         0.27         0.24
## agua7_Río                          0.01          0.00         0.06        -0.06
## agua9_Vecino                       0.94          0.55         0.41        -0.03
##                         pared5_Quincha pared6_Piedra pared7_Madera
## pared1_Ladrillo                   0.22          0.07          0.89
## pared2_Piedra                     0.14          0.10          0.50
## pared3_Adobe                      0.15          0.18          0.16
## pared4_Tapia                     -0.11          0.06         -0.09
## pared5_Quincha                    1.00         -0.03          0.17
## pared6_Piedra                    -0.03          1.00          0.04
## pared7_Madera                     0.17          0.04          1.00
## pared8_Triplay                    0.45          0.04          0.74
## techo1_Concreto                   0.20          0.07          0.88
## techo2_Madera                     0.18          0.05          0.94
## techo3_Tejas                     -0.05          0.00          0.03
## techo4_Planchas                   0.43          0.12          0.87
## techo5_Caña                       0.16         -0.05          0.31
## techo6_Triplay                    0.25          0.03          0.73
## techo7_Paja                      -0.06          0.26          0.24
## piso1_Parquet                     0.17          0.07          0.89
## piso2_Láminas                     0.18          0.07          0.88
## piso3_Losetas                     0.22          0.06          0.89
## piso4_Madera                      0.04          0.02          0.76
## piso5_Cemento                     0.24          0.07          0.89
## piso6_Tierra                      0.50          0.23          0.58
## agua1_Red                         0.24          0.07          0.89
## agua2_Red_fueraVivienda           0.21          0.10          0.90
## agua3_Pilón                       0.28          0.11          0.79
## agua4_Camión                      0.27          0.06          0.88
## agua5_Pozo                        0.07          0.24          0.45
## agua6_Manantial                  -0.04          0.39          0.00
## agua7_Río                         0.47          0.10          0.19
## agua9_Vecino                      0.45          0.06          0.86
##                         pared8_Triplay techo1_Concreto techo2_Madera
## pared1_Ladrillo                   0.85            1.00          0.96
## pared2_Piedra                     0.50            0.65          0.55
## pared3_Adobe                      0.27            0.35          0.26
## pared4_Tapia                     -0.09           -0.02         -0.04
## pared5_Quincha                    0.45            0.20          0.18
## pared6_Piedra                     0.04            0.07          0.05
## pared7_Madera                     0.74            0.88          0.94
## pared8_Triplay                    1.00            0.84          0.79
## techo1_Concreto                   0.84            1.00          0.96
## techo2_Madera                     0.79            0.96          1.00
## techo3_Tejas                      0.04            0.13          0.10
## techo4_Planchas                   0.85            0.91          0.88
## techo5_Caña                       0.51            0.44          0.39
## techo6_Triplay                    0.93            0.83          0.80
## techo7_Paja                       0.01            0.04          0.08
## piso1_Parquet                     0.81            0.99          0.96
## piso2_Láminas                     0.81            0.99          0.96
## piso3_Losetas                     0.85            1.00          0.97
## piso4_Madera                      0.47            0.56          0.61
## piso5_Cemento                     0.85            0.99          0.96
## piso6_Tierra                      0.72            0.68          0.61
## agua1_Red                         0.85            1.00          0.96
## agua2_Red_fueraVivienda           0.82            0.99          0.96
## agua3_Pilón                       0.79            0.91          0.86
## agua4_Camión                      0.89            0.99          0.95
## agua5_Pozo                        0.29            0.37          0.36
## agua6_Manantial                  -0.08           -0.05         -0.05
## agua7_Río                         0.22           -0.02          0.00
## agua9_Vecino                      0.88            0.93          0.90
##                         techo3_Tejas techo4_Planchas techo5_Caña techo6_Triplay
## pared1_Ladrillo                 0.13            0.92        0.44           0.84
## pared2_Piedra                   0.04            0.62        0.24           0.49
## pared3_Adobe                    0.45            0.46        0.38           0.29
## pared4_Tapia                    0.41           -0.02       -0.13          -0.10
## pared5_Quincha                 -0.05            0.43        0.16           0.25
## pared6_Piedra                   0.00            0.12       -0.05           0.03
## pared7_Madera                   0.03            0.87        0.31           0.73
## pared8_Triplay                  0.04            0.85        0.51           0.93
## techo1_Concreto                 0.13            0.91        0.44           0.83
## techo2_Madera                   0.10            0.88        0.39           0.80
## techo3_Tejas                    1.00            0.09       -0.05           0.03
## techo4_Planchas                 0.09            1.00        0.37           0.76
## techo5_Caña                    -0.05            0.37        1.00           0.66
## techo6_Triplay                  0.03            0.76        0.66           1.00
## techo7_Paja                    -0.13            0.13       -0.11          -0.02
## piso1_Parquet                   0.13            0.89        0.38           0.80
## piso2_Láminas                   0.12            0.89        0.38           0.80
## piso3_Losetas                   0.14            0.92        0.44           0.84
## piso4_Madera                    0.16            0.63        0.14           0.43
## piso5_Cemento                   0.13            0.93        0.47           0.85
## piso6_Tierra                    0.32            0.84        0.36           0.62
## agua1_Red                       0.16            0.93        0.46           0.84
## agua2_Red_fueraVivienda         0.19            0.92        0.40           0.81
## agua3_Pilón                     0.11            0.90        0.38           0.76
## agua4_Camión                    0.10            0.91        0.46           0.88
## agua5_Pozo                      0.04            0.51        0.15           0.26
## agua6_Manantial                 0.19            0.06       -0.13          -0.10
## agua7_Río                      -0.10            0.27       -0.03           0.04
## agua9_Vecino                    0.13            0.96        0.45           0.81
##                         techo7_Paja piso1_Parquet piso2_Láminas piso3_Losetas
## pared1_Ladrillo                0.04          0.99          0.99          1.00
## pared2_Piedra                  0.02          0.59          0.69          0.62
## pared3_Adobe                   0.08          0.31          0.30          0.36
## pared4_Tapia                  -0.12         -0.02         -0.03         -0.03
## pared5_Quincha                -0.06          0.17          0.18          0.22
## pared6_Piedra                  0.26          0.07          0.07          0.06
## pared7_Madera                  0.24          0.89          0.88          0.89
## pared8_Triplay                 0.01          0.81          0.81          0.85
## techo1_Concreto                0.04          0.99          0.99          1.00
## techo2_Madera                  0.08          0.96          0.96          0.97
## techo3_Tejas                  -0.13          0.13          0.12          0.14
## techo4_Planchas                0.13          0.89          0.89          0.92
## techo5_Caña                   -0.11          0.38          0.38          0.44
## techo6_Triplay                -0.02          0.80          0.80          0.84
## techo7_Paja                    1.00          0.05          0.05          0.04
## piso1_Parquet                  0.05          1.00          0.99          0.99
## piso2_Láminas                  0.05          0.99          1.00          0.99
## piso3_Losetas                  0.04          0.99          0.99          1.00
## piso4_Madera                   0.34          0.57          0.56          0.57
## piso5_Cemento                  0.04          0.98          0.98          0.99
## piso6_Tierra                   0.20          0.63          0.64          0.68
## agua1_Red                      0.04          0.98          0.99          1.00
## agua2_Red_fueraVivienda        0.06          0.99          0.98          0.99
## agua3_Pilón                    0.08          0.87          0.92          0.90
## agua4_Camión                   0.03          0.98          0.98          0.99
## agua5_Pozo                     0.51          0.35          0.35          0.35
## agua6_Manantial                0.33         -0.04         -0.05         -0.06
## agua7_Río                      0.43         -0.03         -0.03          0.01
## agua9_Vecino                   0.06          0.91          0.91          0.94
##                         piso4_Madera piso5_Cemento piso6_Tierra agua1_Red
## pared1_Ladrillo                 0.57          1.00         0.69      1.00
## pared2_Piedra                   0.32          0.67         0.45      0.64
## pared3_Adobe                    0.12          0.38         0.72      0.39
## pared4_Tapia                   -0.02         -0.03         0.14     -0.02
## pared5_Quincha                  0.04          0.24         0.50      0.24
## pared6_Piedra                   0.02          0.07         0.23      0.07
## pared7_Madera                   0.76          0.89         0.58      0.89
## pared8_Triplay                  0.47          0.85         0.72      0.85
## techo1_Concreto                 0.56          0.99         0.68      1.00
## techo2_Madera                   0.61          0.96         0.61      0.96
## techo3_Tejas                    0.16          0.13         0.32      0.16
## techo4_Planchas                 0.63          0.93         0.84      0.93
## techo5_Caña                     0.14          0.47         0.36      0.46
## techo6_Triplay                  0.43          0.85         0.62      0.84
## techo7_Paja                     0.34          0.04         0.20      0.04
## piso1_Parquet                   0.57          0.98         0.63      0.98
## piso2_Láminas                   0.56          0.98         0.64      0.99
## piso3_Losetas                   0.57          0.99         0.68      1.00
## piso4_Madera                    1.00          0.58         0.38      0.58
## piso5_Cemento                   0.58          1.00         0.71      1.00
## piso6_Tierra                    0.38          0.71         1.00      0.72
## agua1_Red                       0.58          1.00         0.72      1.00
## agua2_Red_fueraVivienda         0.59          0.98         0.72      0.99
## agua3_Pilón                     0.51          0.92         0.73      0.91
## agua4_Camión                    0.56          0.98         0.69      0.99
## agua5_Pozo                      0.43          0.40         0.59      0.38
## agua6_Manantial                -0.01         -0.05         0.30     -0.05
## agua7_Río                       0.35          0.02         0.37      0.02
## agua9_Vecino                    0.59          0.94         0.79      0.95
##                         agua2_Red_fueraVivienda agua3_Pilón agua4_Camión
## pared1_Ladrillo                            0.99        0.91         0.99
## pared2_Piedra                              0.59        0.84         0.61
## pared3_Adobe                               0.40        0.38         0.33
## pared4_Tapia                               0.01       -0.02        -0.06
## pared5_Quincha                             0.21        0.28         0.27
## pared6_Piedra                              0.10        0.11         0.06
## pared7_Madera                              0.90        0.79         0.88
## pared8_Triplay                             0.82        0.79         0.89
## techo1_Concreto                            0.99        0.91         0.99
## techo2_Madera                              0.96        0.86         0.95
## techo3_Tejas                               0.19        0.11         0.10
## techo4_Planchas                            0.92        0.90         0.91
## techo5_Caña                                0.40        0.38         0.46
## techo6_Triplay                             0.81        0.76         0.88
## techo7_Paja                                0.06        0.08         0.03
## piso1_Parquet                              0.99        0.87         0.98
## piso2_Láminas                              0.98        0.92         0.98
## piso3_Losetas                              0.99        0.90         0.99
## piso4_Madera                               0.59        0.51         0.56
## piso5_Cemento                              0.98        0.92         0.98
## piso6_Tierra                               0.72        0.73         0.69
## agua1_Red                                  0.99        0.91         0.99
## agua2_Red_fueraVivienda                    1.00        0.88         0.97
## agua3_Pilón                                0.88        1.00         0.89
## agua4_Camión                               0.97        0.89         1.00
## agua5_Pozo                                 0.39        0.40         0.34
## agua6_Manantial                            0.03        0.01        -0.07
## agua7_Río                                  0.01        0.10         0.03
## agua9_Vecino                               0.93        0.86         0.94
##                         agua5_Pozo agua6_Manantial agua7_Río agua9_Vecino
## pared1_Ladrillo               0.37           -0.05      0.01         0.94
## pared2_Piedra                 0.25           -0.03      0.00         0.55
## pared3_Adobe                  0.45            0.27      0.06         0.41
## pared4_Tapia                  0.02            0.24     -0.06        -0.03
## pared5_Quincha                0.07           -0.04      0.47         0.45
## pared6_Piedra                 0.24            0.39      0.10         0.06
## pared7_Madera                 0.45            0.00      0.19         0.86
## pared8_Triplay                0.29           -0.08      0.22         0.88
## techo1_Concreto               0.37           -0.05     -0.02         0.93
## techo2_Madera                 0.36           -0.05      0.00         0.90
## techo3_Tejas                  0.04            0.19     -0.10         0.13
## techo4_Planchas               0.51            0.06      0.27         0.96
## techo5_Caña                   0.15           -0.13     -0.03         0.45
## techo6_Triplay                0.26           -0.10      0.04         0.81
## techo7_Paja                   0.51            0.33      0.43         0.06
## piso1_Parquet                 0.35           -0.04     -0.03         0.91
## piso2_Láminas                 0.35           -0.05     -0.03         0.91
## piso3_Losetas                 0.35           -0.06      0.01         0.94
## piso4_Madera                  0.43           -0.01      0.35         0.59
## piso5_Cemento                 0.40           -0.05      0.02         0.94
## piso6_Tierra                  0.59            0.30      0.37         0.79
## agua1_Red                     0.38           -0.05      0.02         0.95
## agua2_Red_fueraVivienda       0.39            0.03      0.01         0.93
## agua3_Pilón                   0.40            0.01      0.10         0.86
## agua4_Camión                  0.34           -0.07      0.03         0.94
## agua5_Pozo                    1.00            0.29      0.27         0.42
## agua6_Manantial               0.29            1.00      0.31        -0.06
## agua7_Río                     0.27            0.31      1.00         0.16
## agua9_Vecino                  0.42           -0.06      0.16         1.00
library(ggcorrplot)
## Loading required package: ggplot2
ggcorrplot(corMatrix)

#VERIFICACION PARA EFA

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.43
## MSA for each item = 
##         pared1_Ladrillo           pared2_Piedra            pared3_Adobe 
##                    0.49                    0.29                    0.16 
##            pared4_Tapia          pared5_Quincha           pared6_Piedra 
##                    0.02                    0.10                    0.03 
##           pared7_Madera          pared8_Triplay         techo1_Concreto 
##                    0.44                    0.43                    0.49 
##           techo2_Madera            techo3_Tejas         techo4_Planchas 
##                    0.47                    0.05                    0.47 
##             techo5_Caña          techo6_Triplay             techo7_Paja 
##                    0.18                    0.42                    0.06 
##           piso1_Parquet           piso2_Láminas           piso3_Losetas 
##                    0.74                    0.74                    0.75 
##            piso4_Madera           piso5_Cemento            piso6_Tierra 
##                    0.54                    0.75                    0.65 
##               agua1_Red agua2_Red_fueraVivienda             agua3_Pilón 
##                    0.59                    0.59                    0.54 
##            agua4_Camión              agua5_Pozo         agua6_Manantial 
##                    0.59                    0.25                    0.07 
##               agua7_Río            agua9_Vecino 
##                    0.10                    0.60
cortest.bartlett(corMatrix,n=nrow(MYDATA))$p.value>0.05
## [1] FALSE
library(matrixcalc)

is.singular.matrix(corMatrix)
## [1] TRUE

#PEDIR NUMERO DE FACTORES

fa.parallel(MYDATA, 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 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
## 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 =  4  and the number of components =  NA
library(GPArotation)
## 
## Attaching package: 'GPArotation'
## The following objects are masked from 'package:psych':
## 
##     equamax, varimin
resfa <- fa(MYDATA,
            nfactors = 4,
            cor = 'mixed',
            rotate = "varimax", #oblimin?
            fm="minres")
## 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
print(resfa$loadings)
## 
## Loadings:
##                         MR1    MR2    MR4    MR3   
## pared1_Ladrillo          0.991                     
## pared2_Piedra            0.631                     
## pared3_Adobe             0.274  0.151  0.299  0.639
## pared4_Tapia                          -0.116  0.379
## pared5_Quincha           0.165         0.735       
## pared6_Piedra                   0.321         0.209
## pared7_Madera            0.905  0.317        -0.162
## pared8_Triplay           0.827         0.448       
## techo1_Concreto          0.991                     
## techo2_Madera            0.970                     
## techo3_Tejas                                  0.591
## techo4_Planchas          0.882  0.256  0.335       
## techo5_Caña              0.429 -0.179  0.280       
## techo6_Triplay           0.833         0.286       
## techo7_Paja                     0.767        -0.115
## piso1_Parquet            0.985                     
## piso2_Láminas            0.990                     
## piso3_Losetas            0.989                     
## piso4_Madera             0.584  0.426        -0.125
## piso5_Cemento            0.987         0.112       
## piso6_Tierra             0.592  0.346  0.560  0.502
## agua1_Red                0.985         0.110  0.119
## agua2_Red_fueraVivienda  0.970                0.165
## agua3_Pilón              0.891  0.107  0.176  0.119
## agua4_Camión             0.980         0.149       
## agua5_Pozo               0.331  0.588         0.207
## agua6_Manantial         -0.115  0.499         0.395
## agua7_Río                       0.608  0.515 -0.196
## agua9_Vecino             0.908         0.339       
## 
##                   MR1   MR2   MR4   MR3
## SS loadings    15.787 2.237 1.929 1.621
## Proportion Var  0.544 0.077 0.067 0.056
## Cumulative Var  0.544 0.621 0.688 0.744

#TRY 2

library(GPArotation)
resfa2 <- fa(MYDATA,
            nfactors = 4,
            cor = 'mixed',
            rotate = "oblimin", #varimax?
            fm="minres")
## 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
print(resfa$loadings)
## 
## Loadings:
##                         MR1    MR2    MR4    MR3   
## pared1_Ladrillo          0.991                     
## pared2_Piedra            0.631                     
## pared3_Adobe             0.274  0.151  0.299  0.639
## pared4_Tapia                          -0.116  0.379
## pared5_Quincha           0.165         0.735       
## pared6_Piedra                   0.321         0.209
## pared7_Madera            0.905  0.317        -0.162
## pared8_Triplay           0.827         0.448       
## techo1_Concreto          0.991                     
## techo2_Madera            0.970                     
## techo3_Tejas                                  0.591
## techo4_Planchas          0.882  0.256  0.335       
## techo5_Caña              0.429 -0.179  0.280       
## techo6_Triplay           0.833         0.286       
## techo7_Paja                     0.767        -0.115
## piso1_Parquet            0.985                     
## piso2_Láminas            0.990                     
## piso3_Losetas            0.989                     
## piso4_Madera             0.584  0.426        -0.125
## piso5_Cemento            0.987         0.112       
## piso6_Tierra             0.592  0.346  0.560  0.502
## agua1_Red                0.985         0.110  0.119
## agua2_Red_fueraVivienda  0.970                0.165
## agua3_Pilón              0.891  0.107  0.176  0.119
## agua4_Camión             0.980         0.149       
## agua5_Pozo               0.331  0.588         0.207
## agua6_Manantial         -0.115  0.499         0.395
## agua7_Río                       0.608  0.515 -0.196
## agua9_Vecino             0.908         0.339       
## 
##                   MR1   MR2   MR4   MR3
## SS loadings    15.787 2.237 1.929 1.621
## Proportion Var  0.544 0.077 0.067 0.056
## Cumulative Var  0.544 0.621 0.688 0.744

#RESULTADO MEJORADO PARA MAS DE UN FACTOR

print(resfa$loadings,cutoff = 0.5)
## 
## Loadings:
##                         MR1    MR2    MR4    MR3   
## pared1_Ladrillo          0.991                     
## pared2_Piedra            0.631                     
## pared3_Adobe                                  0.639
## pared4_Tapia                                       
## pared5_Quincha                         0.735       
## pared6_Piedra                                      
## pared7_Madera            0.905                     
## pared8_Triplay           0.827                     
## techo1_Concreto          0.991                     
## techo2_Madera            0.970                     
## techo3_Tejas                                  0.591
## techo4_Planchas          0.882                     
## techo5_Caña                                        
## techo6_Triplay           0.833                     
## techo7_Paja                     0.767              
## piso1_Parquet            0.985                     
## piso2_Láminas            0.990                     
## piso3_Losetas            0.989                     
## piso4_Madera             0.584                     
## piso5_Cemento            0.987                     
## piso6_Tierra             0.592         0.560  0.502
## agua1_Red                0.985                     
## agua2_Red_fueraVivienda  0.970                     
## agua3_Pilón              0.891                     
## agua4_Camión             0.980                     
## agua5_Pozo                      0.588              
## agua6_Manantial                                    
## agua7_Río                       0.608  0.515       
## agua9_Vecino             0.908                     
## 
##                   MR1   MR2   MR4   MR3
## SS loadings    15.787 2.237 1.929 1.621
## Proportion Var  0.544 0.077 0.067 0.056
## Cumulative Var  0.544 0.621 0.688 0.744

#try2

print(resfa2$loadings,cutoff = 0.5)
## 
## Loadings:
##                         MR1    MR3    MR2    MR4   
## pared1_Ladrillo          0.998                     
## pared2_Piedra            0.634                     
## pared3_Adobe                    0.732              
## pared4_Tapia                                       
## pared5_Quincha                                0.738
## pared6_Piedra                                      
## pared7_Madera            0.956                     
## pared8_Triplay           0.777                     
## techo1_Concreto          1.003                     
## techo2_Madera            1.009                     
## techo3_Tejas                    0.580              
## techo4_Planchas          0.814                     
## techo5_Caña                                        
## techo6_Triplay           0.815                     
## techo7_Paja                            0.779       
## piso1_Parquet            1.014                     
## piso2_Láminas            1.019                     
## piso3_Losetas            0.996                     
## piso4_Madera             0.615                     
## piso5_Cemento            0.985                     
## piso6_Tierra                    0.664              
## agua1_Red                0.980                     
## agua2_Red_fueraVivienda  0.963                     
## agua3_Pilón              0.863                     
## agua4_Camión             0.984                     
## agua5_Pozo                             0.500       
## agua6_Manantial                 0.509              
## agua7_Río                              0.564  0.563
## agua9_Vecino             0.856                     
## 
##                   MR1   MR3   MR2   MR4
## SS loadings    15.464 2.009 1.852 1.523
## Proportion Var  0.533 0.069 0.064 0.053
## Cumulative Var  0.533 0.603 0.666 0.719

#RESULTADOS

fa.diagram(resfa,main = "Resultados del EFA")

#try2

fa.diagram(resfa2,main = "Resultados del EFA_2")

#CUANTO APORTAN LAS VARIABLES A CADA FACTOR

sort(resfa$communality)
##           pared6_Piedra            pared4_Tapia             techo5_Caña 
##               0.1479411               0.1599348               0.2971420 
##            techo3_Tejas           pared2_Piedra         agua6_Manantial 
##               0.3611027               0.4059884               0.4187500 
##              agua5_Pozo            piso4_Madera          pared5_Quincha 
##               0.5072346               0.5401829               0.5768864 
##            pared3_Adobe             techo7_Paja               agua7_Río 
##               0.5958685               0.6045122               0.6748916 
##          techo6_Triplay             agua3_Pilón          pared8_Triplay 
##               0.7840876               0.8502469               0.8888532 
##           pared7_Madera           techo2_Madera            agua9_Vecino 
##               0.9457654               0.9479949               0.9521136 
##         techo4_Planchas           piso1_Parquet agua2_Red_fueraVivienda 
##               0.9654449               0.9766272               0.9802470 
##            agua4_Camión           piso2_Láminas           piso3_Losetas 
##               0.9847153               0.9860292               0.9929105 
##         techo1_Concreto         pared1_Ladrillo               agua1_Red 
##               0.9958066               0.9977988               0.9992224 
##           piso5_Cemento            piso6_Tierra 
##               0.9994736               1.0358692

#TRY2

sort(resfa2$communality)
##           pared6_Piedra            pared4_Tapia             techo5_Caña 
##               0.1479411               0.1599348               0.2971420 
##            techo3_Tejas           pared2_Piedra         agua6_Manantial 
##               0.3611027               0.4059884               0.4187500 
##              agua5_Pozo            piso4_Madera          pared5_Quincha 
##               0.5072346               0.5401829               0.5768864 
##            pared3_Adobe             techo7_Paja               agua7_Río 
##               0.5958685               0.6045122               0.6748916 
##          techo6_Triplay             agua3_Pilón          pared8_Triplay 
##               0.7840876               0.8502469               0.8888532 
##           pared7_Madera           techo2_Madera            agua9_Vecino 
##               0.9457654               0.9479949               0.9521136 
##         techo4_Planchas           piso1_Parquet agua2_Red_fueraVivienda 
##               0.9654449               0.9766272               0.9802470 
##            agua4_Camión           piso2_Láminas           piso3_Losetas 
##               0.9847153               0.9860292               0.9929105 
##         techo1_Concreto         pared1_Ladrillo               agua1_Red 
##               0.9958066               0.9977988               0.9992224 
##           piso5_Cemento            piso6_Tierra 
##               0.9994736               1.0358692

#VARIABLES QUE CONTRIBUYEN A MAS DE UN FACTOR

sort(resfa$complexity)
##           piso2_Láminas           piso1_Parquet           techo2_Madera 
##                1.011784                1.014342                1.016012 
##         techo1_Concreto           piso3_Losetas         pared1_Ladrillo 
##                1.027309                1.031529                1.032962 
##           pared2_Piedra            agua4_Camión           piso5_Cemento 
##                1.039372                1.048692                1.050759 
##             techo7_Paja               agua1_Red            techo3_Tejas 
##                1.056476                1.058431                1.069872 
## agua2_Red_fueraVivienda          pared5_Quincha             agua3_Pilón 
##                1.085726                1.139839                1.145980 
##            pared4_Tapia          techo6_Triplay            agua9_Vecino 
##                1.233110                1.259537                1.310512 
##           pared7_Madera         techo4_Planchas          pared8_Triplay 
##                1.311682                1.495958                1.552028 
##           pared6_Piedra              agua5_Pozo            piso4_Madera 
##                1.742245                1.931349                1.949594 
##            pared3_Adobe         agua6_Manantial             techo5_Caña 
##                1.958637                2.029351                2.156064 
##               agua7_Río            piso6_Tierra 
##                2.188219                3.587191

#TRY2

sort(resfa2$complexity)
##             techo7_Paja         pared1_Ladrillo           piso3_Losetas 
##                1.002881                1.005253                1.007188 
##           piso5_Cemento           pared2_Piedra         techo1_Concreto 
##                1.007774                1.008549                1.013302 
##            agua4_Camión               agua1_Red           piso2_Láminas 
##                1.013787                1.014631                1.019858 
##           piso1_Parquet           techo2_Madera             agua3_Pilón 
##                1.023800                1.027409                1.039974 
##          pared5_Quincha agua2_Red_fueraVivienda            pared3_Adobe 
##                1.041406                1.042834                1.043910 
##            agua9_Vecino          techo6_Triplay         techo4_Planchas 
##                1.169709                1.232282                1.278679 
##           pared7_Madera          pared8_Triplay            techo3_Tejas 
##                1.289426                1.513417                1.570284 
##            piso4_Madera            pared4_Tapia            piso6_Tierra 
##                1.870983                1.975566                2.150520 
##               agua7_Río              agua5_Pozo           pared6_Piedra 
##                2.170220                2.215170                2.251239 
##             techo5_Caña         agua6_Manantial 
##                2.414398                2.474577

#PRUEBAS

resfa$TLI #Tucker Lewis>0.9
## [1] 0.2649863
resfa$rms #RMS cerca a 0
## [1] 0.03908243
resfa$RMSEA #RMSEA cerca a 0
##      RMSEA      lower      upper confidence 
##  0.4575939  0.4519249  0.4657006  0.9000000
resfa$BIC
## [1] 10883.29

#INDICE

as.data.frame(resfa$scores)%>%head()
##          MR1        MR2       MR4       MR3
## 1  2123.7200 -2404.6136 -5291.691 -9319.136
## 2  -633.1749   708.3583  1583.354  2798.038
## 3  1915.6284 -2195.2125 -4755.759 -8346.445
## 4 -1614.6513  1802.2310  4037.661  7138.652
## 5 -2232.2260  2495.7962  5578.198  9857.594
## 6  -749.4869   821.5328  1883.986  3347.314

#CLUSTERIZACION

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
ELECCIONES <- DATAOK %>%
  select(41:ncol(.))
head(ELECCIONES)
##   elec1_Sí elec2_No elec3_Total departamento            provincia Castillo
## 1    13204     6316       19520     AMAZONAS                BAGUA    25629
## 2     6025     1283        7308     AMAZONAS              BONGARA     8374
## 3    12248     1751       13999     AMAZONAS          CHACHAPOYAS    15671
## 4     1792     7924        9716     AMAZONAS         CONDORCANQUI    13154
## 5    10886     1871       12757     AMAZONAS                 LUYA    12606
## 6     6895     2009        8904     AMAZONAS RODRÍGUEZ DE MENDOZA     7967
##   Keiko ganaCastillo countPositivos countFallecidos
## 1 10770            1           8126             462
## 2  5209            1            389              72
## 3 10473            1           2174             281
## 4  1446            1           3481             111
## 5  7840            1            456              88
## 6  5491            1            110              60

#TRANSFORMACION DE DATOS

boxplot(ELECCIONES[,c(1,2,3,6,7,9,10)],horizontal = F,las=2,cex.axis = 0.5)

#NORMALIZACION DE DATOS

library(BBmisc)
## 
## Attaching package: 'BBmisc'
## The following objects are masked from 'package:dplyr':
## 
##     coalesce, collapse, symdiff
## The following object is masked from 'package:base':
## 
##     isFALSE
boxplot(normalize(ELECCIONES[,c(1,6,7,10)],method='range',range=c(0,100)))

boxplot(normalize(ELECCIONES[,c(1,6,7,10)],method='standardize'))

ELECCIONES[,c(1,6,7,10)]=normalize(ELECCIONES[,c(1,6,7,10)],method='standardize')

#CORRELACION

cor(ELECCIONES[,c(1,6,7,10)])
##                  elec1_Sí  Castillo     Keiko countFallecidos
## elec1_Sí        1.0000000 0.9902201 0.9963209       0.9981684
## Castillo        0.9902201 1.0000000 0.9753987       0.9821661
## Keiko           0.9963209 0.9753987 1.0000000       0.9986153
## countFallecidos 0.9981684 0.9821661 0.9986153       1.0000000

#PREP DATA

CLUSDATA <- ELECCIONES[ELECCIONES$provincia != "LIMA", ]


```r
dataClus=CLUSDATA[,c(1,6,7,10)]
row.names(dataClus)=CLUSDATA$provincia

#matriz de distancia

library(cluster)
g.dist = daisy(dataClus, metric="gower")

#AGNES

library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
fviz_nbclust(dataClus, hcut,diss=g.dist,method = "gap_stat",k.max = 10,verbose = F,hc_func = "agnes")

set.seed(123)
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
res.agnes<- hcut(g.dist, k = 4,hc_func='agnes',hc_method = "ward.D")

dataClus$agnes=res.agnes$cluster

# ver
head(dataClus,15)%>%kbl()%>%kable_styling()
elec1_Sí Castillo Keiko countFallecidos agnes
BAGUA -0.1394809 -0.1304810 -0.1223264 -0.1020203 1
BONGARA -0.1866273 -0.2501489 -0.1429655 -0.1618421 1
CHACHAPOYAS -0.1457592 -0.1995423 -0.1234287 -0.1297837 1
CONDORCANQUI -0.2144266 -0.2169984 -0.1569315 -0.1558599 1
LUYA -0.1547038 -0.2207989 -0.1332008 -0.1593879 1
RODRÍGUEZ DE MENDOZA -0.1809138 -0.2529716 -0.1419189 -0.1636828 1
UTCUBAMBA -0.0659866 -0.0548103 -0.0909577 -0.1213473 2
AIJA -0.2161603 -0.2921003 -0.1570540 -0.1688980 1
ANTONIO RAYMONDI -0.2059088 -0.2731601 -0.1593736 -0.1681310 1
ASUNCIÓN -0.2128504 -0.2883900 -0.1592289 -0.1696649 1
BOLOGNESI -0.1908960 -0.2548927 -0.1474749 -0.1581607 1
CARHUAZ -0.1582370 -0.1779737 -0.1304173 -0.1478837 1
CARLOS FERMÍN FITZCARRALD -0.2038795 -0.2634092 -0.1559999 -0.1676709 1
CASMA -0.1497718 -0.2296622 -0.1046008 -0.1173592 1
CORONGO -0.2142689 -0.2931476 -0.1568795 -0.1699717 1

#Dendograma

fviz_dend(res.agnes, cex = 0.7, horiz = T,main = "")
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the factoextra package.
##   Please report the issue at <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

fviz_silhouette(res.agnes,print.summary = F)

#POISSON

library(knitr)
library(modelsummary)
## 
## Attaching package: 'modelsummary'
## The following object is masked from 'package:psych':
## 
##     SD
h1=formula(ganaCastillo~elec1_Sí+countFallecidos)

rl1=lm(h1, data = ELECCIONES)

model1=list('Gana Castillo(I)'=rl1)
modelsummary(model1, title = "Resumen de Regresion Lineal",
             stars = TRUE,
             output = "kableExtra")
Resumen de Regresion Lineal
 Gana Castillo(I)
(Intercept) 0.781***
(0.029)
elec1_Sí 0.898+
(0.477)
countFallecidos -0.987*
(0.477)
Num.Obs. 196
R2 0.065
R2 Adj. 0.055
AIC 205.2
BIC 218.3
Log.Lik. -98.591
F 6.713
RMSE 0.40
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001