Skip to content

Commit 4b87511

Browse files
20260117 - SEM figures
1 parent ddb5edf commit 4b87511

File tree

1 file changed

+134
-125
lines changed

1 file changed

+134
-125
lines changed

sem.qmd

Lines changed: 134 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ ggplot(
9494
y = score,
9595
group = id)) +
9696
geom_line() +
97-
scale_x_continuous(
98-
breaks = 1:4,
99-
name = "Timepoint") +
100-
scale_y_continuous(
101-
name = "Score")
97+
labs(
98+
x = "Timepoint",
99+
y = "Score"
100+
) +
101+
theme_classic()
102102
```
103103

104104
# Latent Growth Curve Model {#sec-lgcm}
@@ -286,29 +286,10 @@ lavaangui::plot_lavaan(lgcm1_fit)
286286

287287
#### Prototypical Growth Curve
288288

289-
Calculated from intercept and slope parameters:
290-
291289
```{r}
292290
lgcm1_intercept <- coef(lgcm1_fit)["intercept~1"]
293291
lgcm1_slope <- coef(lgcm1_fit)["slope~1"]
294292
295-
ggplot() +
296-
xlab("Timepoint") +
297-
ylab("Score") +
298-
scale_x_continuous(
299-
limits = c(0, 3),
300-
labels = 1:4) +
301-
scale_y_continuous(
302-
limits = c(0, 5)) +
303-
geom_abline(
304-
mapping = aes(
305-
slope = lgcm1_slope,
306-
intercept = lgcm1_intercept))
307-
```
308-
309-
Calculated manually:
310-
311-
```{r}
312293
timepoints <- 4
313294
314295
newData <- expand.grid(
@@ -321,45 +302,32 @@ newData$predictedValue[which(newData$time == 4)] <- lgcm1_intercept + (timepoint
321302
322303
ggplot(
323304
data = newData,
324-
mapping = aes(x = time, y = predictedValue)) +
325-
xlab("Timepoint") +
326-
ylab("Score") +
327-
scale_y_continuous(
328-
limits = c(0, 5)) +
329-
geom_line()
305+
mapping = aes(
306+
x = time,
307+
y = predictedValue)) +
308+
geom_line(
309+
linewidth = 1
310+
) +
311+
coord_cartesian(
312+
ylim = c(0, 5)) +
313+
labs(
314+
x = "Timepoint",
315+
y = "Score"
316+
) +
317+
theme_classic()
330318
```
331319

332320
#### Individuals' Growth Curves
333321

334-
Calculated from intercept and slope parameters:
335-
336322
```{r}
337-
newData <- as.data.frame(predict(lgcm1_fit))
338-
newData$id <- row.names(newData)
323+
newData1 <- as.data.frame(predict(lgcm1_fit))
324+
newData1$id <- row.names(newData1)
339325
340-
ggplot(
341-
data = newData) +
342-
xlab("Timepoint") +
343-
ylab("Score") +
344-
scale_x_continuous(
345-
limits = c(0, 3),
346-
labels = 1:4) +
347-
scale_y_continuous(
348-
limits = c(-10, 20)) +
349-
geom_abline(
350-
mapping = aes(
351-
slope = slope,
352-
intercept = intercept))
353-
```
354-
355-
Calculated manually:
356-
357-
```{r}
358-
newData$t1 <- newData$intercept
359-
newData$t4 <- newData$intercept + (timepoints - 1)*newData$slope
326+
newData1$t1 <- newData1$intercept
327+
newData1$t4 <- newData1$intercept + (timepoints - 1)*newData1$slope
360328
361329
newData2 <- pivot_longer(
362-
newData,
330+
newData1,
363331
cols = c(t1, t4)) %>%
364332
select(-intercept, -slope)
365333
@@ -369,39 +337,44 @@ newData2$time[which(newData2$name == "t4")] <- 4
369337
370338
ggplot(
371339
data = newData2,
372-
mapping = aes(x = time, y = value, group = factor(id))) +
373-
xlab("Timepoint") +
374-
ylab("Score") +
375-
scale_y_continuous(
376-
limits = c(-10, 20)) +
377-
geom_line()
340+
mapping = aes(
341+
x = time,
342+
y = value,
343+
group = factor(id))) +
344+
geom_line() +
345+
labs(
346+
x = "Timepoint",
347+
y = "Score"
348+
) +
349+
theme_classic()
378350
```
379351

380352
#### Individuals' Trajectories Overlaid with Prototypical Trajectory
381353

382354
```{r}
383-
newData <- as.data.frame(predict(lgcm1_fit))
384-
newData$id <- row.names(newData)
385-
386-
ggplot(
387-
data = newData) +
388-
xlab("Timepoint") +
389-
ylab("Score") +
390-
scale_x_continuous(
391-
limits = c(0, 3),
392-
labels = 1:4) +
393-
scale_y_continuous(
394-
limits = c(-10, 20)) +
395-
geom_abline(
355+
ggplot() +
356+
geom_line( # individuals' trajectories
357+
data = newData2,
396358
mapping = aes(
397-
slope = slope,
398-
intercept = intercept)) +
399-
geom_abline(
359+
x = time,
360+
y = value,
361+
group = factor(id)),
362+
color = "gray",
363+
alpha = 0.5
364+
) +
365+
geom_line( # prototypical trajectory
366+
data = newData,
400367
mapping = aes(
401-
slope = lgcm1_slope,
402-
intercept = lgcm1_intercept),
403-
color = "blue",
404-
linewidth = 2)
368+
x = time,
369+
y = predictedValue
370+
),
371+
linewidth = 2
372+
) +
373+
labs(
374+
x = "Timepoint",
375+
y = "Score"
376+
) +
377+
theme_classic()
405378
```
406379

407380
## Latent Basis Growth Curve Model {#sec-latentBasisLGCM}
@@ -597,12 +570,19 @@ newData$predictedValue <- lbgcm1_intercept + lbgcm1_slope * newData$slopeloading
597570
598571
ggplot(
599572
data = newData,
600-
mapping = aes(x = time, y = predictedValue)) +
601-
xlab("Timepoint") +
602-
ylab("Score") +
603-
scale_y_continuous(
604-
limits = c(0, 5)) +
605-
geom_line()
573+
mapping = aes(
574+
x = time,
575+
y = predictedValue)) +
576+
geom_line(
577+
linewidth = 1
578+
) +
579+
coord_cartesian(
580+
ylim = c(0, 5)) +
581+
labs(
582+
x = "Timepoint",
583+
y = "Score"
584+
) +
585+
theme_classic()
606586
```
607587

