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