R Under development (unstable) (2026-08-27 r90452 ucrt) -- "Unsuffered Consequences" Copyright (C) 2026 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(bbmle) Loading required package: stats4 > old_opt <- options(digits=3) > if (require(optimx)) { + x <- 0:10 + y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8) + d <- data.frame(x,y) + + ## breaks, don't try this + ## optimx(fn=Lfn,par=c(15,6),method="Rvmmin") + + suppressWarnings(m1 <- mle2(minuslogl=y~dpois(lambda=ymax/(1+x/xhalf)), + start=list(ymax=15,xhalf=6),data=d, + optimizer="optimx", + method=c("BFGS","Nelder-Mead","CG"))) + + ## FIXME!! fails (although not with an error, because + ## errors are caught by profiling) due to npar now + ## being restricted to >1 in optimx 2012.05.24 ... + + suppressWarnings(head(as.data.frame(profile(m1)))) + + ## GH #31: optimx() can silently return NA parameter estimates when a + ## method's backing package isn't installed (e.g. bobyqa/newuoa need + ## minqa, spg needs BB, ucminf needs ucminf, nmkb/hjkb need dfoptim). + ## mle2(optimizer="optimx") now checks up front: an unrecognized method + ## name errors immediately, and a recognized method backed by a missing + ## package errors with a clear message instead of quietly propagating NAs. + zz <- rpois(100,lambda=5) + dd <- data.frame(z=zz) + + ## unrecognized/mistyped method name (this was the original report's + ## own reproducible example: passing the *package* name "minqa" instead + ## of an actual optimx method such as "bobyqa") + stopifnot(inherits(try(mle2(z~dpois(lambda=L), start=list(L=4), data=dd, + optimizer="optimx", method="minqa"), + silent=TRUE), + "try-error")) + + ## sanity-check the method -> required-package mapping bbmle relies on + ## (taken from optimx's own registry, optimx::ctrldefault()) + pkg_for_method <- function(m) { + ctrl <- optimx::ctrldefault(1) + ctrl$allpkg[match(m, ctrl$allmeth)] + } + stopifnot(pkg_for_method("bobyqa")=="minqa", + pkg_for_method("newuoa")=="minqa", + pkg_for_method("spg")=="BB", + pkg_for_method("ucminf")=="ucminf", + pkg_for_method("nmkb")=="dfoptim", + pkg_for_method("hjkb")=="dfoptim") + + ## when the backing package IS installed, the method should work as + ## normal (regression check); if it genuinely isn't installed here, + ## requesting it should fail with an informative error rather than + ## silently returning NAs + if (requireNamespace("minqa", quietly=TRUE)) { + m2 <- mle2(z~dpois(lambda=L), start=list(L=4), data=dd, + optimizer="optimx", method="bobyqa") + stopifnot(is.finite(coef(m2))) + } else { + stopifnot(inherits(try(mle2(z~dpois(lambda=L), start=list(L=4), data=dd, + optimizer="optimx", method="bobyqa"), + silent=TRUE), + "try-error")) + } + + detach("package:optimx") + } Loading required package: optimx > > ## GH #31: generic guard against a silent NA-parameter result from *any* > ## optimizer (not just optimx) -- doesn't require optimx/minqa at all > badopt <- function(par, fn, ...) list(par=c(L=NA_real_), value=NA_real_, convergence=1) > zz2 <- rpois(100,lambda=5) > stopifnot(inherits(try(mle2(z~dpois(lambda=L), start=list(L=4), + data=data.frame(z=zz2), + optimizer="user", optimfun=badopt), + silent=TRUE), + "try-error")) > > options(old_opt) > > proc.time() user system elapsed 6.06 0.21 6.26