You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using higher-order polynomials, we could specify contrast codes for time to reduce multicollinearity between the linear and quadratic growth factors: https://tdjorgensen.github.io/SEM-in-Ed-compendium/ch27.html#saturated-growth-model
222
+
223
+
```{r}
224
+
factorLoadings <- poly(
225
+
x = c(0,1,2,3), # times (can allow unequal spacing)
226
+
degree = 2)
227
+
228
+
factorLoadings
229
+
230
+
linearLoadings <- factorLoadings[,1]
231
+
quadraticLoadings <- factorLoadings[,2]
232
+
233
+
linearLoadings
234
+
quadraticLoadings
235
+
```
236
+
237
+
#### Fit Model
238
+
239
+
```{r}
240
+
quadraticGCM <- lmer(
241
+
math ~ female + ageYearsCentered + ageYearsCenteredSquared + female:ageYearsCentered + female:ageYearsCenteredSquared + (1 + ageYearsCentered | id), # random intercepts and linear slopes; fixed quadratic slopes; sex as a fixed-effect predictor of the intercepts and slopes
242
+
data = mydata,
243
+
REML = FALSE, #for ML
244
+
na.action = na.exclude,
245
+
control = lmerControl(optimizer = "bobyqa"))
246
+
247
+
summary(quadraticGCM)
248
+
```
249
+
250
+
This is equivalent to:
251
+
252
+
```{r}
253
+
quadraticGCM <- lmer(
254
+
math ~ female + ageYearsCentered + I(ageYearsCentered^2) + female:ageYearsCentered + female:I(ageYearsCentered^2) + (1 + ageYearsCentered | id), # random intercepts and slopes; sex as a fixed-effect predictor of the intercepts and slopes
#### Individuals' Trajectories Overlaid with Prototypical Trajectory
316
+
317
+
```{r}
318
+
ggplot(
319
+
data = mydata,
320
+
mapping = aes(
321
+
x = ageYears,
322
+
y = predictedValue,
323
+
group = factor(id))) +
324
+
xlab("Age (Years)") +
325
+
ylab("Math Score") +
326
+
geom_line() +
327
+
geom_line(
328
+
data = newData,
329
+
mapping = aes(
330
+
x = ageYears,
331
+
y = predictedValue,
332
+
group = sex,
333
+
color = sex),
334
+
linewidth = 2)
335
+
```
336
+
337
+
#### Extract Random Effects
338
+
339
+
```{r}
340
+
ranef(quadraticGCM)
341
+
```
342
+
220
343
### Spline Growth Curve Model {#splineGCM}
221
344
345
+
#### Create Knot
346
+
347
+
```{r}
348
+
mydata$knot <- NA
349
+
mydata$knot[which(mydata$ageYears <= 11)] <- 0
350
+
mydata$knot[which(mydata$ageYears > 11)] <- 1
351
+
```
352
+
353
+
#### Fit Model
354
+
355
+
```{r}
356
+
splineGCM <- lmer(
357
+
math ~ female + ageYearsCentered + female:ageYearsCentered + knot + knot:ageYearsCentered + female:knot + female:knot:ageYearsCentered + (1 + ageYearsCentered | id), # random intercepts and linear slopes; fixed quadratic slopes; sex as a fixed-effect predictor of the intercepts and slopes
0 commit comments