Overview

This tutorial walks through a few helpful initial steps before conducting nonlinear growth curve analyses (or any analyses for that matter).

The code and example provided in this tutorial are from Chapter 10 of Grimm, Ram, and Estabrook (2016), with a few additions in code and commentary; however, the chpater should be referred to for further interpretations and insights about the analyses.

Outline

This tutorial provides line-by-line code to

  1. plot raw data
  2. fit a multilevel quadratic growth model with nlme
  3. fit a multilevel spline model with nlme
  4. fit a quadratic growth model with lavaan
  5. fit a spline model with lavaan

Preliminaries: Read in the data and call needed libraries.

filepath <- "https://quantdev.ssri.psu.edu/sites/qdev/files/bgs_height_long.csv"
hght_long <- read.csv(file=url(filepath),header=TRUE)
head(hght_long)
##   id age hght
## 1  1   1 53.5
## 2  1   3 60.4
## 3  1   6 67.2
## 4  1   9 70.9
## 5  1  12 76.2
## 6  1  15 81.1

Quick descriptives of our dataset

summary(hght_long)
##        id          age             hght       
##  Min.   : 1   Min.   : 1.00   Min.   : 49.50  
##  1st Qu.:21   1st Qu.: 6.00   1st Qu.: 65.00  
##  Median :42   Median :12.00   Median : 74.20  
##  Mean   :42   Mean   :13.78   Mean   : 74.34  
##  3rd Qu.:63   3rd Qu.:18.00   3rd Qu.: 83.20  
##  Max.   :83   Max.   :36.00   Max.   :106.00  
##                               NA's   :166

Loading necessary libraries

library(nlme)
library(lavaan)
library(ggplot2)

Plot the longitudinal data

ggplot(hght_long, aes(x = age, y = hght, color = as.factor(id), group = id)) + 
  geom_point() + 
  geom_line() + 
  theme_classic(base_size = 18) + 
  theme(legend.position = "none") + 
  labs(title = "Individual Height Trajectories", y = "Height (cm)", x = "Age (months)")