-
Notifications
You must be signed in to change notification settings - Fork 0
/
35-cfa-lavaan.R
102 lines (64 loc) · 3.11 KB
/
35-cfa-lavaan.R
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
######################################################################
#################### Confirmatory Factor Analysis ####################
######################################################################
# Date: TODAY'S DATE
# By: *INSERT NAME HERE*
# Description: PROJECT DISCRIPTION
# Version of R used: CURRENT VERSION OF R
#####################################
### Import data & load libraries ####
#####################################
## Install the required script packages if not yet installed
# Install pacman, jmv, & reghelper package if necessary
if(!"pacman" %in% rownames(installed.packages())) install.packages("pacman")
pacman::p_load(rio, dplyr, lavaan)
## load data
dat <- import(file.choose())
# check to see that you loaded the correct dataset
View(dat)
# list variables in dataset
glimpse(dat)
# NB: Alt + Shift + K --> will bring up keyboard shortcuts
###############################
########## Data Prep ##########
###############################
## CFA instructions for lavaan can be found: http://lavaan.ugent.be/tutorial/cfa.html
## cheatsheet here too: https://github.com/jeromyanglim/lavaan-examples/blob/master/cheat-sheet-lavaan/cheat-sheet-lavaan.md
# create new dataframes for just the focal CFA
dat1 = dat %>%
dplyr::select(item1, item2, item3, item4, item5)
# create more dataframes if multiple CFAs
dat2 = dat %>%
dplyr::select(item1, item2, item3, item4, item5, item6, item7, item8, item9, item10)
# check dataframes
glimpse(dat1)
glimpse(dat2)
#### lavaan ----
## cereate models
# specify model
five_item_model <- ' factor1 =~ item1 + item2 + item3 + item4 + item5 '
two_factor_model <- ' factor1 =~ item1 + item2 + item3 + item4 + item5
factor2 =~ item6 + item7 + item8 + item9 + item10 '
saturated_model <- ' factor1 =~ item1 + item2 + item3 + item4 + item5 +
item6 + item7 + item8 + item9 + item10 '
## fit model
# might take a minute or two to run depending on amount of resamples
fit_five_item <- cfa(five_item_model, data = dat1, mimic = 'Mplus')
fit_two_factor_model <- cfa(two_factor_model, data = dat2, mimic = 'Mplus')
fit_saturated_model <- cfa(saturated_model, data = dat2, mimic = 'Mplus')
## fit measures ----
# fit summary for 1 factor, 5 item model
summary(fit_five_item, fit.measures=TRUE)
round(fitMeasures(fit_five_item)[c('chisq', 'df', 'pvalue', 'cfi', 'ifi', 'tli', 'rmsea', 'srmr')], 3)
# fitMeasures(fit_five_item) # could also run this
# fit summary for 2 factor model
summary(fit_two_factor_model, fit.measures=TRUE)
round(fitMeasures(fit_two_factor_model)[c('chisq', 'df', 'pvalue', 'cfi', 'ifi', 'tli', 'rmsea', 'srmr')], 3)
# fit summary for fully saturated, 1 factor model
summary(fit_saturated_model, fit.measures=TRUE)
round(fitMeasures(fit_saturated_model)[c('chisq', 'df', 'pvalue', 'cfi', 'ifi', 'tli', 'rmsea', 'srmr')], 3)
## compare models, if needed
# produces Chi-square difference test
# can only compare models with the same items but different df
# compare 2 factor (10 item) model vs fully saturated model
anova(fit_two_factor_model, fit_saturated_model)