Carrying out latent profile analysis (LPA) by adapting code examples for a two-factor model and growth mixture models
Posted on
joshuarosenberg
Joined: 07/06/2017
Attachment | Size |
---|---|
mixture.R | 2.7 KB |
Hi everyone. I'm new to both OpenMX and mixture models and am trying to do a latent profile analysis, or a mixture model (initially with two classes) of a Two-factor Model.
With the good documentation and code examples for Two-factor Models and mixture models (specifically the code for a growth mixture model), I tried to combine the two. However, when I run it, I get the following error that I'm having trouble debugging:
Error: The reference 'Class1.fitfunction' is unknown in the algebra named 'Latent Profile Model.mixtureObj'
Can you help point me in the right direction? A reproducible example is given in the attached code. Thank you in advance for considering this!
source code control
Do you use source code control? You probably could have figured this out yourself if you did. There are lots of online tutorials to help you get up to speed such as the [one at github](https://try.github.io/)
Log in or register to post comments
In reply to source code control by jpritikin
thank you. I do use GitHub.
just wondering, how would source code control (use of GitHub) have helped figure out the error?
thank you again,
Josh
Log in or register to post comments
In reply to thank you. I do use GitHub. by joshuarosenberg
source code control
Log in or register to post comments
In comparing them (as part of
Log in or register to post comments
In reply to In comparing them (as part of by joshuarosenberg
no problem
Log in or register to post comments
estimated parameters for both classes?
Could I ask if you could help me to modify this code to allow both classes to have free parameters estimated in this example - now here on GitHub?
Thanks so much.
Log in or register to post comments
In reply to estimated parameters for both classes? by joshuarosenberg
remove the labels
Log in or register to post comments
In reply to remove the labels by jpritikin
Thank you - when I remove the
means <- mxPath( from="one", to=c("x1","x2","x3","y1","y2","y3","F1","F2"),
arrows=1,
free=c(T,T,T,T,T,T,F,F), values=c(1,1,1,1,1,1,0,0),
labels=c("meanx1","meanx2","meanx3",
"meany1","meany2","meany3",NA,NA) )
To:
means <- mxPath( from="one", to=c("ce","be","ae"),
arrows=1,
free=c(T,T,T), values=c(1,1,1)
)
I now see different estimates for Class1 and Class2 mean parameters, but they have the same values (though different standard errors).
I pushed the change to the example here.
Thank you again very much for helping me work through this.
Log in or register to post comments
In reply to Thank you - when I remove the by joshuarosenberg
per model
Log in or register to post comments
In reply to per model by jpritikin
I think per row - something
Thank you again
Log in or register to post comments
?emxMixtureModel
EasyMx
, an R package that is a wrapper aroundOpenMx
. Run this:install.packages("EasyMx")
require(EasyMx)
?EasyMx # look at the help page for the package
?emxMixtureModel # look at the help page for the mixture model helper
The general idea with the mixture model helper is that you give it a list of
MxModel
objects and it creates the needed mixture pieces. As I understand it, a latent profile analysis is a mixture model in which each mixture component freely estimates all of its means, variances, and covariances. Here's the example latent profile model:# Latent Profile Example
require(EasyMx)
m1 <- omxSaturatedModel(demoOneFactor)[[1]]
m1 <- mxRename(m1, 'profile1')
m2 <- omxSaturatedModel(demoOneFactor)[[1]]
m2 <- mxRename(m2, 'profile2')
mod <- emxMixtureModel(list(m1, m2), data=demoOneFactor, run=TRUE)
summary(mod)
coef(mod)
mxGetExpected(mod$profile1, 'covariance')
mxGetExpected(mod$profile1, 'means')
mxGetExpected(mod$profile2, 'covariance')
mxGetExpected(mod$profile2, 'means')
Log in or register to post comments
In reply to ?emxMixtureModel by AdminHunter
Thank you, this worked great.
Could I ask for help for specifying a model for equal variance across mixture components?
Thank you and Joshua very much.
Log in or register to post comments
In reply to Thank you, this worked great. by joshuarosenberg
If I can, please allow me to
Can I ask for help specifying models with:
- Varying means, fixed residuals and no covariances (covariances within profiles are set to 0)
- Varying means, fixed residual variances and covariances
My understanding is the model fit in the example above is:
- Varying means, varying residual variances and covariances.
Log in or register to post comments
In reply to ?emxMixtureModel by AdminHunter
Covariances may be set to zero
Log in or register to post comments
In reply to Covariances may be set to zero by neale
Thanks for the clarifying
If only the means and variances would be freed, how can: a) the model in which both are freed and b) the model in which only the means are freed be estimated? And am I understanding correctly that the model (using EasyMx) specified above would be the model corresponding to a) the model in which both means and variances are freely estimated?
Log in or register to post comments
In reply to Thanks for the clarifying by joshuarosenberg
what is mxmatrix?
Log in or register to post comments
In reply to what is mxmatrix? by jpritikin
I have, but I will review the
Log in or register to post comments
In reply to I have, but I will review the by joshuarosenberg
See help pages
omxSaturatedModel
is just a quick way to build a saturated model. It returns a list where the first element is the saturated model (free means, free variances, free covariances), and the second element is the independence model (i.e., has free means, free variances, and zero covariances). The input toemxMixtureModel
is just a list of models and a data set. I usedomxSaturatedModel
just as a one-line way to build a model. You'll have to do some thinking about what model you want to specify for the components of the mixture, and build those models.You can constrain parameters to be equal by given them the same "label".
Log in or register to post comments
In reply to ?emxMixtureModel by AdminHunter
Getting Posterior Probabilities?
Thank you so much.
Log in or register to post comments
In reply to Getting Posterior Probabilities? by KB
posterior class-membership probabilities
#The two mixture proportions are prior class-membership probabilities
#for drawing inferences about each participant:
pri1 <- gmmFit$expectation$output$weights[1]
pri2 <- gmmFit$expectation$output$weights[2]
#The fitfunctions of the two component models return a vector of participants'
#raw (not log) likelihoods:
lik1 <- as.vector(gmmFit$Class1$fitfunction$result)
lik2 <- as.vector(gmmFit$Class2$fitfunction$result)
#Calculate participants' marginal likelihoods (probably not be necessary):
marglik <- (pri1*lik1) + (pri2*lik2)
#Calculate participants' posterior densities:
post1 <- (pri1*lik1)/marglik
post2 <- (pri2*lik2)/marglik
#Create a matrix of participants' posterior densities for the two components,
#and then normalize them into probabilities:
posteriors <- cbind(post1,post2)/rowSums(cbind(post1,post2))
#^^^Since each row of the matrix needs to be normalized, you probably
#don't need the marginal likelihoods.
Neale or Hunter can correct me if I've made a mistake anywhere.
I wonder if this is something that should have a dedicated helper function to do it?
Log in or register to post comments
EasyMx - initial values?
I am also new to OpenMx and like Joshua, I am trying to run a simple LPA with two normal component densities. I was wondering, is there a way to easily change the initial values in EasyMx? Or if I want that kind of flexibility do I need to go back to working directly with OpenMx?
Thanks!
Log in or register to post comments
Many umx and EasyMx functions create mxModels.
model$matrix$values <- c(1,5)
or
model$matrix <- mxMatrix(...)
to replace a matrix inside a model.
Log in or register to post comments