equivalent of DROP in OpenMx interface

3 replies [Last post]
mspiegel's picture
Offline
Joined: 07/31/2009

There's been some discussion among the beta testers to support setting a free parameter to a fixed parameter across an entire model, or setting a fixed parameter to a free parameter across an entire model. Here's a function in R that will accomplish the transformation. The 'model argument is a MxModel object. The 'labels' argument is a vector of parameter names. The 'free' argument is either a NA or a single TRUE or FALSE. The 'value' argument is either a NA or a single numeric value. If 'free' is not NA, then all the parameters with name 'labels' will have their free/fixed status changed to 'free'. If 'value' is not NA, then all the parameters with name 'labels' with have their starting values changed to 'value'.

setParameters <- function(model, labels, free = NA, value = NA) {
   if(missing(model) || !is(model, "MxModel")) {
      stop("The 'model' argument must be a MxModel object")
   }
   if(missing(labels) || typeof(labels) != "character") {
      stop("The 'labels' argument must be a vector of characters")
   }
   if(!is.na(free) && (typeof(free) != "logical" || length(free) != 1)) {
      stop("The 'free' argument must be a single boolean value")
   }
   if(!is.na(value) && (!is.numeric(value) || length(value) != 1)) {
      stop("The 'value' argument must be a single numeric value")
   }
   return(setParametersHelper(model, labels, free, value))
}
 
setParametersHelper <- function(model, labels, free, value) {
   model@matrices <- lapply(model@matrices, setParametersMatrix, labels, free, value)
   model@submodels <- lapply(model@submodels, setParametersHelper, labels, free, value)
   return(model)
}
 
setParametersMatrix <- function(matrix, labels, free, value) {
   select <- apply(matrix@labels, c(1,2), function(x) {!is.na(x) && x %in% labels})
   if (!is.na(free)) {
      matrix@free[select] <- free
   }
   if (!is.na(value)) {
      matrix@values[select] <- value
   }
   return(matrix)
}

Hermine's picture
Offline
Joined: 07/31/2009
Is this implemented in the

Is this implemented in the latest build? Do you have an example?

Thanks!

tbates's picture
Offline
Joined: 07/31/2009
The functions are not part of

The functions are not part of OpenMx, so to implement it, you would simply paste the function code into your demo scripts: then it is available to be called.

I added an example using this (univariate ACE, dropping c by label)

trunk/models/passing/univACE_drop_helper.R

It would be great if this type of syntax worked

model = setParameters(model, labels=c("share.c1"), free = FALSE, value = 0)

i.e., pointing into a sub-model

also this highlights how helpful this parameter of summary() (or perhaps mxRun) would be

summary(fit, sat=xxx)

then the AIC would be computed between the old and new models.

mspiegel's picture
Offline
Joined: 07/31/2009
setParameters() recursively

setParameters() recursively transforms all the parameters in the model hierarchy. To transform only a sub-tree the following would work:

model$share <- setParameters(model$share, "c1", FALSE, 0)

To write a non-recursive version of setParameters, delete the second line in the setParametersHelper function.