Skip to contents

Gives each species' response to each named covariate, draw by draw, with the posterior probability of its sign. A jsdgam() fit with trait_slopes also gives each trait's effect on those responses, the fourth-corner coefficients. The heatmaps show which species respond positively, which negatively and which the data leave uncertain.

Usage

species_effects(object, ...)

# S3 method for class 'mvgam'
species_effects(
  object,
  variables,
  ...,
  prob_threshold = 0.95,
  probs = c(0.025, 0.975),
  robust = FALSE,
  ndraws = NULL,
  draw_ids = NULL,
  resp = NULL
)

# S3 method for class 'mvgam_species_effects'
summary(object, type = c("species", "traits"), ...)

# S3 method for class 'mvgam_species_effects'
print(x, digits = 2L, ...)

# S3 method for class 'mvgam_species_effects'
plot(x, type = c("species", "traits"), supported_only = FALSE, ...)

Arguments

object

A fitted mvgam object of several series.

...

Unused. Anything passed here is refused.

variables

Character vector of the covariates to report, each a column of the data that a formula of the model uses.

prob_threshold

The posterior probability of its sign an effect needs to count as supported, at least 0.5 and below 1. Defaults to 0.95. It sets the sig column of summary() and the effects plot() outlines.

probs

Two probabilities for the lower and upper bounds of the credible interval. Defaults to c(0.025, 0.975).

robust

Logical. TRUE summarises each effect by its median and median absolute deviation. Defaults to FALSE, the mean and the standard deviation.

ndraws, draw_ids

The posterior draws to use, as hindcast.mvgam() takes them. NULL for both uses every draw.

resp

The response to report, needed on a model with several.

type

"species" (the default) for the species by covariate panel, or "traits" for the trait by covariate panel of a trait_slopes fit.

x

An mvgam_species_effects object.

digits

Integer number of significant digits. Defaults to 2.

supported_only

Logical. TRUE leaves blank every effect whose prob_nonzero is at most prob_threshold. Defaults to FALSE.

Value

An object of class mvgam_species_effects. print() counts the supported positive and negative responses to each covariate. summary() returns a tibble of each effect's estimate, interval and sign probabilities, with sig marking the supported ones. plot() draws a heatmap of species by covariate, or of trait by covariate with type = "traits". The species element holds the summary matrices, [species, effect]. The draws element holds the [draws, species, effect] array. A trait_slopes fit adds a traits element of the same form.

Details

Species effects. A species' response to a numeric covariate is the average slope of its linear predictor, over the rows the species was fitted to, as marginaleffects::avg_slopes() gives it with type = "link". A linear term gives the species' own coefficient, such as b_env plus b_env:seriessp_2 in y ~ env * series. A smooth or an interaction gives the slope averaged over the species' rows. A smooth that rises and falls can average to a slope near zero. conditional_effects.mvgam() shows its shape. A covariate in trend_formula, including one written with lv_axis(), reaches each species through its loadings. A factor covariate gives one effect per level, its difference from the reference level.

A diri(), multi() or categ() fit gives each species' share through a softmax, which ignores a slope every species shares. Each draw is then centred across the species. An effect is the slope of the species' log share relative to the geometric mean of the shares, the centred log-ratio scale.

An effect is on the link scale, per unit of the covariate. Effects of covariates measured in different units compare better when the covariates are standardised. A covariate that only a distributional formula uses, such as the detection formula of occ(), moves no species' linear predictor and is refused.

Trait effects. trait_slopes = ~ trait in jsdgam() makes each species' response depend on its traits. A trait effect is how far the response moves per unit of the trait, with each species' own deviation held. A linear term gives its coefficient, such as b_env:size. A smooth of the covariate or of the trait gives the effect averaged over the fitted rows. A factor trait gives one effect per level, its difference from the reference level. A covariate of factor_formula reaches no trait. Its trait effects are zero.

Support. prob_nonzero is the larger of the posterior probabilities that an effect is positive and that it is negative. An effect is supported when prob_nonzero exceeds prob_threshold.

References

Ovaskainen, O. and Abrego, N. (2020). Joint Species Distribution Modelling: With Applications in R. Cambridge University Press. doi:10.1017/9781108591720

See also

residual_cor() for the associations the covariates leave, conditional_effects.mvgam() for the shape of an effect, variance_partition() for how much each source moves a species

Author

Nicholas J Clark

Examples

# \dontrun{
# Detections of 6 species at 80 sites, 4 visits each. Occupancy
# responds to the site covariate `env` and detection to the time of
# day `tod_c`. Two latent factors drive the species associations.
simdat <- sim_closure_unit_data(
  family    = occ(),
  n_species = 6L,
  n_sites   = 80L,
  n_visits  = 4L,
  n_lv      = 2L,
  seed      = 1L
)

