library(rio)
data=import("02_2C.xlsx")
## New names:
## • `` -> `...1`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
## • `` -> `...9`
## • `` -> `...10`
## • `` -> `...11`
## • `` -> `...12`
## • `` -> `...13`
## • `` -> `...14`
## • `` -> `...15`
## • `` -> `...16`
data=na.omit(data)
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
# Renombrar variables y eliminar la columna ...6
data <- data %>%
select(-`...6`) %>% # Elimina la columna ...6
rename(
n = `...1`,
ubigeo = `ANEXO Nº II.2C`,
departamento = `...3`,
provincia = `...4`,
distrito = `...5`,
dañadas_freq = `...7`,
dañadas_por = `...8`,
hacinadas_freq = `...9`,
hacinadas_por = `...10`,
sin_sshh_freq = `...11`,
sin_sshh_por = `...12`,
niños_sinescuela_freq = `...13`,
niños_sinescuela_por = `...14`,
dep_economica_freq = `...15`,
dep_economica_por = `...16`
)
library(dplyr)
data <- data %>%
mutate(
across(
-c(departamento, provincia, distrito), # Excluir estas columnas
as.numeric # Convertir el resto a numéricas
)
)
dontselect=c("provincia","dañadas_por","hacinadas_por",
"sin_sshh_por","niños_sinescuela_por","dep_economica_por", "n", "ubigeo", "departamento", "distrito", "")
select=setdiff(names(data),dontselect)
theData=data[,select]
# usaremos:
library(magrittr)
head(theData,10)%>%
rmarkdown::paged_table()
str(theData)
## 'data.frame': 1874 obs. of 5 variables:
## $ dañadas_freq : num 247 74 10 55 86 86 60 3 193 88 ...
## $ hacinadas_freq : num 405 9 47 3 19 36 4 7 135 116 ...
## $ sin_sshh_freq : num 113 0 44 26 52 43 4 17 63 22 ...
## $ niños_sinescuela_freq: num 64 1 3 2 5 7 0 0 13 15 ...
## $ dep_economica_freq : num 106 0 16 2 0 19 3 2 32 44 ...
theData$dañadas_freq = as.numeric(theData$dañadas_freq)
theData$hacinadas_freq = as.numeric(theData$hacinadas_freq)
theData$sin_sshh_freq = as.numeric(theData$sin_sshh_freq)
theData$niños_sinescuela_freq = as.numeric(theData$niños_sinescuela_freq)
theData$dep_economica_freq = as.numeric(theData$dep_economica_freq)
theData=na.omit(theData)
library(polycor)
corMatrix=polycor::hetcor(theData)$correlations
round(corMatrix,2)
## dañadas_freq hacinadas_freq sin_sshh_freq
## dañadas_freq 1.00 0.74 0.48
## hacinadas_freq 0.74 1.00 0.44
## sin_sshh_freq 0.48 0.44 1.00
## niños_sinescuela_freq 0.75 0.97 0.45
## dep_economica_freq 0.55 0.69 0.60
## niños_sinescuela_freq dep_economica_freq
## dañadas_freq 0.75 0.55
## hacinadas_freq 0.97 0.69
## sin_sshh_freq 0.45 0.60
## niños_sinescuela_freq 1.00 0.70
## dep_economica_freq 0.70 1.00
library(ggcorrplot)
## Loading required package: ggplot2
ggcorrplot(corMatrix)
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(corMatrix)
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix)
## Overall MSA = 0.79
## MSA for each item =
## dañadas_freq hacinadas_freq sin_sshh_freq
## 0.94 0.72 0.78
## niños_sinescuela_freq dep_economica_freq
## 0.71 0.87
#nuestras varaibles obtienen al menos más de 0.7 en sus valores KMO, lo que significa que aún es posible hacer un analisis factorial
cortest.bartlett(corMatrix,n=nrow(theData))$p.value>0.05
## [1] FALSE
library(matrixcalc)
is.singular.matrix(corMatrix)
## [1] FALSE
#No es matriz singular, entonces aún apto para el AFE
theData= na.omit(theData)
fa.parallel(theData, fa = 'fa',correct = T,plot = F)
## Parallel analysis suggests that the number of factors = 3 and the number of components = NA
#Sugiere 3 factores
library(GPArotation)
##
## Attaching package: 'GPArotation'
## The following objects are masked from 'package:psych':
##
## equamax, varimin
resfa <- fa(theData,
nfactors = 3,
cor = 'mixed',
rotate = "varimax",
fm="minres")
print(resfa$loadings)
##
## Loadings:
## MR1 MR2 MR3
## dañadas_freq 0.584 0.350 0.509
## hacinadas_freq 0.912 0.311 0.198
## sin_sshh_freq 0.180 0.716 0.251
## niños_sinescuela_freq 0.911 0.321 0.209
## dep_economica_freq 0.520 0.709
##
## MR1 MR2 MR3
## SS loadings 2.306 1.337 0.405
## Proportion Var 0.461 0.267 0.081
## Cumulative Var 0.461 0.729 0.810
fa.diagram(resfa,main = "Resultados del EFA")
#son las cargas factoriales
sort(resfa$communality)
## sin_sshh_freq dañadas_freq dep_economica_freq
## 0.6078186 0.7231862 0.7721888
## hacinadas_freq niños_sinescuela_freq
## 0.9677779 0.9772057
#la proporción de los factores explicadas segun cada variable, estan ordenadas de menor a mayor; es decir, desde las que menos aportaron a las que más aportaron a los factores.
sort(resfa$complexity)
## hacinadas_freq niños_sinescuela_freq sin_sshh_freq
## 1.332739 1.360698 1.381837
## dep_economica_freq dañadas_freq
## 1.834380 2.631270
#explica que variable contribuyen a la construccion de más de un factor y va de menor a mayor
resfa$TLI
## [1] 1.001083
#indica un buen ajuste
resfa$rms
## [1] 1.251842e-08
#indica un buen ajuste
resfa$RMSEA
## NULL
resfa$BIC
## NULL
as.data.frame(resfa$scores)%>%head()
## MR1 MR2 MR3
## 9 0.1107457 -0.1481547 -0.196319935
## 10 -0.2049169 -0.5307458 -0.006245897
## 11 -0.1975980 -0.4389242 -0.072285022
## 12 -0.2149867 -0.4990121 -0.009770240
## 13 -0.2085193 -0.4883763 0.022431770
## 14 -0.2007101 -0.4258990 -0.033819874