TMB-interface {RTMB} | R Documentation |
Interface to TMB
Description
Interface to TMB
Usage
MakeADFun(
func,
parameters,
random = NULL,
profile = NULL,
integrate = NULL,
intern = FALSE,
map = list(),
ADreport = FALSE,
silent = FALSE,
ridge.correct = FALSE,
...
)
sdreport(obj, ...)
ADREPORT(x)
REPORT(x)
getAll(..., warn = TRUE)
OBS(x)
checkConsistency(obj, fast = TRUE, ...)
Arguments
func |
Function taking a parameter list (or parameter vector) as input. |
parameters |
Parameter list (or parameter vector) used by |
random |
As MakeADFun. |
profile |
As MakeADFun. |
integrate |
As MakeADFun. |
intern |
As MakeADFun. |
map |
As MakeADFun. |
ADreport |
As MakeADFun. |
silent |
As MakeADFun. |
ridge.correct |
Experimental |
... |
Passed to TMB |
obj |
TMB model object (output from MakeADFun) |
x |
Observation object |
warn |
Give a warning if overwriting an existing object? |
fast |
Pass |
Details
MakeADFun builds a TMB model object mostly compatible with the TMB package and with an almost identical interface.
The main difference in RTMB is that the objective function and the data is now given via a single argument func
. Because func
can be a closure, there is no need for an explicit data argument to MakeADFun (see examples).
Value
TMB model object.
Functions
-
MakeADFun()
: Interface to MakeADFun. -
sdreport()
: Interface to sdreport. -
ADREPORT()
: Can be used inside the objective function to report quantities for which uncertainties will be calculated by sdreport. -
REPORT()
: Can be used inside the objective function to report quantities via the model object usingobj$report()
. -
getAll()
: Can be used to assign all parameter or data objects from a list inside the objective function. -
OBS()
: Mark the observation to be used by eitheroneStepPredict
or byobj$simulate
. If your objective function is using an observationx
, you simply need to runx <- OBS(x)
inside the objective function. This will (1) allowoneStepPredict
to change the class ofx
to"osa"
(OSA-residuals) or (2) allowobj$simulate
to change the class ofx
to"simref"
(Simulation) on request. -
checkConsistency()
: Interface to checkConsistency.
Examples
## Objective with data from the user workspace
data(rivers)
f <- function(p) { -sum(dnorm(rivers, p$mu, p$sd, log=TRUE)) }
obj <- MakeADFun(f, list(mu=0, sd=1), silent=TRUE)
opt <- nlminb(obj$par, obj$fn, obj$gr)
sdreport(obj)
## Same objective with an explicit data argument
f <- function(p, data) { -sum(dnorm(data, p$mu, p$sd, log=TRUE)) }
cmb <- function(f, d) function(p) f(p, d) ## Helper to make closure
obj <- MakeADFun(cmb(f, rivers), list(mu=0, sd=1), silent=TRUE)
## 'REML trick'
obj2 <- MakeADFun(cmb(f, rivers), list(mu=0, sd=1), random="mu", silent=TRUE)
opt2 <- nlminb(obj2$par, obj2$fn, obj2$gr)
sdreport(obj2) ## Compare with sd(rivers)
## Single argument vector function with numeric 'parameters'
fr <- function(x) { ## Rosenbrock Banana function
x1 <- x[1]
x2 <- x[2]
100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
obj <- MakeADFun(fr, numeric(2), silent=TRUE)
nlminb(c(-1.2, 1), obj$fn, obj$gr, obj$he)