margins: Predictive margins after an R4VN model

View source: R/postestimation.R

marginsR Documentation

Predictive margins after an R4VN model

Description

Calculates average adjusted predictions and confidence intervals from the active or supplied model.

Usage

margins(model = NULL,
  at = NULL,
  over = NULL,
  type = c("response",
    "link"),
  atmeans = FALSE,
  level = 0.95,
  digits = 3,
  p_digits = 3,
  show = TRUE,
  console = FALSE)

Arguments

model

Optional fitted model or R4VN model result. The most recently fitted R4VN model is used when omitted.

at

Predictor settings created by at(); vectors are crossed into scenarios.

over

Optional grouping variable(s), including vars(a, b), for margins within observed groups.

type

Response-scale or link-scale predictions.

atmeans

If TRUE, unspecified covariates are fixed at representative means/modes instead of averaging individual predictions.

level

Confidence level.

digits, p_digits

Formatting digits.

show, console

R4VN display controls.

Details

Unless atmeans = TRUE, variables not specified in at() retain their observed values and predictions are averaged over the estimation sample. Standard errors use the fitted covariance matrix and a delta-method gradient when the model provides the information required.

Value

An R4VN result object, invisibly.

See Also

at, marginsplot, predict, lincom

Examples

d <- data.frame(
  outcome = factor(c(0, 0, 0, 1, 0, 1, 1, 1), levels = 0:1,
                   labels = c("No", "Yes")),
  age = c(20, 25, 30, 35, 40, 45, 50, 55),
  sex = factor(rep(c("Female", "Male"), 4))
)
m <- logistic(outcome, c.age, i.sex, data = d, event = "Yes", show = FALSE)
margins(m, at = at(age = 40), show = FALSE)
margins(m, over = sex, show = FALSE)

R4VN documentation built on Sept. 30, 2026, 5:13 p.m.