R/marginal-effects.R
fastglm_marginal_effects.RdEstimates the average rate of change in each category probability as a continuous predictor changes. Uncertainty uses the delta method, conditional on the supplied predictor values and averaging weights.
fastglm_marginal_effects(
object,
variable,
newdata = NULL,
weights = NULL,
vcov. = NULL,
level = 0.95,
type = "response",
eps = NULL,
offset = NULL
)a converged fastglm_ordinal fit, with common or
threshold-specific slopes.
name of one continuous numeric variable. For a formula fit, this names the original variable in the data; interactions and smooth transformations are differentiated together. For a matrix fit, this names one location-design column, with the other columns held fixed.
data frame for a formula fit, or location-design matrix for a matrix fit. Defaults to retained fitting data. Supply the original variables explicitly when they cannot be recovered from the retained model frame.
nonnegative averaging weights, defaulting to equal weights. Fitting frequency weights are not reused automatically.
optional full coefficient covariance matrix, as in
fastglm_average().
confidence level for separate normal intervals.
category probabilities ("response" or
"probability") or "cumulative" probabilities.
positive absolute step for numerical differentiation of formula
terms. The default is .Machine$double.eps^(1/3) * pmax(1, abs(x))
for the selected variable. Matrix-column derivatives are exact and do not
use this argument.
optional replacement prediction offset. All offsets are held
fixed when differentiating, including formula offsets involving
variable.
A data frame with variable, category, estimate,
std.error, conf.low, and conf.high. Category effects
sum to zero. Removed categories have zero effects and unavailable standard
errors; inference otherwise refers to the reduced category model.
A marginal effect is a derivative, measured in probability units
per unit of the continuous predictor. It describes a local rate of change;
a finite change can give a different probability difference. Use
fastglm_average(contrast = ...) for discrete comparisons, including
factors. A numeric binary indicator is treated as continuous if requested.
Formula design derivatives use centered differences, with the fitted
prediction basis and factor coding retained. Terms must be smooth functions
of each row's predictors, or use a prediction basis frozen during fitting
(for example, poly() or splines::ns()). Expressions that
recompute sample summaries such as I(x - mean(x)) are unsupported.
Rows and perturbed values with positive averaging weight must be finite
and within the domain of the formula transformations. A smaller eps
may be needed near a transformation's domain boundary.
Derivatives of probabilities and their parameter gradients are analytic
conditional on the computed design derivative. The gradient includes
uncertainty in both thresholds and slopes. Computation uses blocks of rows
and retains the base prediction design, without storing a full array of
observation-by-parameter-by-category derivatives. Normal intervals are
first-order approximations; the covariance and reduced-category conventions
match fastglm_average().
For nonparallel models, each cumulative probability uses its own slope.
Evaluation rows must lie within the fitted model's valid predictor domain.
Supplied covariance, including cluster covariance, retains covariance among
thresholds, common slopes, and threshold-specific slopes.
set.seed(91)
d <- data.frame(x=rnorm(150))
d$y <- ordered(cut(.5*d$x + rlogis(150), c(-Inf,-.5,.7,Inf)))
fit <- fastglm_ordinal(y ~ x, data=d, firth=TRUE)
fastglm_marginal_effects(fit, "x")
#> variable category estimate std.error conf.low conf.high
#> 1 x (-Inf,-0.5] -0.074409937 0.032765561 -0.138629256 -0.01019062
#> 2 x (-0.5,0.7] 0.006715711 0.006508746 -0.006041196 0.01947262
#> 3 x (0.7, Inf] 0.067694226 0.030100717 0.008697905 0.12669055