Here, the independent variable X and the intermediate variable M are both continuous values,
but the dependent variable Y is a binary variable which is modeled in binomial logistic regression.
How to use lavaan to establish a structural equation model?
library(tidyverse) library(palmerpenguins) d <- penguins %>% drop_na() %>% filter(species == "Gentoo") %>% mutate(gender = if_else(sex == "male", 1, 0)) d # If we only focus on the Y variable mod <- glm(gender ~ 1 + body_mass_g + flipper_length_mm, family = binomial(link = "logit"), data = d)
But, I don't know how to use lavaan in this situation
library(lavaan) model <- ' # regression body_mass_g ~ a * flipper_length_mm gender ~ cprime * flipper_length_mm + b * body_mass_g # effects indirect := a*b direct := cprime total := cprime + a*b ' d1 <- d %>% mutate(gender = order(gender)) fit <- sem(model, data = d1, link = "logit", ordered = c("gender") )
Thanks so much for your assistance!