TMB-interface: Interface to TMB

TMB-interfaceR 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 func.

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 observation.name to TMB ?

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 using obj$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 either oneStepPredict or by obj$simulate. If your objective function is using an observation x, you simply need to run x <- OBS(x) inside the objective function. This will (1) allow oneStepPredict to change the class of x to "osa" (OSA-residuals) or (2) allow obj$simulate to change the class of x 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)

RTMB documentation built on Sept. 12, 2024, 6:45 a.m.