From 4ede11551fe64d83b2d64e7605652e4f98942aef Mon Sep 17 00:00:00 2001 From: Simon Beyer <64092903+simonbeyer1@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:07:25 +0100 Subject: [PATCH] fix(nlme): em() with spike-and-slab random effects stops when the inner search of a subset fails; cppDE >= 0.13.16; dMod2 0.17.1 Co-Authored-By: Claude Opus 5.5 --- DESCRIPTION | 6 +++--- NEWS.md | 6 ++++++ R/nlmeSpikeSubsets.R | 7 +++++-- tests/testthat/test-spikeSlabSubsets.R | 19 ++++++++++++++++--- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d5ba739..236fc96 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: dMod2 Type: Package Title: Parameter Estimation in Dynamical Models -Version: 0.17.0 -Date: 2026-11-16 +Version: 0.17.1 +Date: 2026-11-25 Authors@R: c( person("Simon", "Beyer", email = "simon.beyer@fdm.uni-freiburg.de", role = c("aut", "cre")), @@ -27,7 +27,7 @@ Imports: cOde, methods, tools, - cppDE (>= 0.13.12), + cppDE (>= 0.13.16), ggplot2 (>= 3.5.0), parallel, parallelly, diff --git a/NEWS.md b/NEWS.md index 77a31f3..0d0c788 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# dMod2 0.17.1 + +* Requires cppDE >= 0.13.16, whose derivatives of `exp10()` are finite at a spike of `-Inf`. +* `em()` with spike-and-slab random effects stops when the inner search of a subset fails, instead + of leaving the subset out of the sum over subsets. + # dMod2 0.17.0 * Requires symident >= 0.18.1. The global analysis of symident stops on work limits instead of time diff --git a/R/nlmeSpikeSubsets.R b/R/nlmeSpikeSubsets.R index 711b9f6..35e3010 100644 --- a/R/nlmeSpikeSubsets.R +++ b/R/nlmeSpikeSubsets.R @@ -69,10 +69,13 @@ if (!is.null(out$hessian)) H <- H + as.matrix(out$hessian)[names(p), names(p)] objlist(value = out$value + prior$value, gradient = gr, hessian = H) } + # a failed inner search stops the fit: every subset stays in the sum over subsets fit <- tryCatch(.innerTrust(list(objfun = objfun, parinit = start[on], fixed = fx, rinit = 1, rmax = 10, iterlim = 100L)), - error = function(e) NULL) - if (is.null(fit) || !is.finite(fit$value)) return(NULL) + error = function(e) stop("em: the inner search of group ", g, " with the slab etas ", + paste(on, collapse = ", "), " and the others at the spike failed: ", + conditionMessage(e), call. = FALSE)) + if (!is.finite(fit$value)) return(NULL) at <- objfun(fit$argument, fixed = fx) H <- (at$hessian + t(at$hessian)) / 2 ch <- tryCatch(chol(H / 2), error = function(e) NULL) diff --git a/tests/testthat/test-spikeSlabSubsets.R b/tests/testthat/test-spikeSlabSubsets.R index b63c077..1750d2e 100644 --- a/tests/testthat/test-spikeSlabSubsets.R +++ b/tests/testthat/test-spikeSlabSubsets.R @@ -50,6 +50,11 @@ dir.create(.spkDir, showWarnings = FALSE) pMixOff <- P(mixOff, modelname = "spk_pmixoff", derivMode = ff, compile = FALSE) pNorm <- P(norm, modelname = "spk_pnorm", derivMode = ff, compile = FALSE) pLin <- P(linear, modelname = "spk_plin", compile = FALSE) + ten <- insert(branch(define(NULL, "p <- p", p = c("x", "k0", "k1")), table = data.frame(row.names = "C1", one = 1)), + "k <- exp10(k)", k = c("k0", "k1")) + ten <- insert(ten, k ~ 0 + (1 | gr(dist = spikeSlab(prob = 0.5, mean = log10(0.5), tau = 1 / log(10), spike = -Inf))), + k = c("k0", "k1")) + pTen <- P(ten, modelname = "spk_pten", compile = FALSE) # two independent species with four candidate rates in one group ks <- c("a0", "a1", "b0", "b1") x2 <- Xs(odemodel(eqnvec(x = "a0 - a1*x", y = "b0 - b1*y"), modelname = "spk_x2", compile = FALSE)) @@ -58,11 +63,11 @@ dir.create(.spkDir, showWarnings = FALSE) "k <- exp(k)", k = ks) ec <- insert(ec, k ~ 0 + (1 | gr(dist = slab(prob = 0.5, mean = log(0.5)))), k = ks) pEC <- P(ec, modelname = "spk_pec", compile = FALSE) - compile(g, x, err, xFF, gFF, pSel, pProb, pGrp, pDef, pDefFF, pMix, pMixOff, pNorm, pLin, g2, x2, pEC, + compile(g, x, err, xFF, gFF, pSel, pProb, pGrp, pDef, pDefFF, pMix, pMixOff, pNorm, pLin, pTen, g2, x2, pEC, output = "spk_all", cores = test_cores()) list(one = one, two = two, counts = counts, shift = shift, mixed = mixed, pure = pure, x = x, g = g, err = err, xFF = xFF, gFF = gFF, pSel = pSel, pProb = pProb, pGrp = pGrp, pDef = pDef, pDefFF = pDefFF, pMix = pMix, - pMixOff = pMixOff, pNorm = pNorm, pLin = pLin, x2 = x2, g2 = g2, pEC = pEC, dir = .spkDir) + pMixOff = pMixOff, pNorm = pNorm, pLin = pLin, pTen = pTen, x2 = x2, g2 = g2, pEC = pEC, dir = .spkDir) }) .spkFit <- function(data, p, ..., errmodel = NULL, method = "focei") { @@ -86,7 +91,7 @@ dir.create(.spkDir, showWarnings = FALSE) })) test_that("a production term the data do not need is switched off, the decay rate stays", { - r <- .spkFit(.spk$one, .spk$pSel, init = c(x = 1)) + r <- .spkShared("one_sel", .spkFit(.spk$one, .spk$pSel, init = c(x = 1))) fit <- r$fit expect_true(fit$converged) p0 <- setNames(fit$random$p0, fit$random$eta) @@ -114,6 +119,14 @@ test_that("a production term the data do not need is switched off, the decay rat expect_equal(p0[["k0"]], 1 / (1 + exp(-(z2 - z1) / 2)), tolerance = 1e-4) }) +test_that("exp10() with the slab scaled by 1 / log(10) gives the fit of exp()", { + ten <- .spkFit(.spk$one, .spk$pTen, init = c(x = 1))$fit + e <- .spkShared("one_sel", .spkFit(.spk$one, .spk$pSel, init = c(x = 1)))$fit + expect_equal(ten$random$p0, e$random$p0, tolerance = 1e-4) + expect_equal(ten$value, e$value, tolerance = 1e-6) + expect_equal(10^ten$random$mode, exp(e$random$mode), tolerance = 1e-4) +}) + test_that("with counts and an error model of the prediction the production term is switched off as well", { fit <- .spkCounts("focei")$fit expect_true(fit$converged)