| make_zero_one_map | R Documentation |
A grouping factor level (e.g. factor(year), but also possibly a region,
stratum, region-by-year interaction, or any other factor level) with all-zero
observations can drive the corresponding factor-coded coefficient to -Inf,
causing convergence problems. In delta/hurdle and binomial or beta-binomial
models, an all-positive or all-success level can similarly drive the
encounter coefficient to +Inf. Tweedie will have the same issue with all zeros.This helper returns map and start lists
suitable for passing through sdmTMBcontrol() to fix those problem
coefficients at large positive/negative values. This mirrors an approach
taken in VAST.
make_zero_one_map(formula, data, group, family, weights = NULL, value = 20)
formula |
The fixed-effect formula to be used in |
data |
The data frame passed to |
group |
Character. Name of the grouping column in |
family |
The |
weights |
Optional numeric vector of trial sizes for non-delta binomial
or beta-binomial models fit with proportion data. This should match the
|
value |
Positive numeric. Magnitude at which to fix the intercept;
|
Inspect the returned all_zero_levels and all_one_levels elements before
fitting to make sure the detected problem levels match your expectation.
Although the motivating use case is years in fisheries index standardization
(e.g., density ~ 0 + factor(year)), the same mechanism applies to any
factor whose levels each get a column in the design matrix. For an
interaction such as region by year, create a combined column (e.g.,
data$region_year <- interaction(data$region, data$year, drop = TRUE)) and
use ~ 0 + region_year.
For Poisson-link delta families (e.g., delta_gamma(type = "poisson-link")),
the first linear predictor controls both the encounter probability and the
positive rate, so fixing it shifts the meaning of the second-linear-predictor
coefficients in those levels. The helper still works for all-zero levels (the
large negative value makes encounter probability ~0, and the level
contributes ~0 to the index), but emits a message for all-positive levels.
Although this affects interpretation of the coefficients, the overall
combined prediction remains OK.
A named list with elements:
mapA list with b_j (and b_j2 for delta families) factors
suitable for sdmTMBcontrol(map = ...).
startA list with b_j (and b_j2) starting-value vectors
suitable for sdmTMBcontrol(start = ...).
all_zero_levelsGroup levels with all-zero observations.
all_one_levelsGroup levels with all-positive observations for delta families, or all-success observations for non-delta binomial/beta-binomial families. Empty for other families.
sdmTMBcontrol(), sdmTMB()
d <- pcod
d$density[d$year == 2003] <- 0 # force an all-zero year
m <- make_zero_one_map(
formula = density ~ 0 + factor(year),
data = d,
group = "year",
family = tweedie()
)
m$all_zero_levels
m$all_one_levels
m$start
m$map
mesh <- make_mesh(d, c("X", "Y"), cutoff = 15)
fit <- sdmTMB(
density ~ 0 + factor(year),
data = d,
mesh = mesh,
time = "year",
family = tweedie(),
spatial = "on",
spatiotemporal = "off", # off for example speed
control = sdmTMBcontrol(map = m$map, start = m$start) #<
)
fit
coef(fit)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.