Conditional fixed effects of sdmTMB models on the response scale
Source:R/conditional-effects.R
conditional_effects.RdPredicts the population-level response of one or more fitted sdmTMB models
along each fixed-effect term in turn, holding the other terms at reference
values, with confidence intervals. The terms of an sdmTMB formula are
additive on the link scale, so each term gets its own panel and the shape of
an effect does not depend on the references of the others, only its level
does. Use plot() to draw the figure and
effects_reference_text() to write the references into a caption.
Unlike plot_fixed_effects(), which plots the coefficients on the link
scale, this function shows the predicted response, so smooths, factors and
linear terms are on one interpretable scale.
Usage
conditional_effects(
fit,
ref = list(),
labels = NULL,
peak = "depth",
n = 80,
level = 0.95,
se_fit = TRUE,
max_discrete = 12
)Arguments
- fit
A fitted
sdmTMB::sdmTMB()model, or a named list of fitted models (the names are used as model headings in the plot).- ref
Named list of reference values by variable name, e.g.
list(year = 2025, geartype = "Shrimp", depth = 400). Variables not given get the defaults described in Details. Applies to every model infit.- labels
Named character vector of term labels by variable name, e.g.
c(depth = "Depth (m)", geartype = "Gear"). Defaults to the variable name.- peak
Character vector of continuous variables whose reference is the peak of their own effect. Default
"depth"(ignored when the model has no such variable). Usecharacter(0)to use medians instead.- n
Integer. Number of values along each continuous variable. Default
80.- level
Numeric. Confidence level. Default
0.95.- se_fit
Logical. Compute confidence intervals? Default
TRUE;FALSEis much faster on big models and returnsNAlimits.- max_discrete
Integer. Numeric variables with at most this many distinct integer values (e.g. month) are treated as discrete for the default reference (most common value) and the prediction range (full range). Default
12.
Value
A data frame of class sdmTMB_conditional_effects with columns
model, term (variable name), label, type ("continuous" or
"factor"), x (value as character), xnum (numeric position: the value
for continuous terms, the level index for factors), est, lwr, upr
(response scale), and an attribute ref: a data frame with the reference
value of each shown term per model (model, term, label, type,
value, xnum, source, where source is "peak", "mode", "last",
"median" or "user").
Details
Terms. Variables are read from the right-hand side of the model formula (both formulas of a delta model) and classified as:
continuous: numeric variables used in smooths (
s(x)), linear or transformed terms (x,log(x),poly(x, 2)). The variable is varied overnvalues between its 0.5 % and 99.5 % quantiles (or its range when it takesmax_discreteor fewer integer values, e.g. month);factor: factor or character variables, and numeric variables wrapped in
factor()oras.factor()in the formula (e.g.as.factor(year)). Every level is predicted, in level order.
Random-intercept terms ((1 | g)), offset terms and variables of
spatial_varying or time_varying formulas are not shown; they are held at
their references.
References. Each term not being varied is held at its reference value:
terms in
peak(by defaultdepthwhen it is in the model): the value where the term's own predicted effect peaks, e.g. the modal depth. The peak is found first by a cheap prediction without standard errors over the term's range, with the other terms at their references; peak terms are therefore shown first, because their references are used in the other panels;factors: the most common level in the data, except for a factor of the model's
timevariable, whose reference is the last level (the last year);numeric variables with
max_discreteor fewer integer values (e.g. month): the most common value (the most sampled month);other numeric variables: the median.
Any reference can be set through ref, a named list of values by variable
name, which also overrides the peak search.
Prediction. One call to
predict(fit, newdata, re_form = NA, re_form_iid = NA, se_fit = TRUE, model = NA, offset = 0) per model predicts every term without the spatial,
spatiotemporal and IID random effects and with a zero offset (i.e. per unit
of the offset, such as per unit swept area). For delta models, model = NA
combines the encounter probability and the positive-catch density on the
log scale, so the estimate and its confidence interval are exp() of the
log-scale prediction and its standard error. For other families the inverse
link is used. The confidence intervals are Wald intervals on the link scale
transformed to the response scale.
Speed. se_fit = TRUE runs TMB::sdreport() on the prediction, which
takes about as long as the model's own sdreport: seconds for small models,
but several minutes (around 8 min for a delta model with spatiotemporal
fields and ~ 70 000 hauls) for large ones. The function therefore makes a
single se_fit call per model; save the result (e.g. with saveRDS()) in
reports that are re-rendered. se_fit = FALSE gives the estimates only, in
seconds.
The time variable of a model with spatiotemporal fields is set to the
reference year; with re_form = NA it does not change the prediction
unless the model has time_varying terms. Effects of terms inside
interactions are shown with the interacting variables at their references.
AI assistance
Written with Claude Opus 5.5 (Anthropic); reviewed in full by Claude Opus 5.5 (Anthropic), 2026-11-17.
Examples
# \donttest{
d <- subset(sebastes, year >= 2021 & cruiseseries %in% c("EcoS", "EggaN"))
d$geartype <- factor(d$geartype)
mesh <- sdmTMB::make_mesh(d, c("X", "Y"), cutoff = 80)
m <- sdmTMB::sdmTMB(
density ~ s(depth, k = 5) + geartype + as.factor(year),
data = d, mesh = mesh, time = "year", spatiotemporal = "off",
family = sdmTMB::delta_gamma()
)
eff <- conditional_effects(m, labels = c(depth = "Depth (m)", geartype = "Gear"))
eff
#> Conditional effects of 1 model(s): Model
#> Terms: Depth (m), Gear, year
#> References:
#> model label value source
#> Model Depth (m) 371.4 peak
#> Model Gear Shrimp mode
#> Model year 2025 last
plot(eff, y_lab = "Density (kg/nmi^2)")
effects_reference_text(eff)
#> [1] "Depth (m) 371 (at the peak of its effect); Gear Shrimp (the most common level); year 2025 (the last level)"
# }