View source: R/mvgam_trend_types.R
| ZMVN | R Documentation |
Set up latent correlated multivariate Gaussian residual processes in mvgam. This function does not evaluate its arguments – it exists purely to help set up a model with particular error processes
ZMVN(unit = time, gr = NA, subgr = series)
unit |
The unquoted name of the variable that represents the unit of
analysis in |
gr |
An optional grouping variable, which must be a
where |
subgr |
A subgrouping Models that use the hierarchical correlations (by supplying a value for
For example, if you are modelling counts for a group of species (labelled
as Internally,
|
An object of class mvgam_trend, which contains a list of
arguments to be interpreted by the parsing functions in mvgam
## Not run:
# Simulate counts of four species over ten sampling locations
site_dat <- data.frame(
site = rep(1:10, 4),
species = as.factor(sort(rep(letters[1:4], 10))),
y = c(NA, rpois(39, 3))
)
head(site_dat)
# Set up a correlated residual (i.e. Joint Species Distribution) model
trend_model <- ZMVN(unit = site, subgr = species)
mod <- mvgam(
y ~ species,
trend_model = ZMVN(unit = site, subgr = species),
data = site_dat,
chains = 2,
silent = 2
)
# Inspect the estimated species-species residual covariances
mcmc_plot(mod, variable = 'Sigma', regex = TRUE, type = 'hist')
# A hierarchical correlation example
Sigma <- matrix(
c(1, -0.4, 0.5,
-0.4, 1, 0.3,
0.5, 0.3, 1),
byrow = TRUE,
nrow = 3
)
make_site_dat <- function(...) {
errors <- mgcv::rmvn(
n = 30,
mu = c(0.6, 0.8, 1.8),
V = Sigma
)
site_dat <- do.call(rbind, lapply(1:3, function(spec) {
data.frame(
y = rpois(30, lambda = exp(errors[, spec])),
species = paste0('species', spec),
site = 1:30
)
}))
site_dat
}
site_dat <- rbind(
make_site_dat() %>%
dplyr::mutate(group = 'group1'),
make_site_dat() %>%
dplyr::mutate(group = 'group2')
) %>%
dplyr::mutate(
species = as.factor(species),
group = as.factor(group)
)
# Fit the hierarchical correlated residual model
mod <- mvgam(
y ~ species,
trend_model = ZMVN(unit = site, gr = group, subgr = species),
data = site_dat,
chains = 2,
silent = 2
)
# Inspect the estimated species-species residual covariances
mcmc_plot(mod, variable = 'Sigma', regex = TRUE, type = 'hist')
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.