Scope of functions available to OpenMx

2 replies [Last post]
carey's picture
Offline
Joined: 10/19/2009

folks,
short and sweet: how can OpenMx access user-written functions and functions from other packages? e.g., i would like to access alpha and sumx in the following code:

alpha <- .3
sumx <- function (x) {
sum(x)
}
x <- c(1, 2, 3)
testModel <- mxModel("testModel", mxAlgebra(expression="alpha * sumx(x)", name="test"))
mxRun(testModel)

gory and detailed: i can certainly do this using mxMatrix and mxAlgebra statements, but the problem that i am dealing with is much more complex. it involves analyzing symptom count data in twins where there are lots of 0 counts, fewer counts of 1, etc. i assume that the count data follow poisson processes for twin 1 and twin2 (with respective parameters lambda1 and lambda2) and that lambda1 and lambda2 are random variables distributed as a bivariate gamma that allows for a correlation between lambda1 and lambda2.

to accomplish this, i need to integrate the bivariate gamma over lambda1 and lambda2. the easiest way to do this is to access numerical quadrature routines that are external to the OpenMx functions.

any help is greatly appreciated.

greg

mspiegel's picture
Offline
Joined: 07/31/2009
There are two alternatives to

There are two alternatives to accomplish this goal. The first approach is to write your own objective function in R. You write a function that accepts a MxModel as an input, and it returns a numeric value as an output. It is also possible to preserve some state information in between successive calls to this objective function. On the plus side, this approach is extremely flexible. On the minus side, the programmer becomes responsible for any necessary bookkeeping in order to compute the objective function. See http://openmx.psyc.virginia.edu/wiki/howto-make-objective-function for more information, scroll down to "Using MxRObjective".

The second approach is to implement a matrix function in C that can available to the OpenMx backend. For the sake of efficiency, the OpenMx backend understands a limit set of matrix functions and operations that are implemented in C. You can add an implementation, see the following steps: http://openmx.psyc.virginia.edu/wiki/howto-create-matrix-operation-or-fu...

I think the easiest way to accomplish your task is to write your own objective function in R. For simplicity, you should have that objective function invoke the mxEval() function. So if your user-defined function is called 'foobar' and it accepts three arguments, the objective function could call mxEval(foobar(A, B, C), model, compute=TRUE), assuming that A, B, and C are entities within the model.

carey's picture
Offline
Joined: 10/19/2009
mank thanks, mike that was

mank thanks, mike
that was very helpful.
greg