This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
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(MASS)
## Warning: package 'MASS' was built under R version 4.4.1
##
## Adjuntando el paquete: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
library(readxl)
dataexamenfinal2 <- read_excel("C:/Users/JHOJAN/Downloads/dataexamenfinal.xlsx")
dataexamenfinal = dplyr::select(dataexamenfinal2, c(1,3,5,7,9))
dontselect <- c("PROVINCIA") # Columnas que no quieres seleccionar
select <- setdiff(names(dataexamenfinal), dontselect) # Seleccionar todas las columnas excepto "PROVINCIA"
theData <- dataexamenfinal[, select] # Aplicar la selección a datafinal2
theData <- as.data.frame(theData)
str(theData)
## 'data.frame': 196 obs. of 4 variables:
## $ Sí usa electricidad : num 574 161 124 14 90 65 255 921 16 33 ...
## $ Sí usa gas (balón GLP): num 10641 9917 4659 1536 6339 ...
## $ Sí usa carbón : num 176 289 58 26 33 26 335 218 4 4 ...
## $ Sí usa leña : num 8101 13117 5468 8808 11369 ...
library(polycor)
corMatrix=polycor::hetcor(theData)$correlations
round(corMatrix,2)
## Sí usa electricidad Sí usa gas (balón GLP) Sí usa carbón
## Sí usa electricidad 1.00 0.99 0.49
## Sí usa gas (balón GLP) 0.99 1.00 0.53
## Sí usa carbón 0.49 0.53 1.00
## Sí usa leña 0.29 0.34 0.42
## Sí usa leña
## Sí usa electricidad 0.29
## Sí usa gas (balón GLP) 0.34
## Sí usa carbón 0.42
## Sí usa leña 1.00
library(psych)
psych::KMO(corMatrix)
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix)
## Overall MSA = 0.56
## MSA for each item =
## Sí usa electricidad Sí usa gas (balón GLP) Sí usa carbón
## 0.52 0.53 0.81
## Sí usa leña
## 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)
resfa1 <- fa(theData,
nfactors = 2,
cor = 'mixed',
rotate = "varimax", #oblimin?
fm="minres")
print(resfa1$loadings)
##
## Loadings:
## MR1 MR2
## Sí usa electricidad 0.968 0.242
## Sí usa gas (balón GLP) 0.942 0.330
## Sí usa carbón 0.365 0.554
## Sí usa leña 0.130 0.667
##
## MR1 MR2
## SS loadings 1.974 0.919
## Proportion Var 0.494 0.230
## Cumulative Var 0.494 0.723
fa.diagram(resfa1,main = "Resultados del EFA")
library(GPArotation)
resfa2 <- fa(theData,
nfactors = 2,
cor = 'mixed',
rotate = "oblimin", #oblimin?
fm="minres")
print(resfa2$loadings)
##
## Loadings:
## MR1 MR2
## Sí usa electricidad 1.022
## Sí usa gas (balón GLP) 0.965
## Sí usa carbón 0.245 0.502
## Sí usa leña 0.706
##
## MR1 MR2
## SS loadings 2.037 0.757
## Proportion Var 0.509 0.189
## Cumulative Var 0.509 0.699
fa.diagram(resfa2,main = "Resultados del EFA")