library(rio)
theData=import("reporte.xlsx")
## New names:
## • `` -> `...1`
str(theData)
## 'data.frame':    200 obs. of  11 variables:
##  $ ...1                  : chr  NA NA NA NA ...
##  $ Código                : chr  "101" "102" "103" "104" ...
##  $ Provincia             : chr  "Amazonas, provincia: Chachapoyas" "Amazonas, provincia: Bagua" "Amazonas, provincia: Bongara" "Amazonas, provincia: Condorcanqui" ...
##  $ No usa electricidad   : num  14763 20313 7689 9853 13112 ...
##  $ Sí usa electricidad   : num  574 161 124 14 90 65 255 921 16 33 ...
##  $ No usa gas (balón GLP): num  4696 10557 3154 8331 6863 ...
##  $ Sí usa gas (balón GLP): num  10641 9917 4659 1536 6339 ...
##  $ No usa carbón         : num  15161 20185 7755 9841 13169 ...
##  $ Sí usa carbón         : num  176 289 58 26 33 26 335 218 4 4 ...
##  $ No usa leña           : num  7236 7357 2345 1059 1833 ...
##  $ Sí usa leña           : num  8101 13117 5468 8808 11369 ...
names(theData)
##  [1] "...1"                   "Código"                 "Provincia"             
##  [4] "No usa electricidad"    "Sí usa electricidad"    "No usa gas (balón GLP)"
##  [7] "Sí usa gas (balón GLP)" "No usa carbón"          "Sí usa carbón"         
## [10] "No usa leña"            "Sí usa leña"
theData$total_con_electricidad <- theData$"Sí usa electricidad" + theData$"No usa electricidad"
theData$Electricidad_pct=theData$"Sí usa electricidad"/theData$total_con_electricidad
theData$total_con_gas<- theData$"Sí usa gas (balón GLP)" + theData$"No usa gas (balón GLP)"
theData$gas_pct=theData$"Sí usa gas (balón GLP)"/theData$total_con_gas
theData$total_con_carbon<- theData$"No usa carbón" + theData$ "Sí usa carbón" 
theData$Carbon_pct= theData$ "Sí usa carbón" /theData$total_con_carbon
theData$total_con_leña<- theData$"No usa leña" + theData$ "Sí usa leña" 
theData$leña_pct=theData$"Sí usa leña" /theData$total_con_leña
library(polycor)
## Warning: package 'polycor' was built under R version 4.3.3
dontselect=c("...1" ,                  "Código"                , "Provincia" ,             "No usa electricidad",   "Sí usa electricidad"  ,  "No usa gas (balón GLP)", "Sí usa gas (balón GLP)", "No usa carbón",         
 "Sí usa carbón"   ,       "No usa leña"        , "Sí usa leña", "total_con_electricidad", "total_con_leña","total_con_gas","total_con_carbon")
select=setdiff(names(theData),dontselect) 
theData1=theData[,select]

# usaremos:
library(magrittr)
head(theData1,10)%>%
    rmarkdown::paged_table()
corMatrix=polycor::hetcor(theData1)$correlations
round(corMatrix,2)
##                  Electricidad_pct gas_pct Carbon_pct leña_pct
## Electricidad_pct             1.00    0.48       0.10    -0.46
## gas_pct                      0.48    1.00       0.23    -0.87
## Carbon_pct                   0.10    0.23       1.00    -0.31
## leña_pct                    -0.46   -0.87      -0.31     1.00
library(psych)
## Warning: package 'psych' was built under R version 4.3.3
## 
## Attaching package: 'psych'
## 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.63
## MSA for each item = 
## Electricidad_pct          gas_pct       Carbon_pct         leña_pct 
##             0.91             0.59             0.72             0.59
cortest.bartlett(corMatrix,n=nrow(theData))$p.value>0.05
## [1] FALSE
library(matrixcalc)

is.singular.matrix(corMatrix)
## [1] FALSE
fa.parallel(theData1, fa = 'fa',correct = T,plot = F)
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Parallel analysis suggests that the number of factors =  1  and the number of components =  NA
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(theData1,
            nfactors = 1,
            cor = 'mixed',
            rotate = "varimax", #oblimin?
            fm="minres")
print(resfa$loadings)
## 
## Loadings:
##                  MR1   
## Electricidad_pct  0.493
## gas_pct           0.912
## Carbon_pct        0.279
## leña_pct         -0.956
## 
##                  MR1
## SS loadings    2.068
## Proportion Var 0.517
library(GPArotation)
resfa <- fa(theData1,
            nfactors = 1,
            cor = 'mixed',
            rotate = "oblimin", 
            fm="minres")
print(resfa$loadings)
## 
## Loadings:
##                  MR1   
## Electricidad_pct  0.493
## gas_pct           0.912
## Carbon_pct        0.279
## leña_pct         -0.956
## 
##                  MR1
## SS loadings    2.068
## Proportion Var 0.517