-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.R
44 lines (33 loc) · 1.19 KB
/
syntax.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
# Load 'lavaan' package
library("lavaan")
# Import data as .csv
data <- read.csv("data.csv")
# Define doubly latent model. Note: NA*variable allows the variable
# to be freely estimated. Here, the first factor loading is not
# constrained to 1 by default, but freely estimated. c1*variable
# allows for a constraint to be placed on that variable. The factor
# loadings for the 3 observed variables are constrained to be equal
# across L1 and L2. The L1 factor variance is fixed to 1, while the
# L2 factor variance is freely estimated. Residual variances of the
# 3 observed variables are estimated at both L1 and L2 (var ~~ var).
model <- '
level: 1
Factor_w =~ NA*read + c1*read + c2*math + c3*science
read ~~ read
math ~~ math
science ~~ science
Factor_w ~~ 1*Factor_w
level: 2
Factor_b =~ NA*read + c1*read + c2*math + c3*science
read ~~ read
math ~~ math
science ~~ science
Factor_b ~~ Factor_b
'
# Estimate the model using maximum likelihood with robust standard errors
fit <- sem(model = model, data = data, cluster = "school_ID",
estimator = "ml", se = "robust.huber.white")
# Examine fit statistics
fitMeasures(fit, fit.measures = "all")
# Examine parameter estimates
summary(fit)