I want to try to apply nonlinear structural equation modeling in the lavaan with WLSMV estimator. How can I do it?
--
Seongho Bae
dat$age2 <- dat$age^2
model <- '# latent variables
N =~ N1+N2+N3+N4+N5+N6
E =~ E1+E2+E3+E4+E5+E6
# regressions
N ~ age + age2
E ~ age + age2'
fit <- sem(model, data=dat)
# to plot the "quadratic" effect for dat$age, you have to compute f(dat$age) using the coefficients from, then you can plot dat$age against f(dat$age)
fit@Model@GLIST$beta
First of all you have to specify what you mean by "nonlinear"? Some people call simple quadratic effects "nonlinear" which of course is right
The next question is for which component do you want to estimate nonlinear effects? For latent variables (this would be more complicated)? Or for continuous explanatory variables?
I would like to plot the quadratic effect of age from this model included below. I see Giuseppe's explanation about computing f(dat$age) and then plotting it against dat$age, but I do not follow exactly how this is executed.
## generate a sequence of values in the observed range of your x-axis predictorageValues <- seq(from = min(data$age), to = max(data$age), length.out = 50)## extract relevant parameter estimates to put in regression equation# if you don't have a mean structure, you can set b0 <- 0b0 <- lavInspect(fitmodage2, "coef")$alpha["aggressO"]b1 <- lavInspect(fitmodage2, "coef")$beta["aggressO", "age1"]b2 <- lavInspect(fitmodage2, "coef")$beta["aggressO", "age2"]b3 <- lavInspect(fitmodage2, "coef")$beta["aggressO", "boy"]## calculate predicted values using regression equationyHat.girl <- b0 + b1*ageValues + b2*(ageValues^2)yHat.boy <- b0 + b1*ageValues + b2*(ageValues^2) + b3 # I assume "boy" is a dummy code## plot one group's regression lineplot(x = ageValues, y = yHat.girl, type = "l", col = "red", lwd = 2)## add the other group's regression linelines(x = ageValues, y = yHat.boy, col = "blue", lwd = 2)