Skip to content

Commit 3dbd329

Browse files
20260319 - lavaan syntax
1 parent 3f0ca9c commit 3dbd329

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

sem.qmd

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,88 @@ longitudinalMI <- read.csv("./data/Bliese-Ployhart-2002-indicators-1.csv")
6464

6565
<https://isaactpetersen.github.io/Principles-Psychological-Assessment/structural-equation-modeling.html#sec-semModelExample-sem>
6666

67+
# `lavaan` Syntax {#sec-lavaanSyntax}
68+
69+
Adapted from <https://osf.io/2y67f/files/q2prk>
70+
71+
- New lines should be included for each new "equation"
72+
- `=~` creates reflective latent variables (is measured by)
73+
- `latent =~ variable1 + variable2` ...
74+
- You name it `=~` names from the dataset
75+
- Model is that latent variable influences the manifest variables
76+
- `<~` creates formative latent variables (is influenced by)
77+
- `latent <~ variable1 + variable2`
78+
- You name it `<~` names from the dataset
79+
- Model is that latent variable is influenced by the manifest variables
80+
- `~` indicates regression
81+
- `Y ~ X`
82+
- Model is that `Y` is influenced by `X` (i.e., `X` predicts `Y`)
83+
- Names need to be defined in dataset or previously defined latents
84+
- `~~` indicates covariance (i.e., unstandardized correlation; it is a correlation, i.e., standardized covariance, if the variables—or the coefficient—are standardized)
85+
- Does not matter which order you write it in
86+
- Names need to be defined in dataset or previously defined latents
87+
- You can set specific variances by using:
88+
- `variable ~~ NUMBER*variable`
89+
For instance, you can standardize the variance for a variable by fixing its variance to 1:
90+
- `variable ~~ 1*variable`
91+
- `~ 1` indicates intercept
92+
- `variable ~ 1`
93+
- To center a variable, you can set its intercept to 0:
94+
- `variable ~~ 0*1`
95+
96+
Factor loadings for reflective latent factors:
97+
98+
```r
99+
latent1 ~ x1 + x2 + x3
100+
latent2 ~ label1*x1 + label2*x2 + label3*x3 # parameter labels
101+
latent1 ~ NA*x1 + x2 + x3 # free factor loading of first indicator (it is set to 1 by default)
102+
```
103+
104+
Factor loadings for formative latent factors:
105+
106+
```r
107+
latent1 <~ x1 + x2 + x3
108+
latent2 <~ label1*x1 + label2*x2 + label3*x3 # parameter labels
109+
```
110+
111+
Regression paths:
112+
113+
```r
114+
y1 ~ x1 + x2
115+
y2 ~ label1*x1 + label2*x2 # parameter labels
116+
```
117+
118+
Covariance paths:
119+
120+
```r
121+
y1 ~~ y2
122+
x1 ~~ label1*x2 # parameter labels
123+
y1 + y2 ~~ x1 + x2
124+
```
125+
126+
Variances:
127+
128+
```r
129+
y1 ~~ y1
130+
y2 ~~ label1*y2 # parameter labels
131+
x1 ~~ 1*x1 # fix variance to 1
132+
```
133+
134+
Intercepts:
135+
136+
```r
137+
y1 ~~ 1
138+
y2 ~~ label1*1 # parameter labels
139+
x1 ~~ 0*1 # fix intercept to 0
140+
```
141+
142+
Parameter labels for multiple groups (have as many labels as there are groups):
143+
144+
```r
145+
latent1 ~ c(label1a, label1b)*x1 + c(label2a, label2b)*x2 + c(label3a, label3b)*x3 # allow parameters to differ across groups
146+
latent2 ~ c(label1, label1)*x1 + c(label2, label2)*x2 + c(label3, label3)*x3 # constrain parameters to be the same across groups
147+
```
148+
67149
# Plot Observed Growth Curves
68150

69151
## Spaghetti Plot

0 commit comments

Comments
 (0)