knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
options(scipen = 999)
knitr::opts_chunk$set(echo = TRUE)

Terminology

Terminology

library(lavaan)
library(semPlot)
HS.model <- ' visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9 '

fit <- cfa(HS.model, data = HolzingerSwineford1939)
semPaths(fit,
         whatLabels = "std",
         edge.label.cex = 1)

What is EFA?

Directionality

library(lavaan)
library(semPlot)
HS.model <- ' visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9 '

fit <- cfa(HS.model, data = HolzingerSwineford1939)
semPaths(fit,
         whatLabels = "std",
         edge.label.cex = 1)

Why use EFA?

The Example Data

The Example Data

library(rio)
library(psych)
master <- import("data/lecture_efa.csv")
head(master)

Steps to Analysis

How many factors do I use?

Kaiser criterion

Scree plots

knitr::include_graphics("pictures/scree.png")

Parallel analysis

Finding factors/components

number_items <- fa.parallel(master, #data frame
                            fm="ml", #math
                            fa="fa") #only efa

Eigenvalues

sum(number_items$fa.values > 1)
sum(number_items$fa.values > .7)

Simple structure

Rotation

knitr::include_graphics("pictures/rotate.png")

Rotation

Simple structure/solution

Run an EFA

EFA_fit <- fa(master, #data
              nfactors = 2, #number of factors
              rotate = "oblimin", #rotation
              fm = "ml") #math

Look at the results

EFA_fit

Item 23

EFA_fit2 <- fa(master[ , -23], #data
              nfactors = 2, #number of factors
              rotate = "oblimin", #rotation
              fm = "ml") #math

EFA_fit2

Plots of the results

 fa.plot(EFA_fit2, 
     labels = colnames(master[ , -23]))

Plots of the results

fa.diagram(EFA_fit2)

Adequate solution

Fit statistics

EFA_fit2$rms #Root mean square of the residuals
EFA_fit2$RMSEA #root mean squared error of approximation
EFA_fit2$TLI #tucker lewis index
1 - ((EFA_fit2$STATISTIC-EFA_fit2$dof)/
       (EFA_fit2$null.chisq-EFA_fit2$null.dof)) #CFI 

Reliability

factor1 = c(1:7, 9:10, 12:16, 18:22)
factor2 = c(8, 11, 17)
##we use the psych::alpha to make sure that R knows we want the alpha function from the psych package.
##ggplot2 has an alpha function and if we have them both open at the same time
##you will sometimes get a color error without this :: information. 
psych::alpha(master[, factor1], check.keys = T)
psych::alpha(master[, factor2], check.keys = T)

Interpretation

Factor 1:

  1. Statistics makes me cry
  2. My friends will think I'm stupid for not being able to cope with R
  3. Standard deviations excite me
  4. I dream that Pearson is attacking me with correlation coefficients
  5. I don't understand statistics
  6. I have little experience of computers
  7. All computers hate me
  8. My friends are better at statistics than me
  9. Computers are useful only for playing games
  10. People try to tell you that R makes statistics easier to understand but it doesn't
  11. I worry that I will cause irreparable damage because of my incompetence with computers
  12. Computers have minds of their own and deliberately go wrong whenever I use them
  13. Computers are out to get me
  14. I weep openly at the mention of central tendency
  15. R always crashes when I try to use it
  16. Everybody looks at me when I use R
  17. I can't sleep for thoughts of eigenvectors
  18. I wake up under my duvet thinking that I am trapped under a normal distribution
  19. My friends are better at R than I am

Factor 2:

  1. I have never been good at mathematics
  2. I did badly at mathematics at school
  3. I slip into a coma whenever I see an equation

Bad:

  1. If I'm good at statistics my friends will think I'm a nerd

Wrapping Up



doomlab/learnSEM documentation built on Jan. 25, 2024, 2 p.m.