Linear Growth Model with Time-Invariant Covariates – Multilevel & SEM Implementation in R

1 Overview

This tutorial illustrates fitting of linear growth models with time-invariant covariates in the multilevel and SEM frameworks in R.

Example data and code are drawn from Chapter 5 of Grimm, Ram, and Estabrook (2017). Specifically, using the NLSY-CYA Dataset we examine how change in children’s mathematics achievement across grade is related to low birth weight and early (kindergarten to first grade) antisocial behaviors. Please see the book chapter for additional interpretations and insights about the analyses.

1.0.1 Preliminaries - Loading libraries used in this script.

library(psych)  #for basic functions
library(plyr)   #for data management
library(ggplot2)  #for plotting
library(nlme) #for mixed effects models
library(lme4) #for mixed effects models
library(lavaan) #for SEM 
library(semPlot) #for making SEM diagrams

1.0.2 Preliminaries - Data Preparation and Description

For our examples, we use the mathematics achievement scores from the NLSY-CYA Long Data.

Load the repeated measures data

#set filepath for data file
filepath <- "https://raw.githubusercontent.com/LRI-2/Data/main/GrowthModeling/nlsy_math_long_R.dat"
#read in the text data file using the url() function
dat <- read.table(file=url(filepath),
                  na.strings = ".")  #indicates the missing data designator
#copy data with new name 
nlsy_math_long <- dat  

#Add names the columns of the data set
names(nlsy_math_long) = c('id'     , 'female', 'lb_wght', 
                          'anti_k1', 'math'  , 'grade'  ,
                          'occ'    , 'age'   , 'men'    ,
                          'spring' , 'anti')


#view the first few observations in the data set 
head(nlsy_math_long, 10)
id female lb_wght anti_k1 math grade occ age men spring anti
201 1 0 0 38 3 2 111 0 1 0
201 1 0 0 55 5 3 135 1 1 0
303 1 0 1 26 2 2 121 0 1 2
303 1 0 1 33 5 3 145 0 1 2
2702 0 0 0 56 2 2 100 NA 1 0
2702 0 0 0 58 4 3 125 NA 1 2
2702 0 0 0 80 8 4 173 NA 1 2
4303 1 0 0 41 3 2 115 0 0 1
4303 1 0 0 58 4 3 135 0 1 2
5002 0 0 4 46 4 2 117 NA 1 4

Our specific interest is intraindividual change in the repeated measures of math change across grade, and how the interindividual differences in those trajectories (intercept and linear slope) are related to lb_wght and anti_k1.

As noted in Chapter 2 , it is important to plot the data to obtain a better understanding of the structure and form of the observed phenomenon. Here, we want to examine the data to see how the repeated measures of math are structured with respect to age.

Longitudinal Plot of Math across Grade at Testing

#intraindividual change trajetories
ggplot(data=nlsy_math_long,                    #data set
       aes(x = grade, y = math, group = id)) + #setting variables
  geom_point(size=.5) + #adding points to plot
  geom_line(aes(linetype=factor(lb_wght), alpha= anti_k1)) +  #adding lines to plot
  theme_bw() +   #changing style/background
  #setting the x-axis with breaks and labels
  scale_x_continuous(limits=c(2,8),
                     breaks = c(2,3,4,5,6,7,8), 
                     name = "Grade at Testing") +    
  #setting the y-axis with limits breaks and labels
  scale_y_continuous(limits=c(10,90), 
                     breaks = c(10,30,50,70,90), 
                     name = "PIAT Mathematics")