optimize_theta {aghq}R Documentation

Obtain function information necessary for performing quadrature

Description

This function computes the two pieces of information needed about the log posterior to do adaptive quadrature: the mode, and the hessian at the mode. It is designed for use within aghq::aghq, but is exported in case users need to debug the optimization process and documented in case users want to write their own optimizations.

Usage

optimize_theta(ff, startingvalue, control = default_control(), ...)

Arguments

ff

A list with three elements:

  • fn: function taking argument theta and returning a numeric value representing the log-posterior at theta

  • gr: function taking argument theta and returning a numeric vector representing the gradient of the log-posterior at theta

  • he: function taking argument theta and returning a numeric matrix representing the hessian of the log-posterior at theta

The user may wish to use numDeriv::grad and/or numDeriv::hessian to obtain these. Alternatively, the user may consider the TMB package. This list is deliberately formatted to match the output of TMB::MakeADFun.

startingvalue

Value to start the optimization. ff$fn(startingvalue), ff$gr(startingvalue), and ff$he(startingvalue) must all return appropriate values without error.

control

A list with elements

  • method: optimization method to use:

    • 'sparse_trust' (default): trustOptim::trust.optim with method = 'sparse'

    • 'SR1' (default): trustOptim::trust.optim with method = 'SR1'

    • 'trust': trust::trust

    • 'BFGS': optim(...,method = "BFGS")

    Default is 'sparse_trust'.

  • optcontrol: optional: a list of control parameters to pass to the internal optimizer you chose. The aghq package uses sensible defaults.

...

Additional arguments to be passed to ff$fn, ff$gr, and ff$he.

Value

A list with elements

See Also

Other quadrature: aghq(), get_hessian(), get_log_normconst(), get_mode(), get_nodesandweights(), get_numquadpoints(), get_opt_results(), get_param_dim(), laplace_approximation(), marginal_laplace_tmb(), marginal_laplace(), nested_quadrature(), normalize_logpost(), plot.aghq(), print.aghqsummary(), print.aghq(), print.laplacesummary(), print.laplace(), print.marginallaplacesummary(), summary.aghq(), summary.laplace(), summary.marginallaplace()

Examples

# Poisson/Exponential example
logfteta <- function(eta,y) {
  sum(y) * eta - (length(y) + 1) * exp(eta) - sum(lgamma(y+1)) + eta
}

y <- rpois(10,5) # Mode should be (sum(y) + 1) / (length(y) + 1)

objfunc <- function(x) logfteta(x,y)
funlist <- list(
  fn = objfunc,
  gr = function(x) numDeriv::grad(objfunc,x),
  he = function(x) numDeriv::hessian(objfunc,x)
)

optimize_theta(funlist,1.5)
optimize_theta(funlist,1.5,control = default_control(method = "trust"))
optimize_theta(funlist,1.5,control = default_control(method = "BFGS"))


[Package aghq version 0.4.1 Index]