608588
#### Individuals' Growth Curves
@@ -634,12 +614,16 @@ individual_trajectories <- person_factors %>%
634614
635615
ggplot(
636616
data = individual_trajectories,
637-
mapping = aes(x = time, y = value, group = factor(id))) +
638-
xlab("Timepoint") +
639-
ylab("Score") +
640-
scale_y_continuous(
641-
limits = c(-10, 20)) +
642-
geom_line()
617+
mapping = aes(
618+
x = time,
619+
y = value,
620+
group = factor(id))) +
621+
geom_line() +
622+
labs(
623+
x = "Timepoint",
624+
y = "Score"
625+
) +
626+
theme_classic()
643627
```
644628

645629
#### Individuals' Trajectories Overlaid with Prototypical Trajectory
@@ -652,6 +636,8 @@ ggplot() +
652636
x = time,
653637
y = value,
654638
group = id),
639+
color = "gray",
640+
alpha = 0.5
655641
) +
656642
geom_line( # prototypical trajectory
657643
data = newData,
@@ -660,7 +646,12 @@ ggplot() +
660646
y = predictedValue),
661647
color = "blue",
662648
linewidth = 2
663-
)
649+
) +
650+
labs(
651+
x = "Timepoint",
652+
y = "Score"
653+
) +
654+
theme_classic()
664655
```
665656

