factory-neg2loglikFactory: Function Factory for 'neg2loglikelihood' with Unified Input...

neg2loglikFactoryR Documentation

Function Factory for neg2loglikelihood with Unified Input Arguments

Description

A function factory which generates a function of the form function(parameters) which returns the neg2loglikelihood.

Usage

neg2loglikFactory(y, X, distmat, covariance = NULL, cov.args = list(),
  chol.args = list(), Rstruct = NULL, covarianceFunction = NULL,
  choleskyFunction = NULL)

Arguments

y

Numeric vector. Dependent variable.

X

Optional data.frame containing covariates.

distmat

Distance matrix, either a numeric matrix or a spam object.

covariance

A function which takes as input an object containing distances (h), a vector of parameters (theta), and a list of optional settings (cov.args).

cov.args

A list of optional settings for a covariance function.

chol.args

A list of optional settings for a cholesky function.

Rstruct

A 'spam.chol.NgPeyton' object which represents the sparsity structure.

covarianceFunction

A function returned by covarianceFactory().

choleskyFunction

A function returned by choleskyFactory().

Details

This function factory returns a function of the form function(parameters) which computes the neg2loglikelihood for given input parameters. The purpose of this is to reduce the number of arguments that need to be specified by the user in a call to optim, or optimParallel. Furthermore, the function detects whether the input distmat is a spam object, and autonomously selects the appropriate method for computing the neg2loglikelihood.

The function is intended to be called from within mleFactory, but is also exported by NAMESPACE for users wishing to make use of the function. There are two distinct strategies available for using the function.

Option 1: the user may specify covariance, cov.args, chol.args, and Rstruct in the call. This syntax is more in line with the corresponding functions found in the spam package, yet still allows passing arguments for customizing the behaviour of the cholesky decomposition.

Option 2: the user may instead specify covarianceFunction and choleskyFunction, obtained from calls to covarianceFactory and choleskyFactory, respectively.

In both cases, the arguments y, X, and distmat are required input. Note that the two options are equivalent, apart from the second option allowing for more concise code.

Value

Returns function of the form function(parameters).

Author(s)

Thomas Caspar Fischer

References

Hadley Wickham (2015) Advanced R, CRC Press.

See Also

covarianceFactory and choleskyFactory

Examples

set.seed(63)
n <- 50
range <- 0.7
theta  <- c(range, 1, 1, 0, 0)

locs <- data.frame(x = runif(n), y = runif(n))
dmat  <- as.matrix(dist(locs))
Sigma <- cov.wendland(h = dmat, theta = theta)
y <- c(spam::rmvnorm(1, Sigma = Sigma))
X <- data.frame()

neg2loglikFun <- neg2loglikFactory(y = y, X = X, distmat = dmat,
  covariance = cov.wendland, cov.args = list(), chol.args = list())
result1 <- neg2loglikFun(theta)

covarianceFun <- covarianceFactory(cov.wendland, cov.args = list())
choleskyFun <- choleskyFactory(chol.args = list())
neg2loglikFun <- neg2loglikFactory(y = y, X = X, distmat = dmat,
  covarianceFunction = covarianceFun, choleskyFunction = choleskyFun)
result2 <- neg2loglikFun(theta)

GeneralizedWendland documentation built on June 22, 2022, 9:06 a.m.