setwd("C:/Users/HP/Documents/sem14est")
library(rio)
data=import("dataOK_all.xlsx")
## New names:
## • `` -> `...1`

1. Se quiere organizar un indicador complejo de pobreza, asumiendo que las variables de calidad de techo, pared,piso y la disponibilidad de agua representa cada una un concepto o variable latente.

names(data)
##  [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","elec1_Sí","elec2_No","elec3_Total","departamento","provincia","Castillo","Keiko","ganaCastillo","countPositivos","countFallecidos","pared9_Otro", "techo8_Otro","piso7_Otro","agua8_Otro", "techo9_Total",  "pared10_Total","piso8_Total","agua10_Total"     )
select=setdiff(names(data),dontselect) 
theData=data[,select]

# usaremos:
library(magrittr)
head(theData,10)%>%
    rmarkdown::paged_table()
library(polycor)
## Warning: package 'polycor' was built under R version 4.3.3
corMatrix=polycor::hetcor(theData)$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)
## Warning: package 'ggcorrplot' was built under R version 4.3.3
## Loading required package: ggplot2
ggcorrplot(corMatrix)

#matriz
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(theData))$p.value>0.05
## [1] FALSE
library(matrixcalc)

is.singular.matrix(corMatrix)
## [1] TRUE
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 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 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