666657
## Quadratic Growth Curve Model {#sec-quadraticLGCM}
@@ -882,16 +873,19 @@ ggplot(
882873
mapping = aes(
883874
x = time,
884875
y = predictedValue)) +
885-
xlab("Timepoint") +
886-
ylab("Score") +
887-
scale_y_continuous(
888-
limits = c(0, 5)) +
889876
geom_smooth(
890877
method = "lm",
891878
formula = y ~ x + I(x^2),
892879
se = FALSE,
893-
linewidth = 0.5
894-
)
880+
linewidth = 1
881+
) +
882+
coord_cartesian(
883+
ylim = c(0, 5)) +
884+
labs(
885+
x = "Timepoint",
886+
y = "Score"
887+
) +
888+
theme_classic()
895889
```
896890

897891
#### Individuals' Growth Curves
@@ -928,28 +922,29 @@ ggplot(
928922
x = time,
929923
y = value,
930924
group = factor(id))) +
931-
xlab("Timepoint") +
932-
ylab("Score") +
933-
scale_y_continuous(
934-
limits = c(-10, 20)) +
935925
geom_smooth(
936926
method = "lm",
937927
formula = y ~ x + I(x^2),
938928
se = FALSE,
939929
linewidth = 0.5
940-
)
930+
) +
931+
labs(
932+
x = "Timepoint",
933+
y = "Score"
934+
) +
935+
theme_classic()
941936
```
942937

943938
#### Individuals' Trajectories Overlaid with Prototypical Trajectory
944939

945940
```{r}
946-
ggplot(
947-
data = individual_trajectories,
948-
mapping = aes(
949-
x = time,
950-
y = value,
951-
group = id)) +
941+
ggplot() +
952942
geom_smooth( # individuals' model-implied trajectories
943+
data = individual_trajectories,
944+
mapping = aes(
945+
x = time,
946+
y = value,
947+
group = id),
953948
method = "lm",
954949
formula = y ~ x + I(x^2),
955950
se = FALSE,
@@ -969,10 +964,10 @@ ggplot(
969964
linewidth = 2
970965
) +
971966
labs(
972-
x = "Age (years)",
973-
y = "Depression Symptoms"
967+
x = "Timepoint",
968+
y = "Score"
974969
) +
975-
theme_classic()
970+
theme_classic()
976971
```
977972

978973
## Spline (Piecewise) Growth Curve Model {#sec-splineLGCM}
@@ -1192,11 +1187,16 @@ ggplot(
11921187
mapping = aes(
11931188
x = time,
11941189
y = predictedValue)) +
1195-
xlab("Timepoint") +
1196-
ylab("Score") +
1197-
scale_y_continuous(
1198-
limits = c(0, 5)) +
1199-
geom_line()
1190+
geom_line(
1191+
linewidth = 1
1192+
) +
1193+
coord_cartesian(
1194+
ylim = c(0, 5)) +
1195+
labs(
1196+
x = "Timepoint",
1197+
y = "Score"
1198+
) +
1199+
theme_classic()
12001200
```
12011201

12021202
#### Individuals' Growth Curves
@@ -1233,11 +1233,12 @@ ggplot(
12331233
x = time,
12341234
y = value,
12351235
group = factor(id))) +
1236-
xlab("Timepoint") +
1237-
ylab("Score") +
1238-
scale_y_continuous(
1239-
limits = c(-10, 20)) +
1240-
geom_line()
1236+
geom_line() +
1237+
labs(
1238+
x = "Timepoint",
1239+
y = "Score"
1240+
) +
1241+
theme_classic()
12411242
```
12421243

12431244
#### Individuals' Trajectories Overlaid with Prototypical Trajectory
@@ -1250,6 +1251,9 @@ ggplot() +
12501251
x = time,
12511252
y = value,
12521253
group = id),
1254+
linewidth = 0.5,
1255+
color = "gray",
1256+
alpha = 0.30
12531257
) +
12541258
geom_line( # prototypical trajectory
12551259
data = newData,
@@ -1258,7 +1262,12 @@ ggplot() +
12581262
y = predictedValue),
12591263
color = "blue",
12601264
linewidth = 2
1261-
)
1265+
) +
1266+
labs(
1267+
x = "Timepoint",
1268+
y = "Score"
1269+
) +
1270+
theme_classic()
12621271
```
12631272

12641273
## Saturated Growth Curve Model {#sec-saturatedGCM}

0 commit comments

Comments
 (0)