You are here

Example of using a definition variable in a RAM model

8 posts / 0 new
Last post
tbates's picture
Offline
Joined: 07/31/2009 - 14:25
Example of using a definition variable in a RAM model

Hi,
Are there any example scripts for how to add a definition variable to a RAM model, and how to then use that in an algebra on a path?

Here's one adding a definition variable for means, but not including its value in an algebra, and then using that algebra to se the value for another path.

https://openmx.ssri.psu.edu/docs/OpenMx/2.5.1/DefinitionMeans_Path.html

jpritikin's picture
Offline
Joined: 05/24/2012 - 00:35
like this?

inst/models/nightly/univACErSEM.R

tbates's picture
Offline
Joined: 07/31/2009 - 14:25
Example rather than test (and creating a moderated path in RAM)

Thanks Josh,
Wanting an example to point people to with comments on most lines explaining how the script works. Also one just showing "Here is how to create a moderated path in RAM" rather than more complex example for learning.

Reading through univACErSEM.R now: The definition variables are used on paths directly, rather than being used to moderate a path (i.e., compute the path based on the definition variable. e.g.

mxMatrix(name= "beta1", 1,1)
mxAlgebra(name= "BMIalg", latentBMI + beta1 * data.BMI_SNP)
tbates's picture
Offline
Joined: 07/31/2009 - 14:25
moderation example: can it be extended?

This example shows how a def var can be used to moderate the effect of x on y by faking a latent variable which is set to have a mean equal to def, and having a path from x to y go via this latent variable.

But can it be extended to the typical case of

x-->y;
def--> y; 
x*def-->y

?
i.e., to include the def on the diagram as a manifest, and use it as a definition variable?

# make up some data
require(umx); library(MASS)
N      = 500 # sample size, per group
Sigma  = matrix(c(1,.5,.5,1),2,2)
group1 = mvrnorm(N, c(1,2), Sigma) # Use mvrnorm from MASS package
group2 = mvrnorm(N, c(0,0), Sigma)
 
# Put the two groups together, create a definition variable,
# and make a list of which variables are to be analyzed (selVars)
xy              = rbind(group1,group2)      # Bind groups together by rows
dimnames(xy)[2] = list(c("x","y"))          # Add names
def             = rep(c(1,0),each=N);       # Add def var [2n] for group status
selVars         = c("x","y")                # Make selection variables object
df = data.frame(xy,def)
 
#############################################
# Make a model with moderation using a definition variable #
#############################################
m1 = mxModel("def_test", type="RAM", manifestVars= c("x", "y"), latentVars= "dummy", mxData(df, type= "raw"),
    mxPath(c("x", "y"), arrows= 2, free= TRUE),
    mxPath("one", to = c("x","y"), arrows= 1, free= TRUE),
    mxPath("dummy", value= 0, arrows= 2, free= FALSE),
    mxPath("one", to = "dummy", value= 0, free= FALSE),
    mxPath("x", to = "y"),
    mxPath("x", to = "dummy", values=0, free=FALSE, label = "data.def"),
    mxPath("dummy", to = "y")
)
m1 = mxRun(m1)
Benny's picture
Offline
Joined: 05/15/2020 - 05:00
Update?

Hi,
I am interested in estimating a GxE biometric twin model using the RAM notation. However, I am not sure how to implement the moderation with the path notation. Could you give me a hint if it is possible and if so how one can code it within OpenMx?

AdminRobK's picture
Offline
Joined: 01/24/2014 - 12:15
Yes, it's possible to do that

Yes, it's possible to do that with RAM specification. I don't have an example handy, though. However, I honestly think RAM is more trouble than it's worth for that kind of model.

If the model you want to fit is fairly basic, you can probably do it pretty easily in the umx package, with one of its umxGxE* functions.

AdminNeale's picture
Offline
Joined: 03/01/2013 - 14:09
Onyx is cool

I drew this model with Onyx and used to OpenMx (path) to obtain a script. Obviously it needs work in the sense of two groups with the same model except for the correlation between the twins' additive genetic factors. But it may get you started. If you download Onyx from https://onyx-sem.com then you can load the diagram from the .xml file which has been renamed to .txt to enable upload here.

Mostly, the moderation trick is to draw two paths, e.g., from A to P, one goes direct and is the unmoderated bit, and the other goes first to a latent variable that has no double-headed arrow or other variance to itself, and then from that latent variable to P. Put moderation parameter on one path, and the definition variable for the moderator on the other, and the two together operate as if the algebra (a + b * Mod) had been directly specified as a formula on a path.

File attachments: 
Benny's picture
Offline
Joined: 05/15/2020 - 05:00
Thank you

Great, that's very helpful. I will try this out. Yes umx is nice but I want to build a biometric LGC with some moderated paths and using the path specification seems to be a intuitive first step (and to check some scripts based on the matrix specification).