boral (version 1.7)

calc.marglogLik: Marginal log-likelihood for an boral model

Description

Calculates the marginal log-likelihood for a set of parameter estimates from an boral model, whereby the latent variables and random effects (if applicable) are integrated out. The integration is performed using Monte Carlo integration. WARNING: As of version 1.6, this function will no longer be updated...use at your own peril!!!

Usage

calc.marglogLik(y, X = NULL, family, trial.size = 1, lv.coefs, 
     X.coefs = NULL, row.eff = "none", row.params = NULL, 
     row.ids = NULL,offset = NULL, num.lv, lv.mc = NULL, 
     cutoffs = NULL, powerparam = NULL)

Arguments

y

The response matrix that the boral model was fitted to.

X

The model matrix used in the boral model. Defaults to NULL, in which case it is assumed no model matrix was used.

family

Either a single element, or a vector of length equal to the number of columns in y. The former assumes all columns of y come from this distribution. The latter option allows for different distributions for each column of y. Elements can be one of "binomial" (with probit link), "poisson" (with log link), "negative.binomial" (with log link), "normal" (with identity link), "lnormal" for lognormal (with log link), "tweedie" (with log link), "exponential" (with log link), "gamma" (with log link), "beta" (with logit link), "ordinal" (cumulative probit regression).

Please see about.distributions for information on distributions available in boral overall.

trial.size

Either equal to a single element, or a vector of length equal to the number of columns in y. If a single element, then all columns assumed to be binomially distributed will have trial size set to this. If a vector, different trial sizes are allowed in each column of y. The argument is ignored for all columns not assumed to be binomially distributed. Defaults to 1, i.e. Bernoulli distribution.

lv.coefs

The column-specific intercept, coefficient estimates relating to the latent variables, and dispersion parameters from the boral model.

X.coefs

The coefficients estimates relating to X from the boral model. Defaults to NULL, in which it is assumed there are no covariates in the model.

row.eff

Single element indicating whether row effects are included as fixed effects ("fixed"), random effects ("random") or not included ("none") in the boral model. If random effects, they are drawn from a normal distribution with mean zero and standard deviation given by row.params. Defaults to "none".

row.params

Parameters corresponding to the row effect from the boral model. If row.eff = "fixed", then these are the fixed effects and should have length equal to the number of columns in y. If row.eff = "random", then this is standard deviation for the random effects normal distribution. If row.eff = "none", then this argument is ignored.

row.ids

A matrix with the number of rows equal to the number of rows in y, and the number of columns equal to the number of row effects to be included in the model. Element \((i,j)\) indicates to the cluster ID of row \(i\) in y for random effect eqnj; please see boral for details. Defaults to NULL, so that if row.params = NULL then the argument is ignored, otherwise if row.params is supplied then row.ids = matrix(1:nrow(y), ncol = 1) i.e., a single, row effect unique to each row. An internal check is done to see row.params and row.ids are consistent in terms of arguments supplied.

offset

A matrix with the same dimensions as the response matrix y, specifying an a-priori known component to be included in the linear predictor during fitting. Defaults to NULL.

num.lv

The number of latent variables used in the boral model. For boral models with no latent variables, please use calc.logLik.lv0 to calculate the log-likelihood.

lv.mc

A matrix used for performing the Monte Carlo integration. Defaults to NULL, in which case a matrix is generated within the function.

cutoffs

Common cutoff estimates from the boral model when any of the columns of y are ordinal responses. Defaults to NULL.

powerparam

Common power parameter from the boral model when any of the columns of y are tweedie responses. Defaults to NULL.

Value

A list with the following components:

logLik

Value of the marginal log-likelihood.

logLik.comp

A vector of the log-likelihood values for each row of y, such that sum(logLik.comp) = logLik.

Details

For an \(n x p\) response matrix y, suppose we fit an boral model with one or more latent variables. If we denote the latent variables by \(\bm{z}_i; i = 1,\ldots,n\), then the marginal log-likelihood is given by

$$ \log(f) = \sum_{i=1}^n \log ( \int \prod_{j=1}^p \{f(y_{ij} | \bm{z}_i, \beta_{0j}, \bm{\theta}_j, \ldots) \} f(\bm{z}_i) d\bm{z}_i), $$

