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.