Fit a SPoRC RTMB model
fit_model.RdConstructs an RTMB automatic differentiation function via
RTMB::MakeADFun, optimizes it with stats::nlminb, and
optionally refines the solution with Newton steps using the analytic
Hessian. The best parameter vector (obj$env$last.par.best),
optimizer output, and model report are attached to the returned object.
Usage
fit_model(
data,
parameters,
mapping,
random = NULL,
newton_loops = 3,
silent = FALSE,
do_optim = TRUE,
nlminb_control = list(iter.max = 1e+05, eval.max = 1e+05, rel.tol = 1e-15),
lower = NULL,
upper = NULL,
model = SPoRC_rtmb,
...
)Arguments
- data
Named list of model data as constructed by the
Setup_Mod_*family of functions.- parameters
Named list of parameter starting values.
- mapping
Named list of factor maps controlling parameter sharing and fixing.
- random
Character vector of parameter names to integrate out as random effects.
NULL(default) fits a fixed-effects-only model.- newton_loops
Integer. Number of Newton refinement steps applied after
nlminbconvergence to reduce gradient magnitudes. Each step solves \(\Delta\theta = -H^{-1} g\) and updates the objective. Default3. Errors and warnings are caught silently viatryCatch, so a step that fails leaves thenlminbsolution in place without a message. \(H\) comes from the AD tape (obj$he) for fixed-effects models, which is exact and costs a single call. Random-effects models fall back tooptimHessdifferencing the gradient, since RTMB does not implement a tape Hessian when random effects are present. Refinement stops early if \(H\) comes back non-finite, which happens on models that have not converged, where second derivatives can be undefined at parameter values the objective and gradient still evaluate at. Thenlminbsolution is kept in that case.- silent
Logical. If
TRUE, suppresses RTMB and optimizer console output. DefaultFALSE.- do_optim
Logical. If
TRUE(default), runsnlminband Newton refinement. IfFALSE, returns the un-optimizedMakeADFunobject only.- nlminb_control
Named list of control parameters passed to
stats::nlminb. Defaultlist(iter.max = 1e5, eval.max = 1e5, rel.tol = 1e-15).- lower
Numeric vector of lower bounds for
obj$par(the estimated parameter vector, i.e. after mapping and random-effects marginalization), passed tostats::nlminband used to clamp each Newton refinement step.NULL(default) is unbounded (-Inffor every element).- upper
Numeric vector of upper bounds for
obj$par, passed tostats::nlminband used to clamp each Newton refinement step.NULL(default) is unbounded (Inffor every element).- model
Function with signature
function(pars, data)passed toRTMB::MakeADFunviacmb. DefaultSPoRC_rtmb. Allows non-SPoRC RTMB models to be fit with the same optimization and Newton-refinement routines.- ...
Additional arguments forwarded to
RTMB::MakeADFun.
Value
The RTMB ADFun object with additional fields: $optim
(the nlminb output list, with $lower/$upper recording
the bounds used), $rep (the model report evaluated at
obj$env$last.par.best), and $data, $parameters,
$mapping, $random.