# Error message when using get() to return values of OpenMx object

12 posts / 0 new
Offline
Joined: 02/23/2018 - 01:57
Error message when using get() to return values of OpenMx object
AttachmentSize
156.38 KB

Hi everyone,

I am trying to return values of an OpenMx object from optimized mxModel by get() (more details could be found in attached). Is there any other way to return the value of an OpenMx object by string? Thanks in advance.

Offline
Joined: 07/31/2009 - 15:26
?mxSave ?omxGetParameters

I'm not sure what you mean by "return the values of an OpenMx object". You can save the final state of a model to a file with mxSave or get the free parameters with coef (see ?omxGetParameters). You can evaluate algebras with mxEval and write those values to files with e.g. write.csv. Hopefully this helps. If not, more details please.

Offline
Joined: 02/23/2018 - 01:57
Thank you

Hi Michael,

Thanks for your informative reply. I want to evaluate algebras; for example, I have a series of algebras named "c1mean", "c2mean", ..., "ckmean". What I want is to create strings by paste0("c", 1:k, "mean"), change them to object names, and to evaluate them. Do you have suggestions about it? Any advice would be appreciated. Thank you!

Offline
Joined: 01/24/2014 - 12:15
single algebra?

Perhaps adhere the 1 thru k algebras into a single algebra with cbind() and/or rbind(), and use mxEval() to evaluate that single algebra?

At any rate, the function mxEvalByName() may be useful to you, in case you didn't know about it already.

Offline
Joined: 02/23/2018 - 01:57
Thanks!

Thank you very much! mxEvalByName() is what I want.

Offline
Joined: 02/23/2018 - 01:57
A relevant question

Dr. Kirk,

After having point estimates by mxEvalByName(), I still want to have the SEs, which I thought can be realized by mxSE(). Yet I got some errors as attached, which might be due to mxAlgebra(). I tried to find the string version of mxSE(), but failed. How can I fix this error? Any advice would be appreciated.

File attachments:
Offline
Joined: 01/24/2014 - 12:15
forceName=TRUE

Try passing argument forceName=TRUE to mxSE().

Offline
Joined: 02/23/2018 - 01:57
It works

Dr. Kirk,

It works! Thank you very much!

Offline
Joined: 01/24/2014 - 12:15

Offline
Joined: 07/31/2009 - 14:25
accessing elements of a model with mxEval and mxEvalByName()

you probably don't want to use get(). things inside models are not in the global name space (same reason get(mpg) won't return the mpg column of mtcars dataframe...

You might want mxEval() and mxEvalByName()

So, if we bang out a quick twin model, and interogate it:

require(umx)
data(twinData) # ?twinData from Australian twins.
# Pick the variables
selDVs = c("ht")
mzData <- twinData[twinData$zygosity %in% "MZFF", ] dzData <- twinData[twinData$zygosity %in% "DZFF", ]
m1 = umxACE(selDVs = selDVs, sep = "", dzData = dzData, mzData = mzData) # -2ll= 9659, a1 = .92

#What's A?
mxEval(top.A, model = m1)
[,1]
[1,] 0.003727328
# or
mxEval(A, model = m1\$top)

# or
mxEvalByName("top.A", model = m1)
Offline
Joined: 07/31/2009 - 15:26
mxAlgebraFromString

I think Veronica wants mxAlgebraFromString() and then mxEval() that algebra. It it something like this.

astring <- paste0("cbind(", paste0("c", 1:k, "mean", collapse=', '), ")")
# e.g. for k=7 astring is
# "cbind(c1mean, c2mean, c3mean, c4mean, c5mean, c6mean, c7mean)"
palg <- mxAlgebraFromString(astring, name='Jerry')
amodel <- mxModel(... stuff ..., palg)
# ... put palg in your model
mxEval(Jerry, amodel)

Of course, there is another consideration. You may want to re-think your model construction so that you don't need to do this. For example, you could have a matrix like this

amat <- mxMatrix('Full', nrow=k, ncol=1, free=TRUE, labels=paste0('c', 1:k, 'mean'), name='JerryM)

Anywhere you want to use 'c3mean' just refer to it by name. This way you don't need to construct 'Jerry' because you started with 'JerryM'.

Offline
Joined: 02/23/2018 - 01:57
Thank you very much!

Thanks for all kind and detailed replies! I created several expressions (c1mean, c2mean, ..., ckmean) using mxAlgebraFromString() and want to return their values by character name. I tried mxEvalByName() and it worked well. I really appreciate it.