Skip to content

Instantly share code, notes, and snippets.

@mihaiconstantin
Last active January 15, 2024 13:54
Show Gist options
  • Save mihaiconstantin/662837454b3766892a3b52fc31d27e79 to your computer and use it in GitHub Desktop.
Save mihaiconstantin/662837454b3766892a3b52fc31d27e79 to your computer and use it in GitHub Desktop.
The Latent Growth Curve Model and Change Over Time
# Libraries.
library(lavaan)
library(ggplot2)
library(patchwork)
# If not installed, then install as follows:
install.packages(c("lavaan", "ggplot2", "patchwork"))
# Model 1.
#
# Here the mean of the latent quadratic slope factor is fixed to -5. This
# indicates that the average rate of change has a negative acceleration (i.e.,
# it decreases or decelerates over time).
model_1 <- "
# Add latent growth intercept with 10 waves.
Intercept =~ 1*x1 + 1*x2 + 1*x3 + 1*x4 + 1*x5 + 1*x6 + 1*x7 + 1*x8 + 1*x9 + 1*x10
# Add slope with 10 waves.
Slope =~ 0*x1 + 1*x2 + 2*x3 + 3*x4 + 4*x5 + 5*x6 + 6*x7 + 7*x8 + 8*x9 + 9*x10
# Add quadratic slope with 10 waves.
SlopeSquared =~ 0*x1 + 1*x2 + 4*x3 + 9*x4 + 16*x5 + 25*x6 + 36*x7 + 49*x8 + 64*x9 + 81*x10
# Fix the means of the latent quadratic slope factors (i.e., the average rate of change).
SlopeSquared ~ -5*1
"
# Model 2.
#
# Here the mean of the latent quadratic slope factor is fixed to 5. This
# indicates that the average rate of change has a positive acceleration (i.e.,
# it increases or accelerates over time).
model_2 <- "
# Add latent growth intercept with 10 waves.
Intercept =~ 1*x1 + 1*x2 + 1*x3 + 1*x4 + 1*x5 + 1*x6 + 1*x7 + 1*x8 + 1*x9 + 1*x10
# Add slope with 10 waves.
Slope =~ 0*x1 + 1*x2 + 2*x3 + 3*x4 + 4*x5 + 5*x6 + 6*x7 + 7*x8 + 8*x9 + 9*x10
# Add quadratic slope with 10 waves.
SlopeSquared =~ 0*x1 + 1*x2 + 4*x3 + 9*x4 + 16*x5 + 25*x6 + 36*x7 + 49*x8 + 64*x9 + 81*x10
# Fix the means of the latent quadratic slope factors (i.e., the average rate of change).
SlopeSquared ~ 5*1
"
# Set a seed for reproducibility of the plots below.
set.seed(2024)
# Generate some data for the two models above.
data_model_1 <- simulateData(model_1, sample.nobs = 100, meanstructure = TRUE)
data_model_2 <- simulateData(model_2, sample.nobs = 100, meanstructure = TRUE)
# Reshape the wide data to long format (i.e., for model 1).
data_long_model_1 <- reshape(
data_model_1,
varying = list(c("x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10")),
v.names = "x",
timevar = "time",
times = 1:10,
idvar = "id",
direction = "long"
)
# Reshape the wide data to long format (i.e., for model 2).
data_long_model_2 <- reshape(
data_model_2,
varying = list(c("x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10")),
v.names = "x",
timevar = "time",
times = 1:10,
idvar = "id",
direction = "long"
)
# Plot the data generated according to each time of acceleration (i.e., negative).
plot_data_model_1 <- ggplot(data_long_model_1, aes(x = time, y = x)) +
geom_jitter(
alpha = 0.1,
width = 0.1
) +
stat_summary(
fun.y = mean,
geom = "line",
col = "darkred",
linewidth = 1
) +
scale_x_continuous(
breaks = seq(1, 10, 1),
labels = seq(1, 10, 1)
) +
labs(
title = "Negative acceleration (i.e., decreasing rate of change)",
x = "Measurement occasion",
y = "Observed variable"
) +
theme_bw() +
theme(
axis.title.y = element_text(
margin = margin(t = 0, r = 5, b = 0, l = 0)
),
axis.title.x = element_text(
margin = margin(t = 15, r = 0, b = 0, l = 0)
)
)
# Plot the data generated according to each time of acceleration (i.e., positive).
plot_data_model_2 <- ggplot(data_long_model_2, aes(x = time, y = x)) +
geom_jitter(
alpha = 0.1,
width = 0.1
) +
stat_summary(
fun.y = mean,
geom = "line",
col = "darkred",
linewidth = 1
) +
scale_x_continuous(
breaks = seq(1, 10, 1),
labels = seq(1, 10, 1)
) +
labs(
title = "Positive acceleration (i.e., increasing rate of change)",
x = "Measurement occasion",
y = "Observed variable"
) +
theme_bw() +
theme(
axis.title.y = element_text(
margin = margin(t = 0, r = 5, b = 0, l = 0)
),
axis.title.x = element_text(
margin = margin(t = 15, r = 0, b = 0, l = 0)
)
)
# Show the plots side by side.
plot_data_model_1 / plot_data_model_2
@mihaiconstantin
Copy link
Author

Example of plots produced by the code above.

lgc-example-change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment