#paquetes que se necesitan.
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
# usage
packages <- c("parameters","apa","haven","ggplot2","ggpubr",
"gridExtra","apaTables", "reshape", "GPArotation", "mvtnorm", "psych", "psychometric", "lavaan", "nFactors", "semPlot", "lavaan", "MVN", "semTools")
ipak(packages)
## Loading required package: parameters
## Warning: package 'parameters' was built under R version 4.0.5
## Loading required package: apa
## Warning: package 'apa' was built under R version 4.0.5
## Loading required package: haven
## Loading required package: ggplot2
## Loading required package: ggpubr
## Warning: package 'ggpubr' was built under R version 4.0.3
## Loading required package: gridExtra
## Loading required package: apaTables
## Warning: package 'apaTables' was built under R version 4.0.5
## Loading required package: reshape
## Loading required package: GPArotation
## Warning: package 'GPArotation' was built under R version 4.0.3
## Loading required package: mvtnorm
## Loading required package: psych
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
## Loading required package: psychometric
## Warning: package 'psychometric' was built under R version 4.0.5
## Loading required package: multilevel
## Warning: package 'multilevel' was built under R version 4.0.5
## Loading required package: nlme
## Loading required package: MASS
##
## Attaching package: 'psychometric'
## The following object is masked from 'package:psych':
##
## alpha
## The following object is masked from 'package:ggplot2':
##
## alpha
## Loading required package: lavaan
## This is lavaan 0.6-7
## lavaan is BETA software! Please report any bugs.
##
## Attaching package: 'lavaan'
## The following object is masked from 'package:psych':
##
## cor2cov
## Loading required package: nFactors
## Warning: package 'nFactors' was built under R version 4.0.5
## Loading required package: lattice
##
## Attaching package: 'nFactors'
## The following object is masked from 'package:lattice':
##
## parallel
## Loading required package: semPlot
## Warning: package 'semPlot' was built under R version 4.0.5
## Registered S3 methods overwritten by 'lme4':
## method from
## cooks.distance.influence.merMod car
## influence.merMod car
## dfbeta.influence.merMod car
## dfbetas.influence.merMod car
## Loading required package: MVN
## Warning: package 'MVN' was built under R version 4.0.3
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
## sROC 0.1-2 loaded
## Loading required package: semTools
##
## ###############################################################################
## This is semTools 0.5-3
## All users of R (or SEM) are invited to submit functions or ideas for functions.
## ###############################################################################
##
## Attaching package: 'semTools'
## The following object is masked from 'package:psych':
##
## skew
## The following object is masked from 'package:parameters':
##
## kurtosis
## parameters apa haven ggplot2 ggpubr gridExtra
## TRUE TRUE TRUE TRUE TRUE TRUE
## apaTables reshape GPArotation mvtnorm psych psychometric
## TRUE TRUE TRUE TRUE TRUE TRUE
## lavaan nFactors semPlot lavaan MVN semTools
## TRUE TRUE TRUE TRUE TRUE TRUE
library(nFactors)
#--------------------------------------------------------------------------
# CONTENIDO
# 0. Entorno de trabajo
# 1. Cargar base
# 2. Ajuste del modelo
# 2.1 Normalizacion
# 2.2 Prueba Kaiser-Meyer-Olkin
# 3. Factorial
# 4. Interpretación
#--------------------------------------------------------------------------
#0. Entorno de trabajo
rm(list=ls())
graphics.off()
library(foreign)
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.0.3
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(ggplot2)
library(psych)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:MASS':
##
## select
## The following object is masked from 'package:nlme':
##
## collapse
## The following object is masked from 'package:reshape':
##
## rename
## The following object is masked from 'package:gridExtra':
##
## combine
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
La base de datos se puede descargar aquí pacientes.csv
pacientes<-read.csv("E:/pacientes.csv", sep=",",header = TRUE)
View(pacientes)
matriz_correlaciones<-cor(pacientes, use = "pairwise.complete.obs")
matriz_correlaciones
## hrs_ejercicio glucosa peso presion
## hrs_ejercicio 1.00000000 0.4104293 0.76485171 0.09928331
## glucosa 0.41042932 1.0000000 0.71368010 0.19943737
## peso 0.76485171 0.7136801 1.00000000 0.32805124
## presion 0.09928331 0.1994374 0.32805124 1.00000000
## dias_dolor_cabeza -0.11831738 0.1830099 0.09655858 0.72525552
## dias_dolor_cabeza
## hrs_ejercicio -0.11831738
## glucosa 0.18300987
## peso 0.09655858
## presion 0.72525552
## dias_dolor_cabeza 1.00000000
#2. Ajuste del modelo
#2.1 Normalización
norm01 <- function(x){(x-min(x))/(max(x)-min(x))}
pacientes_norm<-data.frame(apply(pacientes,2,norm01))
apply(pacientes_norm, 2, max)%>%round(2)
## hrs_ejercicio glucosa peso presion
## 1 1 1 1
## dias_dolor_cabeza
## 1
#2.2 Prueba Kaiser-Meyer-Olkin
# La prueba de 0 a 1 e indica si las correlaciones entre variables son
# suficientemente pequñas. Valores menores a 0.5 no deben ser incluidos
KMO(pacientes_norm)
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = pacientes_norm)
## Overall MSA = 0.5
## MSA for each item =
## hrs_ejercicio glucosa peso presion
## 0.55 0.52 0.50 0.46
## dias_dolor_cabeza
## 0.47
#2.3 Numero de factores (Grafica de sedimentacion)
corr<-round(cor(pacientes_norm),2)
aucor=eigen(corr)
plot(1:5,aucor$values,type="l",xlab="Factores",ylab="Autovalores")
#n factors ASI
library(nFactors)
results_nfactorASI<-n_factors(pacientes, rotate = "varimax", fm = "mle", n = NULL)
plot(results_nfactorASI)
#3. Factorial
fit <- factanal(pacientes, 2, rotation="varimax")
fit
##
## Call:
## factanal(x = pacientes, factors = 2, rotation = "varimax")
##
## Uniquenesses:
## hrs_ejercicio glucosa peso presion
## 0.376 0.477 0.005 0.405
## dias_dolor_cabeza
## 0.005
##
## Loadings:
## Factor1 Factor2
## hrs_ejercicio 0.786
## glucosa 0.689 0.220
## peso 0.986 0.150
## presion 0.219 0.740
## dias_dolor_cabeza 0.996
##
## Factor1 Factor2
## SS loadings 2.116 1.616
## Proportion Var 0.423 0.323
## Cumulative Var 0.423 0.746
##
## Test of the hypothesis that 2 factors are sufficient.
## The chi square statistic is 12.3 on 1 degree of freedom.
## The p-value is 0.000454
#4. Interpretacion
load <- fit$loadings[,1:2]
load
## Factor1 Factor2
## hrs_ejercicio 0.78636910 -0.07656247
## glucosa 0.68857506 0.22025637
## peso 0.98612802 0.15025207
## presion 0.21890756 0.73987305
## dias_dolor_cabeza -0.05383748 0.99604341
modelo_varimax<-fa(matriz_correlaciones,nfactors = 2,rotate = "varimax",
fa="minres")
## 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
fa.diagram(modelo_varimax)