Is there a way to add covariates in growth mixture model
Posted on
Veronica_echo
Joined: 02/23/2018
Attachment | Size |
---|---|
GMM.R | 2.53 KB |
Hi,
I have a script for the 2-class growth mixture model (please see attached). I am wondering if I can add covariate (say x1) as a covariate of my model. I may want to see the effect of x1 on the latent classes only and also of it on both latent classes and the growth factors. As my understanding, in MPlus, the first scenario can be expressed as "c#1 on x1" and the second one can be written as "c#1 on x1" and "int on x1; slp on x1" of each class, but I have zero idea to translate to OpenMx. Any advice would be appreciated.
Change weights
classP <- mxMatrix( type = "Full", nrow = 2, ncol = 1,
free=c(TRUE, FALSE), values=1,
labels = c("p1","p2"), name = "weights" )
to
classPM <- mxMatrix( type = "Full", nrow = 2, ncol = 2,
free=c(TRUE, FALSE,TRUE,TRUE), values=1,
labels = c("p11","p21", "p12", "p22"), name = "weightsM" ) #Edited from original which had a typo
classPV <- mxMatrix(nrow=2, ncol=1, labels=c(NA, "data.X1"), values=1, name="weightsV")
classP <- mxAlgebra(weightsM %*% weightsV, name="weights")
The R object `classPM` is a 2x2 matrix of the base weights: the first row is for the first class; the second row is for the second class. The first column of `weightsM` gets multiplied by 1; the second column gets multiplied by the variable `X1`. This results in the R object `classP` and the OpenMx object `weights`. Include `classPM`, `classV`, and `classP` in the `gmm2` model accordingly.
Log in or register to post comments
In reply to Change weights by mhunter
Thank you
Thanks for your kind reply. I tried to update my codes but got an error: "In model 'GMM2Class' the name 'p21' is used as a free parameter in 'GMM2Class.weightsM' and as a fixed parameter in 'GMM2Class.weightsM'". I don't know why. I am attaching script and data I used, could you kindly help me to figure it out? Thank you very much!
Log in or register to post comments
In reply to Thank you by Veronica_echo
labels
classPM <- mxMatrix( type = "Full", nrow = 2, ncol = 2,
free=c(TRUE, FALSE,TRUE,TRUE), values=1,
labels = c("p11","p21", "p21", "p22"), name = "weightsM" )
Try changing one of the redundant "p21" labels to something else ("p12", perhaps?).
Log in or register to post comments
In reply to labels by AdminRobK
Rob is right
Log in or register to post comments
In reply to Rob is right by mhunter
Thank you very much!
I really appreciate your correction. It works now. I am wondering the interpretation of p11-p22 if I calculate the weights in your script. They are not $\beta$'s in the formula, right? Can I have those $\beta$'s?
Log in or register to post comments
In reply to Thank you very much! by Veronica_echo
They are the betas
Log in or register to post comments
In reply to Rob is right by mhunter
estimate $\beta$
I also tried to write several lines to estimate $\beta$'s from above formula (please see the attached), which may have initial value or model specification issue. Do you have tips about it? Any advice would be appreciate.
Log in or register to post comments
In reply to estimate $\beta$ by Veronica_echo
Use mxExpectationMixture
classBeta <- mxMatrix(type = "Full", nrow = 2, ncol = 3,
free = c(F, T, F, T, F, T), values = c(0, 0, 0, 1.2, 0, 0.9),
labels = c("beta10", "beta20", "beta11", "beta21", "beta12", "beta22"),
name = "classbeta")
classPV <- mxMatrix(nrow = 3, ncol = 1, labels = c(NA, "data.x1", "data.x2"),
values = 1, name = "weightsV")
classP <- mxAlgebra(exp(classbeta %x% weightsV), name = "weights") # Note: MH thinks this should be %*% not %x%
classS <- mxAlgebra(weights %x% (1/sum(weights)), name = "classProbs")
algebraObjective <- mxAlgebra(-2 * sum(log(classProbs[1,1] %x% Class1.objective +
classProbs[2,1] %x% Class2.objective)),
name = "mixtureObjective")
objective <- mxFitFunctionAlgebra("mixtureObjective")
I would change this to
classBeta <- mxMatrix(type = "Full", nrow = 2, ncol = 3,
free = c(F, T, F, T, F, T), values = c(0, 0, 0, 1.2, 0, 0.9),
labels = c("beta10", "beta20", "beta11", "beta21", "beta12", "beta22"),
name = "classbeta")
classPV <- mxMatrix(nrow = 3, ncol = 1, labels = c(NA, "data.x1", "data.x2"),
values = 1, name = "weightsV")
classP <- mxAlgebra(classbeta %*% weightsV), name = "weights")
expec <- mxExpectationMixture(c('Class1', 'Class2'), weights='weights', scale='softmax')
objective <- mxFitFunctionML()
Log in or register to post comments
In reply to Use mxExpectationMixture by mhunter
Now it works well! Thanks for
Log in or register to post comments
In reply to Now it works well! Thanks for by Veronica_echo
Wunderbar!
Log in or register to post comments