You are here

Is there a way to add covariates in growth mixture model

11 posts / 0 new
Last post
Veronica_echo's picture
Offline
Joined: 02/23/2018 - 01:57
Is there a way to add covariates in growth mixture model
AttachmentSize
Binary Data GMM.R2.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.

mhunter's picture
Offline
Joined: 07/31/2009 - 15:26
Change weights

I would change

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.

Veronica_echo's picture
Offline
Joined: 02/23/2018 - 01:57
Thank you

Hi Michael,

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!

File attachments: 
AdminRobK's picture
Offline
Joined: 01/24/2014 - 12:15
labels

This syntax appears to be the culprit:

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?).

mhunter's picture
Offline
Joined: 07/31/2009 - 15:26
Rob is right

I had a typo in my example code. I edited it to be correct now with a note that I edited it.

Veronica_echo's picture
Offline
Joined: 02/23/2018 - 01:57
Thank you very much!

Hi Michael,

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?

mhunter's picture
Offline
Joined: 07/31/2009 - 15:26
They are the betas

No, the p11-p22 should be the $\beta$s: $pij = \beta_{i-1}^{(j)}$ or more specifically $p11 = \beta_0^{(1)}$, $p12 = \beta_1^{(1)}$, $p21 = \beta_0^{(2)}$, and $p22 = \beta_1^{(2)}$.

Veronica_echo's picture
Offline
Joined: 02/23/2018 - 01:57
estimate $\beta$

Hi Michael,

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.

File attachments: 
mhunter's picture
Offline
Joined: 07/31/2009 - 15:26
Use mxExpectationMixture

It looks like your code is using an algebra fit function instead of the built-in mixture modeling. See relevant section from your code below.

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()
Veronica_echo's picture
Offline
Joined: 02/23/2018 - 01:57
Now it works well! Thanks for

Now it works well! Thanks for your kindest advice!

mhunter's picture
Offline
Joined: 07/31/2009 - 15:26
Wunderbar!

Wunderbar!

Log in or register to post comments