PREGUNTA 1

setwd("C:/Users/aml/Downloads")
library(rio)
data=import("reporte.xlsx")
## New names:
## • `` -> `...1`
## • `Total` -> `Total...6`
## • `Total` -> `Total...9`
## • `Total` -> `Total...12`
## • `Total` -> `Total...15`
names(data)
##  [1] "...1"                   "Código"                 "Provincia"             
##  [4] "No usa electricidad"    "Sí usa electricidad"    "Total...6"             
##  [7] "No usa gas (balón GLP)" "Sí usa gas (balón GLP)" "Total...9"             
## [10] "No usa carbón"          "Sí usa carbón"          "Total...12"            
## [13] "No usa leña"            "Sí usa leña"            "Total...15"
select <- c(5, 8, 11, 14)

theData <- data[, select]

library(magrittr)
head(theData, 15) %>%
  rmarkdown::paged_table()
colnames(theData) <- c("elec_si", "gas_si", "carbon_si", "leña_si")
theData[, "elec_si"] <- as.numeric(theData[, "elec_si"])
theData[, "gas_si"] <- as.numeric(theData[, "gas_si"])
theData[, "carbon_si"] <- as.numeric(theData[,"carbon_si"])
theData[,"leña_si"] <- as.numeric(theData[,"leña_si"])
corMatrix=polycor::hetcor(theData)$correlations
round(corMatrix,2)
##           elec_si gas_si carbon_si leña_si
## elec_si      1.00   0.99      0.49    0.29
## gas_si       0.99   1.00      0.53    0.34
## carbon_si    0.49   0.53      1.00    0.42
## leña_si      0.29   0.34      0.42    1.00
library(ggcorrplot)
## Warning: package 'ggcorrplot' was built under R version 4.3.3
## Loading required package: ggplot2
ggcorrplot(corMatrix)

library(psych)
## Warning: package 'psych' was built under R version 4.3.3
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
psych::KMO(corMatrix) 
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix)
## Overall MSA =  0.56
## MSA for each item = 
##   elec_si    gas_si carbon_si   leña_si 
##      0.52      0.53      0.81      0.52
cortest.bartlett(corMatrix,n=nrow(theData))$p.value>0.05
## [1] FALSE
library(matrixcalc)

is.singular.matrix(corMatrix)
## [1] FALSE
fa.parallel(theData, fa = 'fa',correct = T,plot = F)
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Parallel analysis suggests that the number of factors =  2  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(theData,
            nfactors = 2,
            cor = 'mixed',
            rotate = "varimax", #oblimin?
            fm="minres")
print(resfa$loadings)
## 
## Loadings:
##           MR1   MR2  
## elec_si   0.968 0.242
## gas_si    0.942 0.330
## carbon_si 0.365 0.554
## leña_si   0.130 0.667
## 
##                  MR1   MR2
## SS loadings    1.974 0.919
## Proportion Var 0.494 0.230
## Cumulative Var 0.494 0.723
print(resfa$loadings,cutoff = 0.5)
## 
## Loadings:
##           MR1   MR2  
## elec_si   0.968      
## gas_si    0.942      
## carbon_si       0.554
## leña_si         0.667
## 
##                  MR1   MR2
## SS loadings    1.974 0.919
## Proportion Var 0.494 0.230
## Cumulative Var 0.494 0.723
fa.diagram(resfa,main = "Resultados del EFA")

as.data.frame(resfa$scores)%>%head()
##            MR1         MR2
## 1  0.007424396 -0.40533960
## 2 -0.116962309 -0.07803296
## 3 -0.018713896 -0.48232073
## 4 -0.057290277 -0.41400902
## 5 -0.091428184 -0.22345996
## 6 -0.043686685 -0.42121890