r_nll {SpatialGEV}R Documentation

Calculate the negative marginal loglikelihood of the GEV-GP model.

Description

Calculate the negative marginal loglikelihood of the GEV-GP model.

Usage

r_nll(
  y,
  dd,
  a,
  log_b,
  s,
  hyperparam_a,
  hyperparam_b,
  hyperparam_s,
  kernel = "exp",
  beta_a = NULL,
  beta_b = NULL,
  beta_s = NULL,
  X_a = NULL,
  X_b = NULL,
  X_s = NULL,
  f_s = function(x) {     x },
  ...
)

Arguments

y

List of n locations each with n_obs[i] independent GEV realizations.

dd

An ⁠n x n⁠ distance matrix.

a

Vector of n location paramter

log_b

A numeric value or a vector of n log-transformed scale parameters if considered as a random effect.

s

A numeric value or a vector of n shape parameters

hyperparam_a

A vector of hyperparameters for a. See details.

hyperparam_b

A vector of hyperparameters for b. Must be provided if log_b is a vector. See details.

hyperparam_s

A vector of hyperparameters for f(s), where f() is a transformation function for s specifided using the f_s argument. Must be provided if s is a vector.

kernel

"exp" or "matern". Kernel function used to compute the covariance matrix for spatial random effects. Default is "exp".

beta_a

Numeric. Coefficients for mean of GP(a).

beta_b

Numeric. Coefficients for mean of GP(log_b).

beta_s

Numeric. Coefficients for mean of GP(s).

X_a

Design matrix for a. If not provided, this will a ⁠n_loc x 1⁠ column matrix of 1s.

X_b

Design matrix for log(b). If not provided and logb is a random effect, this will a ⁠n_loc x 1⁠ column matrix of 1s.

X_s

Design matrix for s. If not provided, this will a ⁠n_loc x 1⁠ column matrix of 1s.

f_s

A function f() used to transform s such that f(s) ~ GP(X_s*beta_s, Sigma(hyperparam_s)). Default is identitfy function: function(x){x}.

...

Additional arguments to pass to the kernel function, e.g. nu for the matern.

Details

This function is used to test if TMB and R output the same negative loglikelihood. If ⁠kernel="exp⁠, hyperparam_a/b/s should be c(sigma_a/b/s, ell_a/b/s), where sigma is the amplitude hyperparameter and ell is the smoothness hyperparameter for the exponential kernel. If ⁠kernel="matern⁠, hyperparam_a/b/s should be c(sigma_a/b, kappa_a/b/s), where sigma and kappa are hyperparameters for the Matern kernel. If only a is a spatial random effect and b is fixed, only hyperparam_a needs to be provided.

This function is used as the ground truth for testing hpp model likelihood.

Value

Scalar value of the negative marginal loglikelihood:

-logL(Data; spatial_random_effects, fixed_hyperparameters)

Examples

library(SpatialGEV)
a <- simulatedData$a
logb <- simulatedData$logb
logs <- simulatedData$logs
s <- exp(logs)
y <- simulatedData$y
locs <- simulatedData$locs
dd <- as.matrix(stats::dist(locs))
log_sigma_a <- -1; log_ell_a <- 5
log_sigma_b <- -2; log_ell_b <- 10
beta_a <- mean(a); beta_b <- mean(logb)
# Negative marginal log-likelihood produced in R using the exponential kernel
nll_r <- r_nll(y, dd, a=a, log_b=logb, s=s,
               hyperparam_a=c(exp(log_sigma_a), exp(log_ell_a)),
               hyperparam_b=c(exp(log_sigma_b), exp(log_ell_b)),
               kernel="exp", beta_a=beta_a, beta_b=beta_b)
# Negative marg loglik produced by TMB template
init_param <- list(beta_a=beta_a, beta_b=beta_b,
                   a=a, log_b=logb, s=log(s), 
                   log_sigma_a=log_sigma_a, 
                   log_ell_a=log_ell_a,
                   log_sigma_b=log_sigma_b, 
                   log_ell_b=log_ell_b)
adfun <- spatialGEV_fit(y, locs, random="ab",
                        init_param=init_param,
                        reparam_s="positive",
                        kernel="exp",
                        adfun_only=TRUE,
                        ignore_random=TRUE,
                        silent=TRUE)
nll_tmb <- adfun$fn(unlist(init_param))
nll_r - nll_tmb

[Package SpatialGEV version 1.0.0 Index]