VARIMAX

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(theData,
            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

OBLIMIN

library(GPArotation)
resfa2 <- fa(theData,
            nfactors = 4,
            cor = 'mixed',
            rotate = "oblimin", #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(resfa2$loadings)
## 
## Loadings:
##                         MR1    MR3    MR2    MR4   
## pared1_Ladrillo          0.998                     
## pared2_Piedra            0.634                     
## pared3_Adobe                    0.732              
## pared4_Tapia                    0.366        -0.250
## pared5_Quincha                                0.738
## pared6_Piedra                   0.267  0.268       
## pared7_Madera            0.956 -0.191  0.305       
## pared8_Triplay           0.777                0.393
## techo1_Concreto          1.003                     
## techo2_Madera            1.009                     
## techo3_Tejas                    0.580 -0.133 -0.283
## techo4_Planchas          0.814  0.151  0.142  0.215
## techo5_Caña              0.388        -0.245  0.224
## techo6_Triplay           0.815        -0.166  0.218
## techo7_Paja                            0.779       
## piso1_Parquet            1.014               -0.110
## piso2_Láminas            1.019                     
## piso3_Losetas            0.996                     
## piso4_Madera             0.615 -0.114  0.423       
## piso5_Cemento            0.985                     
## piso6_Tierra             0.383  0.664  0.124  0.303
## agua1_Red                0.980                     
## agua2_Red_fueraVivienda  0.963  0.116              
## agua3_Pilón              0.863  0.108              
## agua4_Camión             0.984                     
## agua5_Pozo               0.250  0.311  0.500       
## agua6_Manantial         -0.229  0.509  0.402 -0.122
## agua7_Río               -0.161         0.564  0.563
## agua9_Vecino             0.856                0.232
## 
##                   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
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
fa.diagram(resfa,main = "Resultados del EFA")

#valores que aportan más

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
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

2. Utilizando el porcentaje de viviendas que tiene electricidad, la razón de votacion de castillo entre keiko, y la tasa fallecidos por cada 1000 contagiados, Ud se propone agrupar a las provincias del Peru (sin la provincia de Lima) siguiendo una estrategia aglomerativa (no corrija correlacion negativa si la hubiera); y en ese proceso Ud. encuentra…

names(data)
##  [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"
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
preg2<- data%>%
  select(provincia,elec1_Sí, elec2_No, Castillo, Keiko, ganaCastillo, countPositivos, countFallecidos)
head(preg2)
##              provincia elec1_Sí elec2_No Castillo Keiko ganaCastillo
## 1                BAGUA    13204     6316    25629 10770            1
## 2              BONGARA     6025     1283     8374  5209            1
## 3          CHACHAPOYAS    12248     1751    15671 10473            1
## 4         CONDORCANQUI     1792     7924    13154  1446            1
## 5                 LUYA    10886     1871    12606  7840            1
## 6 RODRÍGUEZ DE MENDOZA     6895     2009     7967  5491            1
##   countPositivos countFallecidos
## 1           8126             462
## 2            389              72
## 3           2174             281
## 4           3481             111
## 5            456              88
## 6            110              60
datos_sin_lima <- preg2 %>%
  filter(provincia != "Lima")
names(preg2)
## [1] "provincia"       "elec1_Sí"        "elec2_No"        "Castillo"       
## [5] "Keiko"           "ganaCastillo"    "countPositivos"  "countFallecidos"
dataClus=preg2[,c(2:8)]
row.names(dataClus)=preg2$provincia
library(cluster)
g.dist = daisy(dataClus, metric="gower")
## Warning in daisy(dataClus, metric = "gower"): binary variable(s) 5 treated as
## interval scaled
## para PAM

library(factoextra)
## Warning: package 'factoextra' was built under R version 4.3.3
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
fviz_nbclust(dataClus, pam,diss=g.dist,method = "gap_stat",k.max = 10,verbose = F)

library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.3.2
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
set.seed(123)
res.pam=pam(g.dist,3,cluster.only = F)

#nueva columna
dataClus$pam=res.pam$cluster

# ver

head(dataClus,15)%>%kbl()%>%kable_styling()
elec1_Sí elec2_No Castillo Keiko ganaCastillo countPositivos countFallecidos pam
BAGUA 13204 6316 25629 10770 1 8126 462 1
BONGARA 6025 1283 8374 5209 1 389 72 1
CHACHAPOYAS 12248 1751 15671 10473 1 2174 281 1
CONDORCANQUI 1792 7924 13154 1446 1 3481 111 1
LUYA 10886 1871 12606 7840 1 456 88 1
RODRÍGUEZ DE MENDOZA 6895 2009 7967 5491 1 110 60 1
UTCUBAMBA 24395 5808 36540 19222 1 3749 336 1
AIJA 1528 413 2325 1413 1 79 26 1
ANTONIO RAYMONDI 3089 697 5056 788 1 54 31 1
ASUNCIÓN 2032 270 2860 827 1 59 21 1
BOLOGNESI 5375 1443 7690 3994 1 242 96 1
CARHUAZ 10348 2655 18781 8590 1 552 163 1
CARLOS FERMÍN FITZCARRALD 3398 1790 6462 1697 1 56 34 1
CASMA 11637 2924 11328 15546 0 963 362 2
CORONGO 1816 209 2174 1460 1 37 19 1
fviz_silhouette(res.pam,print.summary = F)

AGLOMERATIVA

## PARA JERARQUICO

fviz_nbclust(dataClus, hcut,diss=g.dist,method = "gap_stat",k.max = 10,verbose = F,hc_func = "agnes")

set.seed(123)
library(factoextra)

res.agnes<- hcut(g.dist, k = 3,hc_func='agnes',hc_method = "ward.D")

dataClus$agnes=res.agnes$cluster

# ver

head(dataClus,15)%>%kbl()%>%kable_styling()
elec1_Sí elec2_No Castillo Keiko ganaCastillo countPositivos countFallecidos pam agnes
BAGUA 13204 6316 25629 10770 1 8126 462 1 1
BONGARA 6025 1283 8374 5209 1 389 72 1 1
CHACHAPOYAS 12248 1751 15671 10473 1 2174 281 1 1
CONDORCANQUI 1792 7924 13154 1446 1 3481 111 1 1
LUYA 10886 1871 12606 7840 1 456 88 1 1
RODRÍGUEZ DE MENDOZA 6895 2009 7967 5491 1 110 60 1 1
UTCUBAMBA 24395 5808 36540 19222 1 3749 336 1 1
AIJA 1528 413 2325 1413 1 79 26 1 1
ANTONIO RAYMONDI 3089 697 5056 788 1 54 31 1 1
ASUNCIÓN 2032 270 2860 827 1 59 21 1 1
BOLOGNESI 5375 1443 7690 3994 1 242 96 1 1
CARHUAZ 10348 2655 18781 8590 1 552 163 1 1
CARLOS FERMÍN FITZCARRALD 3398 1790 6462 1697 1 56 34 1 1
CASMA 11637 2924 11328 15546 0 963 362 2 2
CORONGO 1816 209 2174 1460 1 37 19 1 1
# Visualize
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.