12 Workshop 15: Factor Analysis (and Reliability Analysis)
Aims:
- To practice using factor analysis to determine the number of factors in a data set.
- To conduct a reliability analysis using Cronbach’s Alpha.
12.1 Exercise 1: Import and Prepare Data
# Set the working directory -WD- so R knows where the data lives. Do this by going Session > Set working directory > Choose directory
# You can check the working directory...
getwd()
#Before doing anything, need to make sure the right packages are installed and open. We will use all of these......
library(tidyverse)
library(psych)
#Import the data
mydata <- read_csv("study_perception.csv")12.2 Exercise Two
#### Understanding your data
str(mydata) #we can use this to check all of the variables in the file are numeric
#any categorical/factor variables would need to be removed.
### A quick summary of the data
summary(mydata)
# Let's create a correlation matrix just to get an understanding of our data file
# we also need to create the correlation matrix to use later
corr_matrix <- round(cor(mydata, use = "pairwise.complete.obs"), 3)
view(corr_matrix)12.3 Exercise Three
#### Assess the need for factor analysis
### assumptions
# Kaiser-Meyer-Olkin (KMO) Test - Measures Sampling Adequacy
kmo_result <- KMO(corr_matrix)
print(kmo_result) # KMO should be > 0.6 for FA to be appropriate
# Bartlett’s Test of Sphericity - Checks if correlation matrix is an identity matrix
bartlett_result <- cortest.bartlett(corr_matrix, n = nrow(mydata))
print(bartlett_result) # p-value should be < 0.05 for FA to be suitable12.4 Exercise Four
#### Determine the number of factors.
# Compute Eigenvalues
eigen_values <- eigen(corr_matrix)$values
print(eigen_values) # Kaiser’s criterion: Keep factors with eigenvalues > 1
# Scree Plot
fa.parallel(mydata, fa = "fa", n.iter = 100, show.legend = TRUE)12.5 Exercise Five
##### Interpretation of factors.
# Perform Factor Analysis with Varimax Rotation
fa_result <- fa(mydata, nfactors = 2, rotate = "varimax", fm = "ml") # Adjust nfactors
# Print Factor Loadings (Rotated Component Matrix)
print(fa_result$loadings, cutoff = 0.3) # Show only loadings > 0.3
# Visualizing Loadings
fa.diagram(fa_result) # Shows factor structure in a diagram12.6 Exercise Six
#### Reliability Analysis
# select items and put them onto a scale.
# Group items based on factor loadings
colnames(mydata)
scale_1 <- mydata %>% select(starts_with("s1_"))
scale_2 <- mydata %>% select(starts_with("s2_"))
# Compute Cronbach's Alpha
alpha(scale_1) # For Factor 1
alpha(scale_2) # For Factor 2Well Done. You have reached the end of the workshop