# 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 - NULL model with site effects only
spiderfit_nb <- boral(y, family = "negative.binomial", 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 log-likelihood at the posterior median
calc.logLik.lv0(y, family = "negative.binomial",
lv.coefs = coef_mat, row.eff = "fixed", row.params = site_coef)
## Example 2 - Model with environmental covariates and random row effects
X <- scale(spider$x)
spiderfit_nb2 <- boral(y, X = X, family = "negative.binomial", row.eff = "random",
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)
site.sigma <- list(ID1 =
median(fit_mcmc[,grep("row.sigma.ID1", mcmc_names)]))
## Calculate the log-likelihood at the posterior median
calc.logLik.lv0(y, X = spider$x, family = "negative.binomial",
row.eff = "random",lv.coefs = coef_mat, X.coefs = X_coef_mat,
row.params = site.sigma)
# }
Run the code above in your browser using DataLab