Dear all.
I am using OpenMx to solve differential equation models, thanks to the the example given here https://www.rdocumentation.org/packages/OpenMx/versions/2.12.2/topics/mxExpectationStateSpaceContinuousTime
It works nicely, and I have been able to estimate first order and second order differential equation. Tests in simulation tend to show that estimates are good. But I found a weird bug, that I reproduce below. If I create simple data with 52 time observations for 250 individuals, and with a noisy signal data with (no) dynamics caused by excitation1 vector:
library(data.table) library(OpenMx) # make test data test <- data.table(ID1 =rep(1:250,each = 52),time = rep(1:52,250)) test[,signal := rnorm(.N,0,1), by = ID1] test[,excitation1 := sample(c(0,1),.N,replace = T,prob = c(0.8,0.2)), by = ID1] # simple first order model cdim <- list('signal', c('ksi')) amat <- mxMatrix('Full', 1, 1, TRUE, -.1, name='A', ubound=0) cmat <- mxMatrix('Full', 1, 1, FALSE, 1, name='C', dimnames=cdim) qmat <- mxMatrix('Zero', 1, 1, name='Q') rmat <- mxMatrix('Diag', 1, 1, TRUE, 0.1, name='R', lbound=1e-6) xmat <- mxMatrix('Full', 1, 1, TRUE, 0, name='x0', lbound=0, ubound=15) # initial position, should be yO pmat <- mxMatrix('Diag', 1, 1, FALSE, 1, name='P0') tmat <- mxMatrix('Full', 1, 1, name='time', labels='data.time') # time dmat <- mxMatrix('Zero', 1, 1, name='D') bmat <- mxMatrix('Full', 1, 1,TRUE,.1,name='B',lbound=0) umat <- mxMatrix('Full', 1, 1, name='u',labels=c('data.excitation1')) model <- mxModel("ODE1", amat, bmat, cmat, dmat, qmat, rmat, xmat, pmat, umat, tmat, mxExpectationSSCT('A', 'B', 'C', 'D', 'Q', 'R', 'x0', 'P0', 'u', 'time'), mxFitFunctionML(), mxData(test, 'raw')) resMx <- mxRun(model)
The model runs with errors. And that is ok, because it is just a test (by the way mxTryHard manage to find propoer starting values). But if I transform the ID column into character:
test[,ID1 := as.character(ID1)] resMx <- mxRun(model)
then R crashes. And I don't get it, because ID1 does not appear in the model. If I do just a simple character column, nothing happens.
I reproduce the crash you report. My
mxVersion()
:The crash occurs with all 3 of the main optimizers. What is your
mxVersion()
output?AFAIK, OpenMx has never been tested with a data.table provided as its raw dataset. The fact that the data.table class stores a pointer to itself likely has something to do with why OpenMx crashes when you try to run the MxModel after you've modified the data.table from what it was when passed to
mxData()
.I also reproduce the crash on my Linux/GNU development system.
mxVersion()
:The crash is being caused by a segmentation fault. So, it looks like memory-safety is the underlying issue.
Here's what I found running under a debugger:
It looks like I was wrong about changing the data.table being the cause of the fault. The following script also segfaults at the same place in the backend:
Note that
test
has itsID1
column converted to character strings before it ever goes intomxData()
, and that it's being cast to a vanilla data.frame when passed tomxData()
.I think this is a bug. The logic in backend
omxDoubleDataElement()
assumes that it will always return either a REALSXP or an INTSXP, and never a STRSXP. It should never be looking at a data column of strings in the first place.OP, could you clarify what you mean by "If I do just a simple character column, nothing happens"?
As of v2.13.2-152-ga29af6cb8, this code correctly fails. Maybe you meant to write
test[,ID1 := as.character(test$ID1)]
?