where \(f(y_{ij}|\cdot)\) is the assumed distribution for column \(j\), \(\beta_{0j}\) are the column-specific intercepts, \(\bm{\theta}_j\) are the column-specific latent variable coefficients, and \(\ldots\) generically denotes anything else included in the model, e.g. row effects, dispersion parameters etc... The quantity \(f(\bm{z}_i)\) denotes the distribution of the latent variable, which is assumed to be standard multivariate Gaussian. Standard Monte Carlo integration is used for calculating the marginal likelihood. If lv.mc = NULL, the function automatically generates a matrix as lv.mc <- rmvnorm(1000, rep(0,num.lv)). If there is a need to apply this function numerous times, we recommend a matrix be inserted into lv.mc to speed up computation.

The key difference between this and the conditional likelihood (see calc.condlogLik) is that the marginal likelihood treats the latent variables as "random effects" and integrates over them, whereas the conditional likelihood treats the latent variables as "fixed effects".

Please note the function is written conditional on all regression coefficients. Therefore, if traits are included in the model, in which case the regression coefficients \(\beta_{0j}, \bm{\beta}_j\) become random effects instead (please see about.traits), then the calculation of the log-likelihood does NOT take this into account, i.e. does not marginalize over them! Likewise if more than two columns are ordinal responses, then the regression coefficients \(\beta_{0j}\) corresponding to these columns become random effects, and the calculation of the log-likelihood also does NOT take this into account, i.e. does not marginalize over them!

When a single \(\alpha_i\) random row effect is inclued, then the log-likelihood is calculated by integrating over this,

$$ \log(f) = \sum_{i=1}^n \log ( \int \prod_{j=1}^p \{f(y_{ij} | \bm{z}_i, \beta_{0j}, \alpha_i, \ldots)\} f(\bm{z}_i) f(\alpha_i) d\alpha_i ), $$

where \(f(\alpha_i)\) is the random effects distribution with mean zero and standard deviation given by the row.params. The integration is again performed using standard Monte Carlo integration. This naturally extends to multiple random row effects structures.

See Also

calc.condlogLik for calculation of the conditional log-likelihood; calc.logLik.lv0 to calculate the conditional/marginal log-likelihood for an boral model with no latent variables.

Examples

Run this code
# NOT RUN {
## NOTE: The values below MUST NOT be used in a real application;
## they are only used here to make the examples run quick!!!
example_mcmc_control <- list(n.burnin = 10, n.iteration = 100, 
     n.thin = 1)

library(mvabund) ## Load a dataset from the mvabund package
data(spider)
y <- spider$abun
n <- nrow(y)
p <- ncol(y)
    
## Example 1 - model with two latent variables, site effects, 
## 	and no environmental covariates
spiderfit_nb <- boral(y, family = "negative.binomial", 
    lv.control = list(num.lv = 2), row.eff = "fixed", save.model = TRUE, 
    mcmc.control = example_mcmc_control)

## Extract all MCMC samples
fit_mcmc <- get.mcmcsamples(spiderfit_nb) 
mcmc_names <- colnames(fit_mcmc)

## Find the posterior medians
coef_mat <- matrix(apply(fit_mcmc[,grep("lv.coefs",mcmc_names)],
    2,median),nrow=p)
site_coef <- list(ID1 = apply(fit_mcmc[,grep("row.coefs.ID1", mcmc_names)],
    2,median))
     
## Calculate the marginal log-likelihood at the posterior median
calc.marglogLik(y, family = "negative.binomial",
    lv.coefs = coef_mat, row.eff = "fixed", row.params = site_coef, 
    num.lv = 2)

	
## Example 2 - model with one latent variable, no site effects, 
## 	and environmental covariates
spiderfit_nb2 <- boral(y, X = spider$x, family = "negative.binomial", 
     lv.control = list(num.lv = 2), save.model = TRUE, 
     mcmc.control = example_mcmc_control)

## Extract all MCMC samples
fit_mcmc <- get.mcmcsamples(spiderfit_nb2) 
mcmc_names <- colnames(fit_mcmc)

## Find the posterior medians
coef_mat <- matrix(apply(fit_mcmc[,grep("lv.coefs",mcmc_names)],
    2,median),nrow=p)
X_coef_mat <- matrix(apply(fit_mcmc[,grep("X.coefs",mcmc_names)],
    2,median),nrow=p)

## Calculate the log-likelihood at the posterior median
calc.marglogLik(y, X = spider$x, family = "negative.binomial", 
    lv.coefs = coef_mat, X.coefs = X_coef_mat, num.lv = 2)	
# }

Run the code above in your browser using DataCamp Workspace