setwd("~/FINALESTADISTICA")
library(rio)
## Warning: package 'rio' was built under R version 4.4.2
data1<-import("data1.xlsx")
# Cargar las librerías necesarias
library(readxl)
## Warning: package 'readxl' was built under R version 4.4.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.2
## 
## Adjuntando el paquete: '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(cluster)
## Warning: package 'cluster' was built under R version 4.4.2
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.4.2
## Cargando paquete requerido: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(psych)
## Warning: package 'psych' was built under R version 4.4.2
## 
## Adjuntando el paquete: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
# Asegurarse de que los nombres de las columnas coincidan exactamente con los del archivo
names(data1) <- gsub(" ", "_", names(data1)) # Reemplazar espacios por guiones bajos en los nombres de columnas
#porcentaje de hogares si usa electricidad
data1 <- data1 %>% 
  mutate(pct_si_electricidad = (`Sí_usa_electricidad` / (`No_usa_electricidad` + `Sí_usa_electricidad`)) * 100,
         pct_si_gas_glp = (`Sí_usa_gas_(balón_GLP)` / (`No_usa_gas_(balón_GLP)` + `Sí_usa_gas_(balón_GLP)`)) * 100,
         pct_si_carbon = (`Sí_usa_carbón` / (`No_usa_carbón` + `Sí_usa_carbón`)) * 100,
         pct_si_lena = (`Sí_usa_leña` / (`No_usa_leña` + `Sí_usa_leña`)) * 100)
# Seleccionar las variables de interés para el análisis factorial
variables <- data1 %>% 
  select(pct_si_electricidad, pct_si_gas_glp, pct_si_carbon, pct_si_lena)
# Realizar el análisis factorial
# Primero, calcular la matriz de correlación
cor_matrix <- cor(variables, use = "complete.obs")
# Evaluar la adecuación del análisis factorial
# Prueba de esfericidad de Bartlett y KMO
bartlett_test <- cortest.bartlett(cor_matrix)
## Warning in cortest.bartlett(cor_matrix): n not specified, 100 used
kmo_test <- KMO(cor_matrix)
# Realizar el análisis factorial con un factor
ef_fa <- fa(variables, nfactors = 1, rotate = "none")
summary(ef_fa)
## 
## Factor analysis with Call: fa(r = variables, nfactors = 1, rotate = "none")
## 
## Test of the hypothesis that 1 factor is sufficient.
## The degrees of freedom for the model is 2  and the objective function was  0.03 
## The number of observations was  197  with Chi Square =  6.34  with prob <  0.042 
## 
## The root mean square of the residuals (RMSA) is  0.03 
## The df corrected root mean square of the residuals is  0.05 
## 
## Tucker Lewis Index of factoring reliability =  0.961
## RMSEA index =  0.105  and the 10 % confidence intervals are  0.017 0.202
## BIC =  -4.23
# Realizar análisis factorial con rotación varimax y oblimin
fa_varimax <- fa(variables, nfactors = 1, rotate = "varimax")
fa_oblimin <- fa(variables, nfactors = 1, rotate = "oblimin")
# Evaluar la varianza explicada
varianza_explicada <- ef_fa$Vaccounted
# Mostrar los resultados
evaluacion_resultados <- list(
  "Bartlett Test" = bartlett_test,
  "KMO Test" = kmo_test,
  "Factor Analysis - No Rotation" = ef_fa,
  "Factor Analysis - Varimax" = fa_varimax,
  "Factor Analysis - Oblimin" = fa_oblimin,
  "Varianza Explicada" = varianza_explicada
)

