library(rio)
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
library(tidyr)
library(polycor)
library(ggcorrplot)
## Loading required package: ggplot2
data1=import("data5_examen_limpio.xlsx")
data1_limpia <- data1 %>%
  mutate(
electricidad = (data1$`Sí usa electricidad` / (data1$`No usa electricidad`+ data1$`Sí usa electricidad`)) * 100,
gas = (data1$`Sí usa gas (balón GLP)` / (data1$`No usa gas (balón GLP)`+ data1$`Sí usa gas (balón GLP)`)) * 100,
carbon = (data1$`Sí usa carbón` / (data1$`No usa carbón` + data1$`Sí usa carbón`)) * 100,
leña = (data1$`Sí usa leña` / (data1$`No usa leña`+ data1$`Sí usa leña`)) * 100
)
data1_limpia=data1_limpia[,c(11:14)] 
corMatrix1=polycor::hetcor(data1_limpia)$correlations
ggcorrplot(corMatrix1)

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(corMatrix1) 
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix1)
## Overall MSA =  0.63
## MSA for each item = 
## electricidad          gas       carbon         leña 
##         0.91         0.59         0.72         0.59
# No es viable
# Identidad
cortest.bartlett(corMatrix1,n=nrow(data1_limpia))$p.value>0.05
## [1] FALSE
# Singular 
library(matrixcalc)

is.singular.matrix(corMatrix1)
## [1] FALSE
fa.parallel(data1_limpia, fa = 'fa',correct = T,plot = F)
## Parallel analysis suggests that the number of factors =  1  and the number of components =  NA
library(GPArotation)
## 
## Attaching package: 'GPArotation'
## The following objects are masked from 'package:psych':
## 
##     equamax, varimin
resfa1 <- fa(data1_limpia,
            nfactors = 1,
            cor = 'mixed',
            rotate = "varimax", 
            fm="minres")
print(resfa1$loadings)
## 
## Loadings:
##              MR1   
## electricidad  0.491
## gas           0.912
## carbon        0.280
## leña         -0.956
## 
##                  MR1
## SS loadings    2.065
## Proportion Var 0.516
fa_oblimin <- fa(data1_limpia, nfactors = 1, rotate = "oblimin")

# Comparar cargas factoriales
print(resfa1$loadings)
## 
## Loadings:
##              MR1   
## electricidad  0.491
## gas           0.912
## carbon        0.280
## leña         -0.956
## 
##                  MR1
## SS loadings    2.065
## Proportion Var 0.516
print(fa_oblimin$loadings)
## 
## Loadings:
##              MR1   
## electricidad  0.491
## gas           0.912
## carbon        0.280
## leña         -0.956
## 
##                  MR1
## SS loadings    2.065
## Proportion Var 0.516

Por poco, pero es viable resumen todo en un solo factor