Learn R Programming

SpatioTemporal (version 0.9.2)

loglike.grad: Compute Gradient and Hessian for the Log-likelihood

Description

Computes finite difference gradients and hessians for the log-likelihood functions loglike and loglike.naive.

Usage

loglike.grad(x, mesa.data.model, type = "p", h = 0.001,
             diff.type = 0)

loglike.naive.grad(x, mesa.data.model, type = "p", h = 0.001, diff.type = 0)

loglike.hessian(x, mesa.data.model, type = "p", h = 0.001)

loglike.naive.hessian(x, mesa.data.model, type = "p", h = 0.001)

Arguments

x
Point at which to compute the gradient or hessian. See loglike.
mesa.data.model
Data structure holding observations, and information regarding which geographic and spatio-temporal covariates to use when fitting the model. See create.data.model and
type
A single character indicating the type of log-likelihood to compute. Valid options are "f", "p", and "r", for full, profile or restricted maximum likelihood (REML). Computation of the full log-likelihood req
h, diff.type
Step length and type of finite difference to use when computing gradients; positive values of diff.type gives forward differences, 0 gives central differences, and negative values gives backward differences. See

Value

encoding

latin1

Warning

loglike.naive.grad and loglike.naive.hessian may take very long time to run, use with extreme care.

Details

Uses gen.gradient and gen.hessian to compute finite difference derivatives of the log-likelihood function in loglike and loglike.naive. Used by the model fitting function fit.mesa.model and provided for users who want to implement their own model fitting.

See Also

Computes gradients and hessians for the log-likelihood function loglike.

Expected names for x are given by loglike.var.names. Used by the estimation functions fit.mesa.model and run.MCMC.

For general computation of gradient and hessian see gen.gradient and gen.hessian. For further log-likelihood computations see loglike, loglike.dim, and loglike.var.names.

Examples

Run this code
##load the data
data(mesa.data.model)

##Compute dimensions for the data structure
dim <- loglike.dim(mesa.data.model)

##Let's create random vectors of values
x <- runif(dim$nparam.cov)
x.all <- runif(dim$nparam)

##Compute the gradients
Gf <- loglike.grad(x.all, mesa.data.model, "f")
Gp <- loglike.grad(x, mesa.data.model, "p")
Gr <- loglike.grad(x, mesa.data.model, "r")

##And the Hessian, this may take some time...
Hf <- loglike.hessian(x.all, mesa.data.model, "f")
Hp <- loglike.hessian(x, mesa.data.model, "p")
Hr <- loglike.hessian(x, mesa.data.model, "r")

Run the code above in your browser using DataLab