Learn R Programming

pomp (version 5.2)

trajectory matching: Trajectory matching

Description

Estimation of parameters for deterministic POMP models via trajectory matching.

Usage

# S4 method for data.frame
traj_objfun(
  data,
  est = character(0),
  fail.value = NA,
  ode_control = list(),
  params,
  rinit,
  skeleton,
  dmeasure,
  partrans,
  ...,
  verbose = getOption("verbose", FALSE)
)

# S4 method for pomp traj_objfun( data, est = character(0), fail.value = NA, ode_control = list(), ..., verbose = getOption("verbose", FALSE) )

# S4 method for traj_match_objfun traj_objfun( data, est, fail.value, ode_control, ..., verbose = getOption("verbose", FALSE) )

Value

traj_objfun constructs a stateful objective function for spectrum matching. Specifically, traj_objfun returns an object of class ‘traj_match_objfun’, which is a function suitable for use in an optim-like optimizer. In particular, this function takes a single numeric-vector argument that is assumed to contain the parameters named in est, in that order. When called, it will return the negative log likelihood. It is a stateful function: Each time it is called, it will remember the values of the parameters and its estimate of the log likelihood.

Arguments

data

either a data frame holding the time series data, or an object of class ‘pomp’, i.e., the output of another pomp calculation. Internally, data will be internally coerced to an array with storage-mode double.

est

character vector; the names of parameters to be estimated.

fail.value

optional numeric scalar; if non-NA, this value is substituted for non-finite values of the objective function. It should be a large number (i.e., bigger than any legitimate values the objective function is likely to take).

ode_control

optional list; the elements of this list will be passed to ode if the skeleton is a vectorfield, and ignored if it is a map.

params

optional; named numeric vector of parameters. This will be coerced internally to storage mode double.

rinit

simulator of the initial-state distribution. This can be furnished either as a C snippet, an R function, or the name of a pre-compiled native routine available in a dynamically loaded library. Setting rinit=NULL sets the initial-state simulator to its default. For more information, see rinit specification.

skeleton

optional; the deterministic skeleton of the unobserved state process. Depending on whether the model operates in continuous or discrete time, this is either a vectorfield or a map. Accordingly, this is supplied using either the vectorfield or map fnctions. For more information, see skeleton specification. Setting skeleton=NULL removes the deterministic skeleton.

dmeasure

evaluator of the measurement model density, specified either as a C snippet, an R function, or the name of a pre-compiled native routine available in a dynamically loaded library. Setting dmeasure=NULL removes the measurement density evaluator. For more information, see dmeasure specification.

partrans

optional parameter transformations, constructed using parameter_trans.

Many algorithms for parameter estimation search an unconstrained space of parameters. When working with such an algorithm and a model for which the parameters are constrained, it can be useful to transform parameters. One should supply the partrans argument via a call to parameter_trans. For more information, see parameter_trans. Setting partrans=NULL removes the parameter transformations, i.e., sets them to the identity transformation.

...

additional arguments will modify the model structure

verbose

logical; if TRUE, diagnostic messages will be printed to the console.

Note for Windows users

Some Windows users report problems when using C snippets in parallel computations. These appear to arise when the temporary files created during the C snippet compilation process are not handled properly by the operating system. To circumvent this problem, use the cdir and cfile options to cause the C snippets to be written to a file of your choice, thus avoiding the use of temporary files altogether.

Important Note

Since pomp cannot guarantee that the final call an optimizer makes to the function is a call at the optimum, it cannot guarantee that the parameters stored in the function are the optimal ones. Therefore, it is a good idea to evaluate the function on the parameters returned by the optimization routine, which will ensure that these parameters are stored.

Warning! Objective functions based on C snippets

If you use C snippets (see Csnippet), a dynamically loadable library will be built. As a rule, pomp functions load this library as needed and unload it when it is no longer needed. The stateful objective functions are an exception to this rule. For efficiency, calls to the objective function do not execute pompLoad or pompUnload: rather, it is assumed that pompLoad has been called before any call to the objective function. When a stateful objective function using one or more C snippets is created, pompLoad is called internally to build and load the library: therefore, within a single R session, if one creates a stateful objective function, one can freely call that objective function and (more to the point) pass it to an optimizer that calls it freely, without needing to call pompLoad. On the other hand, if one retrieves a stored objective function from a file, or passes one to another R session, one must call pompLoad before using it. Failure to do this will typically result in a segmentation fault (i.e., it will crash the R session).

Details

In trajectory matching, one attempts to minimize the discrepancy between a POMP model's predictions and data under the assumption that the latent state process is deterministic and all discrepancies between model and data are due to measurement error. The measurement model likelihood (dmeasure), or rather its negative, is the natural measure of the discrepancy.

Trajectory matching is a generalization of the traditional nonlinear least squares approach. In particular, if, on some scale, measurement errors are normal with constant variance, then trajectory matching is equivalent to least squares on that particular scale.

traj_objfun constructs an objective function that evaluates the likelihood function. It can be passed to any one of a variety of numerical optimization routines, which will adjust model parameters to minimize the discrepancies between the power spectrum of model simulations and that of the data.

See Also

optim, subplex, nloptr

More on methods for deterministic process models: flow(), skeleton specification, skeleton(), trajectory()

More on maximization-based estimation methods: mif2(), nonlinear forecasting, probe matching, spectrum matching

Examples

Run this code
# \donttest{

  ricker() |>
    traj_objfun(
      est=c("r","sigma","N_0"),
      partrans=parameter_trans(log=c("r","sigma","N_0")),
      paramnames=c("r","sigma","N_0"),
      ) -> f

  f(log(c(20,0.3,10)))

  if (require(subplex)) {
    subplex(fn=f,par=log(c(20,0.3,10)),control=list(reltol=1e-5)) -> out
  } else {
    optim(fn=f,par=log(c(20,0.3,10)),control=list(reltol=1e-5)) -> out
  }

  f(out$par)

  if (require(ggplot2)) {

    f |>
      trajectory(format="data.frame") |>
      ggplot(aes(x=time,y=N))+geom_line()+theme_bw()

  }

# }

Run the code above in your browser using DataLab