# A joint occupancy model. Each species has its own occupancy
# intercept and `env` slope, plus its own detection intercept. Two
# latent factors take up the residual association.
mod <- jsdgam(
  formula        = bf(y ~ series * env, p ~ series + tod_c),
  factor_formula = ~ -1,
  data           = simdat$data_train,
  family         = occ(),
  n_lv           = 2L,
  chains         = 2,
  silent         = 2
)
#> Warning: 1 of 2000 (0.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.

summary(mod)
#>  Family: occ 
#>   Links: mu = logit; p = logit 
#> Formula: y ~ series * env 
#>          p ~ series + tod_c 
#>    Data: simdat$data_train (Number of observations: 1920) 
#> Species: 6 
#>  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.10      0.60     0.16     2.51 1.01      407      380
#> p_Intercept       -0.22      0.18    -0.57     0.12 1.00     1281     1556
#> seriessp_2        -0.44      0.70    -1.93     0.78 1.00      492      417
#> seriessp_3        -1.31      0.66    -2.83    -0.21 1.01      498      424
#> seriessp_4        -1.52      1.13    -3.46     1.12 1.01      633      782
#> seriessp_5        -0.21      0.73    -1.78     1.14 1.01      529      632
#> seriessp_6        -3.34      0.81    -5.14    -1.92 1.00      786     1014
#> env               -2.33      0.86    -4.27    -1.00 1.01      363      404
#> seriessp_2:env     1.62      0.97    -0.09     3.71 1.01      471      506
#> seriessp_3:env     2.25      0.91     0.77     4.22 1.01      380      475
#> seriessp_4:env     0.05      1.40    -3.01     2.63 1.01      552      587
#> seriessp_5:env     1.92      0.96     0.32     4.08 1.01      422      505
#> seriessp_6:env     0.29      1.13    -1.87     2.69 1.01      418      417
#> p_seriessp_2       0.56      0.23     0.10     1.03 1.00     1606     1545
#> p_seriessp_3       1.12      0.26     0.63     1.63 1.00     1874     1689
#> p_seriessp_4      -1.06      0.41    -1.92    -0.30 1.00     1376     1134
#> p_seriessp_5       0.52      0.24     0.04     0.98 1.00     1513     1586
#> p_seriessp_6       0.62      0.34    -0.03     1.29 1.00     2235     1524
#> p_tod_c            0.01      0.02    -0.03     0.05 1.00     6094     1134
#> 
#> == Trend Model ==
#> Factor Loadings:
#>                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> Z_tilde[sp_1,1]     1.13      0.67     0.17     2.67 1.01      440      817
#> Z_tilde[sp_2,1]     0.24      0.94    -1.57     2.13 1.00     1500     1616
#> Z_tilde[sp_3,1]    -0.03      0.73    -1.47     1.51 1.00     1123     1302
#> Z_tilde[sp_4,1]     0.32      0.95    -1.72     2.17 1.00     1307     1582
#> Z_tilde[sp_5,1]     0.53      1.01    -1.50     2.42 1.00     1373     1641
#> Z_tilde[sp_6,1]     0.40      0.90    -1.43     2.07 1.00     1080     1210
#> Z_tilde[sp_2,2]     1.04      0.69     0.04     2.66 1.00     1178     1607
#> Z_tilde[sp_3,2]     0.51      0.71    -0.98     1.91 1.00     1339     1419
#> Z_tilde[sp_4,2]     0.26      0.95    -1.76     2.11 1.00     1533     1410
#> Z_tilde[sp_5,2]    -0.89      0.93    -2.61     1.23 1.00     1296     1652
#> Z_tilde[sp_6,2]    -0.24      0.92    -1.98     1.75 1.00     1507     1320
#> 
#> 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, type = "fit_stat")`: goodness of fit of unit totals
#>   - `plot(hindcast(fit, type = "latent_state"))`: posterior occupancy of each unit
#>   - `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
#> Use `how_to_cite(fit)` for a methods paragraph and `methods_md(fit)` for its equations.

# The effect of `env` on the chance of a detection, occupancy times
# detection
conditional_effects(mod)





# Each species' response to env, outlined where its sign is clear
plot(species_effects(mod, variables = "env"))


# The residual correlations among species the factors imply
plot(residual_cor(mod))


# Site scores and species loadings on the two factors
ordinate(mod, rotation = "varimax")


# Expected richness along `env`. The second plot marks the pairs of
# species found together more often than `env` predicts.
gradient <- datagrid(model = mod, env = seq(-2, 2, length.out = 20))
cm <- community(mod, newdata = gradient)
plot(cm)

plot(cm, type = "cooccurrence")


# The draws, for any contrast: richness at the end of the gradient
# less that at its start
rv <- as_draws_rvars(cm)
rv$richness[20] - rv$richness[1]
#> rvar<2000>[1] mean ± sd:
#> [1] -3.3 ± 0.44 

# Next, compare n_lv = 1 with update(mod, n_lv = 1L) and loo()
# }