Repository navigation
Expand file tree
/
Copy pathfactor.R
More file actions
158 lines (127 loc) · 4.16 KB
/
Copy pathfactor.R
File metadata and controls
158 lines (127 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
library(tidyverse) # Pack of most used libraries for data science
library(summarytools) # Summary of the dataset
library(foreign) # Read SPSS files
library(nFactors) # Factor analysis
library(GPArotation) # GPA Rotation for Factor Analysis
library(psych) # Personality, psychometric, and psychological research
data = read.spss("data/example_fact.sav", to.data.frame = T)
head(data, 5)
View(data) # open in table
data = data |> column_to_rownames(var = "RespondentID")
str(data)
# skimr::skim(data)
print(dfSummary(data),
method = "render")
# random regression model
random = rchisq(nrow(data), ncol(data))
fake = lm(random ~ ., data = data)
standardized = rstudent(fake)
fitted = scale(fake$fitted.values)
hist(standardized)
qqnorm(standardized)
abline(0, 1)
plot(fitted, standardized)
abline(h=0, v=0)
corr_matrix = cor(data, method = "pearson")
cortest.bartlett(corr_matrix, n = nrow(data))
KMO(corr_matrix)
num_factors = fa.parallel(
x = data,
fm = "ml", # factor mathod = maximum likelihood
fa = "fa") # factor analysis
sum(num_factors$fa.values > 1) # Number of factors with eigenvalue > 1
sum(num_factors$fa.values > 0.7) # Number of factors with eigenvalue > 0.7
data_pca = princomp(data,
cor = TRUE) # standardizes your dataset before running a PCA
summary(data_pca)
plot(data_pca, type = "lines", npcs = 31)
# No rotation
data_factor = factanal(
data,
factors = 4, # change here the number of facotrs based on the EFA
rotation = "none",
scores = "regression",
fm = "ml"
)
# Rotation Varimax
data_factor_var = factanal(
data,
factors = 4,
rotation = "varimax", # orthogonal rotation (default)
scores = "regression",
fm = "ml"
)
# Rotation Oblimin
data_factor_obl = factanal(
data,
factors = 4,
rotation = "oblimin", # oblique rotation
scores = "regression",
fm = "ml"
)
print(data_factor_obl,
digits = 2,
cutoff = 0.3, # > 0.3 due to the sample size is higher than 350 observations.
sort = TRUE)
View(data_factor_obl$scores)
# write.csv(data_factor_obl$scores, "data/data_factor_obl_scores.csv", sep = "\t")
head(data_factor_obl$scores)
View(data_factor_obl$loadings)
# write.csv(data_factor_obl$loadings, "data/data_factor_obl_loadings.csv", sep = "\t")
head(data_factor_obl$loadings)
# define a plot function
plot_factor_loading <- function(data_factor,
f1 = 1,
f2 = 2,
method = "No rotation",
color = "blue") {
# Convert to numeric matrix (works for psych loadings objects)
L <- as.matrix(data_factor$loadings)
# Extract selected factors
df <- data.frame(item = rownames(L), x = L[, f1], y = L[, f2])
ggplot(df, aes(x = x, y = y, label = item)) +
geom_point() +
geom_text(color = color,
vjust = -0.5,
size = 3) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
coord_equal(xlim = c(-1, 1), ylim = c(-1, 1)) +
labs(
x = paste0("Factor ", f1),
y = paste0("Factor ", f2),
title = method
) +
theme_bw()
}
plot_factor_loading(
data_factor = data_factor, # model no rotation
f1 = 1, # Factor 1
f2 = 2, # Factor 2
method = "No rotation", # plot title
color = "blue"
)
plot_factor_loading(
data_factor = data_factor_var, # model varimax
f1 = 1, # Factor 1
f2 = 2, # Factor 2
method = "Varimax rotation",
color = "red"
)
plot_factor_loading(
data_factor = data_factor_var, # model oblimn
f1 = 1, # Factor 1
f2 = 2, # Factor 2
method = "Oblimin rotation",
color = "darkgreen"
)
# create all combinations
p12 <- plot_factor_loading(data_factor, 1, 2, method = "No rotation", color = "blue")
p13 <- plot_factor_loading(data_factor, 1, 3, method = "No rotation", color = "blue")
p14 <- plot_factor_loading(data_factor, 1, 4, method = "No rotation", color = "blue")
p23 <- plot_factor_loading(data_factor, 2, 3, method = "No rotation", color = "blue")
p24 <- plot_factor_loading(data_factor, 2, 4, method = "No rotation", color = "blue")
p34 <- plot_factor_loading(data_factor, 3, 4, method = "No rotation", color = "blue")
library(patchwork)
(p12 + p13 + p14) /
(p23 + p24 + p34)