evaluacion_resultados
## $`Bartlett Test`
## $`Bartlett Test`$chisq
## [1] 170.7995
## 
## $`Bartlett Test`$p.value
## [1] 3.043729e-34
## 
## $`Bartlett Test`$df
## [1] 6
## 
## 
## $`KMO Test`
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = cor_matrix)
## Overall MSA =  0.63
## MSA for each item = 
## pct_si_electricidad      pct_si_gas_glp       pct_si_carbon         pct_si_lena 
##                0.91                0.59                0.72                0.59 
## 
## $`Factor Analysis - No Rotation`
## Factor Analysis using method =  minres
## Call: fa(r = variables, nfactors = 1, rotate = "none")
## Standardized loadings (pattern matrix) based upon correlation matrix
##                       MR1    h2    u2 com
## pct_si_electricidad  0.49 0.243 0.757   1
## pct_si_gas_glp       0.91 0.832 0.168   1
## pct_si_carbon        0.28 0.078 0.922   1
## pct_si_lena         -0.96 0.914 0.086   1
## 
##                 MR1
## SS loadings    2.07
## Proportion Var 0.52
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## df null model =  6  with the objective function =  1.76 with Chi Square =  341.89
## df of  the model are 2  and the objective function was  0.03 
## 
## The root mean square of the residuals (RMSR) is  0.03 
## The df corrected root mean square of the residuals is  0.05 
## 
## The harmonic n.obs is  197 with the empirical chi square  2.12  with prob <  0.35 
## The total n.obs was  197  with Likelihood Chi Square =  6.34  with prob <  0.042 
## 
## Tucker Lewis Index of factoring reliability =  0.961
## RMSEA index =  0.105  and the 90 % confidence intervals are  0.017 0.202
## BIC =  -4.23
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                    MR1
## Correlation of (regression) scores with factors   0.97
## Multiple R square of scores with factors          0.94
## Minimum correlation of possible factor scores     0.89
## 
## $`Factor Analysis - Varimax`
## Factor Analysis using method =  minres
## Call: fa(r = variables, nfactors = 1, rotate = "varimax")
## Standardized loadings (pattern matrix) based upon correlation matrix
##                       MR1    h2    u2 com
## pct_si_electricidad  0.49 0.243 0.757   1
## pct_si_gas_glp       0.91 0.832 0.168   1
## pct_si_carbon        0.28 0.078 0.922   1
## pct_si_lena         -0.96 0.914 0.086   1
## 
##                 MR1
## SS loadings    2.07
## Proportion Var 0.52
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## df null model =  6  with the objective function =  1.76 with Chi Square =  341.89
## df of  the model are 2  and the objective function was  0.03 
## 
## The root mean square of the residuals (RMSR) is  0.03 
## The df corrected root mean square of the residuals is  0.05 
## 
## The harmonic n.obs is  197 with the empirical chi square  2.12  with prob <  0.35 
## The total n.obs was  197  with Likelihood Chi Square =  6.34  with prob <  0.042 
## 
## Tucker Lewis Index of factoring reliability =  0.961
## RMSEA index =  0.105  and the 90 % confidence intervals are  0.017 0.202
## BIC =  -4.23
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                    MR1
## Correlation of (regression) scores with factors   0.97
## Multiple R square of scores with factors          0.94
## Minimum correlation of possible factor scores     0.89
## 
## $`Factor Analysis - Oblimin`
## Factor Analysis using method =  minres
## Call: fa(r = variables, nfactors = 1, rotate = "oblimin")
## Standardized loadings (pattern matrix) based upon correlation matrix
##                       MR1    h2    u2 com
## pct_si_electricidad  0.49 0.243 0.757   1
## pct_si_gas_glp       0.91 0.832 0.168   1
## pct_si_carbon        0.28 0.078 0.922   1
## pct_si_lena         -0.96 0.914 0.086   1
## 
##                 MR1
## SS loadings    2.07
## Proportion Var 0.52
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## df null model =  6  with the objective function =  1.76 with Chi Square =  341.89
## df of  the model are 2  and the objective function was  0.03 
## 
## The root mean square of the residuals (RMSR) is  0.03 
## The df corrected root mean square of the residuals is  0.05 
## 
## The harmonic n.obs is  197 with the empirical chi square  2.12  with prob <  0.35 
## The total n.obs was  197  with Likelihood Chi Square =  6.34  with prob <  0.042 
## 
## Tucker Lewis Index of factoring reliability =  0.961
## RMSEA index =  0.105  and the 90 % confidence intervals are  0.017 0.202
## BIC =  -4.23
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                    MR1
## Correlation of (regression) scores with factors   0.97
## Multiple R square of scores with factors          0.94
## Minimum correlation of possible factor scores     0.89
## 
## $`Varianza Explicada`
##                      MR1
## SS loadings    2.0677210
## Proportion Var 0.5169303
# Seleccionar las variables de interés para el análisis factorial
variables <- data1 %>% 
  dplyr::select(pct_si_electricidad, pct_si_gas_glp, pct_si_carbon, pct_si_lena)
# Realizar el análisis factorial
# Primero, calcular la matriz de correlación
cor_matrix <- cor(variables, use = "complete.obs")
# Evaluar la adecuación del análisis factorial
# Prueba de esfericidad de Bartlett y KMO
bartlett_test <- cortest.bartlett(cor_matrix)
## Warning in cortest.bartlett(cor_matrix): n not specified, 100 used
kmo_test <- KMO(cor_matrix)
# Realizar el análisis factorial con un factor
ef_fa <- fa(variables, nfactors = 1, rotate = "none")
summary(ef_fa)
## 
## Factor analysis with Call: fa(r = variables, nfactors = 1, rotate = "none")
## 
## Test of the hypothesis that 1 factor is sufficient.
## The degrees of freedom for the model is 2  and the objective function was  0.03 
## The number of observations was  197  with Chi Square =  6.34  with prob <  0.042 
## 
## The root mean square of the residuals (RMSA) is  0.03 
## The df corrected root mean square of the residuals is  0.05 
## 
## Tucker Lewis Index of factoring reliability =  0.961
## RMSEA index =  0.105  and the 10 % confidence intervals are  0.017 0.202
## BIC =  -4.23
# Realizar análisis factorial con rotación varimax y oblimin
fa_varimax <- fa(variables, nfactors = 1, rotate = "varimax")
fa_oblimin <- fa(variables, nfactors = 1, rotate = "oblimin")
# Evaluar la varianza explicada
varianza_explicada <- ef_fa$Vaccounted
# Mostrar los resultados
evaluacion_resultados <- list(
  "Bartlett Test" = bartlett_test,
  "KMO Test" = kmo_test,
  "Factor Analysis - No Rotation" = ef_fa,
  "Factor Analysis - Varimax" = fa_varimax,
  "Factor Analysis - Oblimin" = fa_oblimin,
  "Varianza Explicada" = varianza_explicada
)