rm(list = ls())
library(dplyr)
## 
## 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(rio)
library(magrittr)
library(polycor)
library(psych)
## 
## Adjuntando el paquete: 'psych'
## The following object is masked from 'package:polycor':
## 
##     polyserial
library(matrixcalc)
library(GPArotation)
## 
## Adjuntando el paquete: 'GPArotation'
## The following objects are masked from 'package:psych':
## 
##     equamax, varimin
library(BBmisc)
## 
## Adjuntando el paquete: 'BBmisc'
## The following objects are masked from 'package:dplyr':
## 
##     coalesce, collapse, symdiff
## The following object is masked from 'package:base':
## 
##     isFALSE
library(ggcorrplot)
## Cargando paquete requerido: ggplot2
## 
## Adjuntando el paquete: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
library(knitr)
library(modelsummary)
## 
## Adjuntando el paquete: 'modelsummary'
## The following object is masked from 'package:psych':
## 
##     SD
library(kableExtra)
## 
## Adjuntando el paquete: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
library(car)
## Cargando paquete requerido: carData
## 
## Adjuntando el paquete: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
## The following object is masked from 'package:dplyr':
## 
##     recode
library(dplyr)
library(rio)
library(magrittr)
library(polycor)
library(psych)
library(matrixcalc)
library(GPArotation)
library(BBmisc)
library(ggcorrplot)
library(readxl)
data <- read_excel("C:/Users/JHOJAN/Downloads/data.xlsx")
View(data)
library(readxl)
dataOK_all <- read_excel("C:/Users/JHOJAN/Downloads/dataOK_all.xlsx")
View(dataOK_all)
dataOK_all <- lapply(dataOK_all, function(x) {
  if(is.character(x)) {
    iconv(x, to = "ASCII//TRANSLIT")
  } else {
    x
  }
})

# Convertir la lista de vuelta a un dataframe
dataOK_all <- as.data.frame(dataOK_all)
dataOK_all$PROVINCIA <- gsub("CANETE", "CAÑETE", dataOK_all$PROVINCIA)
dataOK_all$PROVINCIA <- gsub("DATEM DEL MARANON", "DATEM DEL MARAÑON", dataOK_all$PROVINCIA)
dataOK_all$PROVINCIA <- gsub("MARANON", "MARAÑON", dataOK_all$PROVINCIA)
dataOK_all$PROVINCIA <- gsub("FERRENAFE", "FERREÑAFE",dataOK_all$PROVINCIA)
datafinal = merge(data, dataOK_all, by = "PROVINCIA", all.x = T)
datos_perdidos1 <- anti_join(data, dataOK_all, by = "PROVINCIA")
datos_perdidos1
## # A tibble: 0 × 44
## # ℹ 44 variables: PROVINCIA <chr>, pared1_Ladrillo <dbl>, pared2_Piedra <dbl>,
## #   pared3_Adobe <dbl>, pared4_Tapia <dbl>, pared5_Quincha <dbl>,
## #   pared6_Piedra <dbl>, pared7_Madera <dbl>, pared8_Triplay <dbl>,
## #   pared9_Otro <dbl>, pared10_Total <dbl>, techo1_Concreto <dbl>,
## #   techo2_Madera <dbl>, techo3_Tejas <dbl>, techo4_Planchas <dbl>,
## #   techo5_Caña <dbl>, techo6_Triplay <dbl>, techo7_Paja <dbl>,
## #   techo8_Otro <dbl>, techo9_Total <dbl>, piso1_Parquet <dbl>, …
names(datafinal)
##  [1] "PROVINCIA"               "pared1_Ladrillo"        
##  [3] "pared2_Piedra"           "pared3_Adobe"           
##  [5] "pared4_Tapia"            "pared5_Quincha"         
##  [7] "pared6_Piedra"           "pared7_Madera"          
##  [9] "pared8_Triplay"          "pared9_Otro"            
## [11] "pared10_Total"           "techo1_Concreto"        
## [13] "techo2_Madera"           "techo3_Tejas"           
## [15] "techo4_Planchas"         "techo5_Caña"            
## [17] "techo6_Triplay"          "techo7_Paja"            
## [19] "techo8_Otro"             "techo9_Total"           
## [21] "piso1_Parquet"           "piso2_Láminas"          
## [23] "piso3_Losetas"           "piso4_Madera"           
## [25] "piso5_Cemento"           "piso6_Tierra"           
## [27] "piso7_Otro"              "piso8_Total"            
## [29] "agua1_Red"               "agua2_Red_fueraVivienda"
## [31] "agua3_Pilón"             "agua4_Camión"           
## [33] "agua5_Pozo"              "agua6_Manantial"        
## [35] "agua7_Río"               "agua8_Otro"             
## [37] "agua9_Vecino"            "agua10_Total"           
## [39] "N_ELEC_HABIL"            "Castillo"               
## [41] "Keiko"                   "VOTOS_VB"               
## [43] "VOTOS_VN"                "ganaCastillo"           
## [45] "elec1_Sí"                "elec2_No"               
## [47] "elec3_Total"
theData <- datafinal %>% select(pared10_Total, techo9_Total, piso8_Total, agua10_Total)
corMatrix=polycor::hetcor(theData)$correlations
round(corMatrix,2)
##               pared10_Total techo9_Total piso8_Total agua10_Total
## pared10_Total          1.00         0.99        0.91         1.00
## techo9_Total           0.99         1.00        0.92         0.99
## piso8_Total            0.91         0.92        1.00         0.91
## agua10_Total           1.00         0.99        0.91         1.00
psych::KMO(corMatrix) 
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix)
## Overall MSA =  0.83
## MSA for each item = 
## pared10_Total  techo9_Total   piso8_Total  agua10_Total 
##          0.75          0.87          0.95          0.81
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
## 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)
resfa <- fa(theData,
            nfactors = 1,
            cor = 'mixed',
            rotate = "varimax", #oblimin?
            fm="minres")
print(resfa$loadings)
## 
## Loadings:
##               MR1  
## pared10_Total 0.997
## techo9_Total  0.998
## piso8_Total   0.913
## agua10_Total  0.994
## 
##                  MR1
## SS loadings    3.813
## Proportion Var 0.953
print(resfa$loadings,cutoff = 0.5)
## 
## Loadings:
##               MR1  
## pared10_Total 0.997
## techo9_Total  0.998
## piso8_Total   0.913
## agua10_Total  0.994
## 
##                  MR1
## SS loadings    3.813
## Proportion Var 0.953
fa.diagram(resfa,main = "Resultados del EFA")

#Pregunta 2

datafinal <- datafinal %>% 
  mutate(Razon_votacion = Castillo / Keiko)