Learn R Programming

boral (version 0.4)

calc.logLik.lv0: Log-likelihood for a boral model with no latent variables

Description

Calculates the log-likelihood for a set of parameter estimates from an boral model with no latent variables. In such a model, the conditional and marginal logl-likelihood are equivalent.

Usage

calc.logLik.lv0(y, X = NULL, family, trial.size = NULL, lv.coefs, 
	X.coefs = NULL, site.coefs = NULL, cutoffs = NULL,
     powerparam = NULL)

Arguments

y
The response matrix 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 "b
trial.size
Either equal to NULL, 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 allowe
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 the model matrix X from the boral model. Defaults to NULL, in which it is assumed there are no covariates in the model.
site.coefs
Row effects estimates for the boral model. Defaults to NULL, in which case it is assumed there are no row effects in the model.
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:
  • logLikValue of the log-likelihood.
  • logLik.row.compA vector of the log-likelihood values for each row of $y$, such that sum(logLik.row.comp) = logLik.
  • logLik.col.compA vector of the log-likelihood values for each column of $y$, such that sum(logLik.row.comp) = logLik.

Details

A separate function to calc.marglogLik and calc.condlogLik has been written for boral models with with no latent variables, because the marginal and conditional log-likelihoods functions for such a model are equivalent. For an $n x p$ response matrix $y$, the log-likelihood is given by (with parameters where appropriate),

$$\log(f) = \sum_{i=1}^n \sum_{j=1}^p \log (f(y_{ij} | \alpha_i, \tau_k, \theta_{0j}, \bm{x}_i, \bm{\beta}_j, \phi_j)),$$

where $f(y_{ij}|\cdot)$ is the assumed distribution for column $j$, $\alpha_i$ is the row effect, $\tau_k$ are the cutoffs for proportional odds regression, $\theta_{0j}$ is the column-specific intercepts, $\bm{x}'_i$ is row $i$ of the model matrix, $\bm{\beta}_j$ are the column-specific coefficients relating to the model matrix of covariates, $\phi_j$ are column-specific dispersion parameters.

The function returns log-likelihood values both on a per-row and per-column basis. The latter is useful as it returns the log-likelihood for the separate GLMs fitted to each column of $y$.

See Also

calc.marglogLik for calculation of the marginal log-likelihood, and calc.condlogLik for calculation of the conditional log-likelihood for boral models with one or more latent variables.

Examples

Run this code
library(mvabund) ## Load a dataset from the mvabund package
data(spider)
y <- spider$abun
n <- nrow(y); p <- ncol(y); 

## Example 1 - NULL model with site effects and species specific intercepts
spider.fit.nb <- boral(y, family = "negative.binomial", num.lv = 0, 
     site.eff = TRUE, save.model = TRUE, calc.ics = FALSE)

## Extract all MCMC samples
fit.mcmc <- as.mcmc(spider.fit.nb$jags.model)[[1]]

## Find the posterior medians
coef.mat <- matrix(apply(fit.mcmc[,grep("all.params",colnames(fit.mcmc))],
     2,median),nrow=p)
site.coef.median <- apply(fit.mcmc[,grep("site.params", colnames(fit.mcmc))],
     2,median)

## Calculate the log-likelihood at the posterior median
calc.logLik.lv0(y, family = "negative.binomial",
     lv.coefs =  coef.mat, site.coefs = site.coef.median)


## Example 2 - Model without site effects, latent variables, 
##   but includes environmental covariates
spider.fit.nb2 <- boral(y, X = spider$x, family = "negative.binomial", num.lv = 0, 
     site.eff = FALSE, save.model = TRUE, calc.ics = FALSE)

## Extract all MCMC samples
fit.mcmc <- as.mcmc(spider.fit.nb2$jags.model)[[1]]

## Find the posterior medians
coef.mat <- matrix(apply(fit.mcmc[,grep("all.params",colnames(fit.mcmc))],
     2,median),nrow=p)
X.coef.mat <- matrix(apply(fit.mcmc[,grep("X.params",colnames(fit.mcmc))],
	2,median),nrow=p)

## Calculate the log-likelihood at the posterior median
calc.logLik.lv0(y, X = spider$x, family = "negative.binomial", 
	lv.coefs =  coef.mat, X.coefs = X.coef.mat)

Run the code above in your browser using DataLab