bbolker / bbmle

maximum likelihood estimation package
GNU General Public License v3.0
25 stars 13 forks source link

Optimization method not well documented #37

Open dushoff opened 5 months ago

dushoff commented 5 months ago

method is referred to optim; it is hard to figure out that the default method is actually different.

bbolker commented 4 months ago

Also (marginally related), make sure "user" option for optimization is documented; ?example?

library(bbmle)
library(emdbook) ## for dbetabinom
library(DEoptim)
load(system.file("vignetteData","orob1.rda",package="bbmle"))

m1 <- mle2(m ~ dbetabinom(prob = plogis(logit_mu),
                    size = n,
                    theta = exp(log_theta)),
     parameters = list(logit_mu ~ dilution),
     start = list(logit_mu = 0, log_theta = 0),
     data = orob1)

dewrap <- function(par, fn, lower = -Inf, upper = Inf, control = DEoptim.control(), ...) {
    ## what about ... args? eventually we might want to be able to pass
    ##  the DEoptim control settings
    dd <- DEoptim(fn = fn, lower = lower, upper = upper, control = control)
    ## need to preserve/restore original names
    bestpar <- dd$optim$bestmem
    names(bestpar) <- names(par)
    ## pretend we always converged
    return(list(par = bestpar, value = dd$optim$bestval, conv  = 0))
}

npar <- 4
update(m1,
       ## setting lower/upper to ±10 works here because these are log/logit scales
       lower = rep(-10, npar),
       upper = rep( 10, npar),
       optimizer = "user",  ## tell mle2 to use a user-specified optimization function
       optimfun = dewrap,
       control = DEoptim.control(itermax = 100, trace = FALSE)
       )