Skip to contents

For proportions measured together on each unit that sum to one, such as the cover shares of several species at a site. The data hold one row per component at each unit, whose responses sum to one within the unit. Each unit's vector of K shares is Dirichlet with concentrations softmax(mu) * phi. mu holds the components' linear predictors. phi sets how tightly the shares gather around softmax(mu) (Aitchison 1982).

Usage

diri()

Value

A family object to pass to jsdgam() or mvgam().

Details

jsdgam() fits the family, taking the unit and component columns and the number of latent factors n_lv. The factors give the components correlated departures from their expected shares (Warton et al. 2015).

Identification

The softmax is unchanged when the same constant is added to every component. The data then identify neither a shift of the linear predictors that every component shares nor a shift of the loadings that every component shares. mvgam removes both:

  • The first component is the reference. Its linear predictor is subtracted from every component's before the softmax, as in brms's dirichlet() family. A term that acts on every component alike, such as Intercept or env in y ~ env * series, then cancels. Its posterior is its prior, student_t(3, 0, 2.5) by default. The series terms are contrasts with the first component. jsdgam() warns when the formula has no term that varies by component.

  • Each column of the loadings Z sums to zero, through Stan's sum_to_zero_vector type.

A loadings_prior combines with that constraint.

Regression on phi

bf(y ~ env * series, phi ~ env) models the concentration. A unit's density takes one phi. Its covariates must take one value per unit. mvgam refuses a covariate that varies within a unit. The coefficients of phi take a flat prior by default. prior(student_t(3, 0, 2.5), class = b, dpar = phi) regularises them.

Post-fit conventions

The density is joint over a unit's components, which makes the unit the grain of log_lik(), loo() and waic() (Vehtari et al. 2017). log_lik() gives each unit's log density on its first row and NA on the others. loo() then counts each unit once. loo_compare() needs every model it compares scored at that grain.

posterior_linpred() gives mu per row before the reference subtraction. posterior_epred() gives the expected shares, which sum to one within each unit.

species_effects(), variance_partition() and forecast_uncertainty(scale = "link") report the predictor centred across each unit's components, the centred log-ratio scale. A shift shared by every component then drops out.

References

Aitchison, J. (1982). The statistical analysis of compositional data. Journal of the Royal Statistical Society Series B, 44(2):139-177.

Vehtari, A., Gelman, A. and Gabry, J. (2017). Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC. Statistics and Computing, 27:1413-1432. doi:10.1007/s11222-016-9696-4

Warton, D. I., Blanchet, F. G., O'Hara, R. B., Ovaskainen, O., Taskinen, S., Walker, S. C. and Hui, F. K. C. (2015). So many variables: joint modeling in community ecology. Trends in Ecology and Evolution, 30(12):766-779. doi:10.1016/j.tree.2015.09.007

Heaps, S. E. and Jermyn, I. H. (2024). Structured prior distributions for the covariance matrix in latent factor models. Statistics and Computing, 34:143. doi:10.1007/s11222-024-10454-0

See also

Examples

# \dontrun{
# Shares of three categories at 60 sites along an environmental
# gradient
simdat <- sim_jsdgam(family = diri(), n_species = 3L, n_sites = 60L,
                     seed = 2L)
head(simdat$data_train)
#>   site        env series          y time
#> 1    1  0.7079547   sp_1 0.01630229    1
#> 2    1  0.7079547   sp_2 0.14446708    1
#> 3    1  0.7079547   sp_3 0.83923063    1
#> 4    2 -0.2396980   sp_1 0.10325166    2
#> 5    2 -0.2396980   sp_2 0.10469336    2
#> 6    2 -0.2396980   sp_3 0.79205498    2

# `env * series` gives each category its own slope
fit <- jsdgam(
  formula        = y ~ env * series,
  factor_formula = ~ -1,
  data           = simdat$data_train,
  unit           = time, species = series,
  family         = diri(),
  n_lv           = 2L,
  chains         = 2L,
  silent         = 2
)
#> Warning: 2 of 2 chains had an E-BFMI less than 0.3.
#> See https://mc-stan.org/misc/warnings for details.

# The species terms are contrasts with the first species. `Intercept`
# and `env` act on every species alike. The softmax cancels them. Their
# posteriors repeat the prior.
summary(fit)
#>  Family: diri 
#>   Links: mu = identity; phi = log 
#> Formula: y ~ env * series 
#>    Data: simdat$data_train (Number of observations: 180) 
#> Species: 3 
#>  Trends: 2 latent factors 
#>   Draws: 2 chains, each with iter = 2000; warmup = 1000; thin = 1; 
#>          total post-warmup draws = 2000
#> 
#> == Observation Model ==
#> Regression Coefficients:
#>                Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> Intercept         -1.42      4.21    -9.46     6.87 1.00     1977      796
#> env                0.05      3.87    -7.48     8.72 1.00     2594      921
#> seriessp_2         1.76      0.19     1.40     2.15 1.00      578     1155
#> seriessp_3         2.42      0.29     1.85     2.98 1.00      617     1092
#> env:seriessp_2    -0.04      0.15    -0.35     0.25 1.00      779     1045
#> env:seriessp_3     1.26      0.24     0.79     1.73 1.00      690     1057
#> 
#> Further Distributional Parameters:
#>     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> phi    36.43     14.93    17.98    75.91 1.02      167      370
#> 
#> == Trend Model ==
#> Factor Loadings:
#>                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> Z_tilde[sp_1,1]     1.00      0.13     0.76     1.27 1.01      436     1121
#> Z_tilde[sp_2,1]    -0.07      0.07    -0.22     0.06 1.00      394     1227
#> Z_tilde[sp_3,1]    -0.93      0.11    -1.16    -0.73 1.00      698     1073
#> Z_tilde[sp_2,2]     0.21      0.10     0.02     0.39 1.02      196      564
#> Z_tilde[sp_3,2]    -0.21      0.10    -0.39    -0.02 1.02      196      564
#> 
#> Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
#> and Tail_ESS are effective sample size measures, and Rhat is the potential
#> scale reduction factor on split chains (at convergence, Rhat = 1).
#> 
#> Next steps:
#>   - `pp_check(fit)`: posterior predictive checks
#>   - `shared_variation(fit)`: covariance the factors imply, unchanged by rotation
#>   - `residual_cor(fit)`: implied correlations among species
#>   - `ordinate(fit)`: biplot of the factor scores and loadings
#>   - `loo(fit)` / `loo_compare(...)`: model fit + comparison
#>   - `conditional_effects(fit)`: covariate effects
#> Use `how_to_cite(fit)` for a methods paragraph and `methods_md(fit)` for its equations.

# Each species' response to env relative to the average species
species_effects(fit, variables = "env")
#> Species with a supported response, prob_nonzero > 0.95, 2000 draws
#> 
#>  effect positive negative species
#>  env    1        2        3      
#> 
#> summary() gives each species' estimate and interval.

# The expected shares move along the gradient
conditional_effects(fit, effects = "env:series")


# Each site's expected shares sum to one
pe <- posterior_epred(fit, ndraws = 50L)
first_site <- which(simdat$data_train$time == 1L)
rowSums(pe[1:5, first_site])
#> [1] 1 1 1 1 1
# }