# Example of using a definition variable in a RAM model

4 posts / 0 new 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 Offline
Joined: 05/24/2012 - 00:35
like this?

inst/models/nightly/univACErSEM.R 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) 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) = 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)