Fit a SPoRC RTMB model
fit_model.RdConstructs an RTMB automatic differentiation function via
RTMB::MakeADFun, optimises 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),
optimiser 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),
...
)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.- silent
Logical. If
TRUE, suppresses RTMB and optimiser console output. DefaultFALSE.- do_optim
Logical. If
TRUE(default), runsnlminband Newton refinement. IfFALSE, returns the un-optimisedMakeADFunobject only.- nlminb_control
Named list of control parameters passed to
stats::nlminb. Defaultlist(iter.max = 1e5, eval.max = 1e5, rel.tol = 1e-15).- ...
Additional arguments forwarded to
RTMB::MakeADFun.
Value
The RTMB ADFun object with three additional fields:
$optim (the nlminb output list), and $rep (the
model report evaluated at obj$env$last.par.best).