
Simulate multi-species data for a joint species distribution model
Source:R/sim_jsdgam.R
sim_jsdgam.RdDraws a site-by-species response matrix from a latent factor model
and returns it in the long format jsdgam() takes. Each species
gets its own intercept and its own slope on a site-level
covariate. The species share n_lv latent factors, whose loadings
set the residual correlation between them.
This is the non-detection counterpart to sim_closure_unit_data(),
which simulates the same latent factor structure for the
occupancy and N-mixture families.
Usage
sim_jsdgam(
family = brms::negbinomial(),
n_species = 5L,
n_sites = 60L,
n_lv = 2L,
family_pars = list(),
seed = NULL
)
# S3 method for class 'mvgam_sim_jsdgam'
summary(object, ...)
# S3 method for class 'mvgam_sim_jsdgam_summary'
print(x, digits = 3L, ...)Arguments
- family
Observation family. Any family mvgam's response simulator draws from, plus
mvn(),mvt(),diri(),multi()andcateg(). Defaults tobrms::negbinomial().- n_species
Number of species. Defaults to
5.- n_sites
Number of sites. Defaults to
60.- n_lv
Number of latent factors. Defaults to
2.- family_pars
Optional named list of family parameters:
Psi(per-species scale formvn()/mvt()),nu(degrees of freedom formvt()),phi(concentration fordiri()),unit_total(trial total per site formulti()). Other families take their dispersion or scale, such assizefor a negative binomial orsigmafor a gaussian.- seed
Optional integer seed. The session's random state is restored when the call returns.
- object
A
mvgam_sim_jsdgamobject.- ...
Unused. Anything passed here is refused.
- x
A
mvgam_sim_jsdgam_summaryobject.- digits
Integer number of significant digits. Defaults to 3.
Value
An object of class mvgam_sim_jsdgam, inheriting
mvgam_sim, with elements:
data_trainLong
data.framewith columnssite,env,series(the species),yandtime(the unit indexjsdgam()keys on).data_testNULL, kept for consistency with the other simulators.y_array[n_sites x n_species]response matrix.familyThe family used.
n_species,n_sites,n_lvThe design.
truthList of the generative parameters:
loadings,lv,intercepts,env_slopes,env,residual_cov,residual_corandfamily_pars. Formvn(),mvt(),diri()andmulti()these include the defaultsPsi = 0.5,nu = 5,phi = 30andunit_total = 30.
summary() returns an mvgam_sim_jsdgam_summary list: the design,
the per-species generative parameters and the range of the
residual correlations the loadings imply, which a fit of these data
is asked to recover. print() shows it.
Details
The linear predictor for species k at site i is
intercept[k] + env_slope[k] * env[i] + Z[k, ] %*% lv[i, ], where
Z holds the loadings and lv the site scores. The residual
covariance the model targets is Z Z', plus a diagonal of squared
per-species scales for mvn() and mvt().
How that predictor becomes a response depends on the family.
Families whose species are drawn independently, such as
brms::negbinomial() or stats::poisson(), take one draw per
species. mvn() and mvt() add a per-species residual. The
composition families draw a site's species together. diri()
normalises gamma variates. multi() draws counts against a fixed
site total. categ() draws one species per site as a one-hot row.
A composition family identifies its loadings only up to a shift
shared by the species, which is the subspace mvgam's
sum_to_zero_vector parameterisation covers. The loadings are
centred for those families, which puts the simulated truth in the
same subspace a fit explores.
How much of the factor structure survives into the response
depends on the observation noise. The loadings are drawn with
standard deviation 0.7. mvn() and mvt() take their per-species
scale from Psi, which defaults to 0.5. Every other family takes
the scale its own simulator defaults to. A gaussian's sigma
defaults to 1, which can hide the species correlations from the
data. Pass family_pars = list(sigma = 0.5) for a gaussian whose
correlations a fit can recover.
See also
jsdgam() fits the model these data come from.
sim_closure_unit_data() simulates occupancy and N-mixture data.
sim_mvgam() simulates time series. The
joint species distribution article
fits and interprets models of this kind.
Other simulation:
sim_closure_unit_data(),
sim_mvgam()
Examples
simdat <- sim_jsdgam(
family = brms::negbinomial(),
n_species = 4L,
n_sites = 30L,
n_lv = 2L,
seed = 1L
)
head(simdat$data_train)
#> site env series y time
#> 1 1 0.5757814 sp_1 4 1
#> 2 1 0.5757814 sp_2 1 1
#> 3 1 0.5757814 sp_3 0 1
#> 4 1 0.5757814 sp_4 1 1
#> 5 2 -0.3053884 sp_1 0 2
#> 6 2 -0.3053884 sp_2 2 2
summary(simdat)
#> Simulated joint species distribution dataset (sim_jsdgam)
#> Family : negbinomial
#> Species : 4
#> Sites : 30
#> Latent factors: 2
#> Rows : 120
#>
#> True generative parameters
#> Intercepts : 1.100, 0.763, -0.165, -0.253
#> Env slopes : 0.697, 0.557, -0.689, -0.707
#> Residual correlations: [-0.680, 0.999]
#>
#> Simulated response
#> Range : [ 0, 18]
#> Zero fraction : 0.317
# A composition family draws each site's species together
comp <- sim_jsdgam(family = diri(), n_species = 4L, n_sites = 20L,
seed = 2L)
rowSums(comp$y_array)[1:3]
#> [1] 1 1 1
# Next: fit with jsdgam(), then compare residual_cor() of the fit
# with simdat$truth$residual_cor