For some reason, it wouldn't let me post it with the code attached so here is the code:
#Attempting with Binary Outcome - Reproducible code
library(tidyverse)
library(lavaan)
#continous Predictor Variabe
#6 time points, n=300 to match my data with some variation across time
ProtestBelong_t1 <- sample(1:7, 300, replace = T)
ProtestBelong_t2 <- sample(1:7, 300, replace = T)
ProtestBelong_t3 <- sample(1:7, 300, replace = T)
ProtestBelong_t4 <- sample(1:7, 300, replace = T)
ProtestBelong_t5 <- sample(1:7, 300, replace = T)
ProtestBelong_t6 <- sample(1:7, 300, replace = T)
#Binary Outcome Variable
#6 time points, n=300 to match my data with some variation across time
ParticipationBi_t1 <- rbinom(n=300, size=1, prob=0.6)
ParticipationBi_t2 <- rbinom(n=300, size=1, prob=0.55)
ParticipationBi_t3 <- rbinom(n=300, size=1, prob=0.5)
ParticipationBi_t4 <- rbinom(n=300, size=1, prob=0.47)
ParticipationBi_t5 <- rbinom(n=300, size=1, prob=0.45)
ParticipationBi_t6 <- rbinom(n=300, size=1, prob=0.40)
d <- list(ProtestBelong_t1, ProtestBelong_t2, ProtestBelong_t3,
ProtestBelong_t4, ProtestBelong_t5, ProtestBelong_t6,
ParticipationBi_t1, ParticipationBi_t2, ParticipationBi_t3,
ParticipationBi_t4, ParticipationBi_t5, ParticipationBi_t6)
d <- as.data.frame(do.call(cbind, d))
names(d) <- c("ProtestBelong_t1", "ProtestBelong_t2", "ProtestBelong_t3",
"ProtestBelong_t4", "ProtestBelong_t5", "ProtestBelong_t6",
"ParticipationBi_t1", "ParticipationBi_t2", "ParticipationBi_t3",
"ParticipationBi_t4", "ParticipationBi_t5", "ParticipationBi_t6")
#clean up ws
rm(list=setdiff(ls(), "d"))
#setting binary vairable as type ordered for lavaan
d[,c("ParticipationBi_t2",
"ParticipationBi_t3",
"ParticipationBi_t4",
"ParticipationBi_t5",
"ParticipationBi_t6")] <-
lapply(d[,c("ParticipationBi_t2",
"ParticipationBi_t3",
"ParticipationBi_t4",
"ParticipationBi_t5",
"ParticipationBi_t6")], ordered)
class(d$ParticipationBi_t2)
#Defining random intercepts cross-lagged model
RICLPM <- '
# Create between components (random intercepts)
RI_Participation =~ 1*ParticipationBi_t1 + 1*ParticipationBi_t2 +
1*ParticipationBi_t3 + 1*ParticipationBi_t4 +
1*ParticipationBi_t5 + 1*ParticipationBi_t6
RI_Belong =~ 1*ProtestBelong_t1 + 1*ProtestBelong_t2 +
1*ProtestBelong_t3 + 1*ProtestBelong_t4 +
1*ProtestBelong_t5 + 1*ProtestBelong_t6
# Create within-person centered variables
wParticipation1 =~ 1*ParticipationBi_t1
wParticipation2 =~ 1*ParticipationBi_t2
wParticipation3 =~ 1*ParticipationBi_t3
wParticipation4 =~ 1*ParticipationBi_t4
wParticipation5 =~ 1*ParticipationBi_t5
wParticipation6 =~ 1*ParticipationBi_t6
wBelong1 =~ 1*ProtestBelong_t1
wBelong2 =~ 1*ProtestBelong_t2
wBelong3 =~ 1*ProtestBelong_t3
wBelong4 =~ 1*ProtestBelong_t4
wBelong5 =~ 1*ProtestBelong_t5
wBelong6 =~ 1*ProtestBelong_t6
# Estimate the lagged effects between the within-person centered variables.
wParticipation2 + wBelong2 ~ wParticipation1 + wBelong1
wParticipation3 + wBelong3 ~ wParticipation2 + wBelong2
wParticipation4 + wBelong4 ~ wParticipation3 + wBelong3
wParticipation5 + wBelong5 ~ wParticipation4 + wBelong4
wParticipation6 + wBelong6 ~ wParticipation5 + wBelong5
# Estimate the covariance between the within-person centered variables at the first wave.
wParticipation1 ~~ wBelong1 # Covariance
# Estimate the covariances between the residuals of the within-person centered variables (the innovations).
wParticipation2 ~~ wBelong2
wParticipation3 ~~ wBelong3
wParticipation4 ~~ wBelong4
wParticipation5 ~~ wBelong5
wParticipation6 ~~ wBelong6
# Estimate the variance and covariance of the random intercepts.
RI_Participation ~~ RI_Participation
RI_Belong ~~ RI_Belong
RI_Participation ~~ RI_Belong
# Estimate the (residual) variance of the within-person centered variables.
wParticipation1 ~~ wParticipation1 # Variances
wBelong1 ~~ wBelong1
wParticipation2 ~~ wParticipation2 # Residual variances
wBelong2 ~~ wBelong2
wParticipation3 ~~ wParticipation3
wBelong3 ~~ wBelong3
wParticipation4 ~~ wParticipation4
wBelong4 ~~ wBelong4
wParticipation5 ~~ wParticipation5
wBelong5 ~~ wBelong5
wParticipation6 ~~ wParticipation6
wBelong6 ~~ wBelong6
'
RICLPM.fit <- lavaan(RICLPM, data = d, meanstructure = T, int.ov.free = T)
summary(RICLPM.fit, standardized = T)