Fits a weighted marginal Cox proportional hazards model to estimate marginal hazard ratios between treatment groups using propensity score weights. Fit Weighted Marginal Cox Model
Fits a marginal Cox model `Surv(time, event) ~ treatment` with propensity score weights to estimate marginal hazard ratios. Automatically handles zero weights (from trimming) by subsetting data before fitting.
fit_marginal_cox(
data,
treatment_var,
time_var,
event_var,
weights,
treatment_levels,
reference_level,
robust = TRUE,
functionality = "main"
)A list containing:
The fitted coxph model object. NULL if fitting failed in bootstrap mode.
Named numeric vector of log hazard ratios (coefficients). Length = n_levels - 1. Names indicate which group is compared to reference (e.g., "trtB" means group B vs reference). Contains NA for groups that are missing in data or have no events.
Named numeric vector of robust standard errors for log HRs. Same length and names as hr_estimates. NA for failed groups.
The treatment level used as reference.
Vector of all treatment levels.
Number of treatment levels.
Named numeric vector of sample sizes per group before trimming (from original data).
Named numeric vector of sample sizes per group actually used in Cox model (after excluding zero weights).
Named numeric vector of event counts per group before trimming.
Named numeric vector of event counts per group in fitted Cox model.
A data.frame containing the complete-case analysis data.
A character string specifying the name of the treatment
variable in data.
A character string specifying the name of the time variable
in data.
A character string specifying the name of the event variable
in data. Should be coded as 1 = event, 0 = censored.
A numeric vector of propensity score weights with length equal
to nrow(data). Returned from estimate_weights(). May contain zeros
for trimmed observations (these will be excluded before fitting).
A vector of unique treatment values (sorted). Should
match the levels from estimate_ps().
Which treatment level to use as reference in the Cox model. MANDATORY parameter.
Logical. Use robust (sandwich) variance estimator? Default TRUE.
When TRUE, uses coxph(..., robust = TRUE).
Character string indicating purpose: "main" for main point estimation or "boot" for bootstrap. Default "main". Controls error handling behavior when groups are missing or have no events.
Functionality Modes:
**"main" mode (point estimation):** - If reference group is missing or has no events after trimming: throws error - If non-reference group is missing: sets its HR and SE to NA, continues - If non-reference group has no events: sets its HR and SE to NA if coxph fails - Does NOT suppress warnings/messages from coxph
**"boot" mode (bootstrap):** - If reference group is missing or has no events: returns hr_estimates with all NA, no error - If non-reference group is missing: sets its HR and SE to NA - Does NOT suppress warnings (this is handled in bootstrap wrapper function)
Model Formula:
Fits Surv(time, event) ~ treatment where treatment is a factor with
k levels. Cox regression automatically creates k-1 dummy variables relative
to the reference level.
Zero Weights: coxph does not accept zero or negative weights. Observations with weight <= 0 are excluded before model fitting. This handles trimming automatically.
Coefficients:
Coefficients represent log hazard ratios. To get hazard ratios, use
exp(hr_estimates). A positive coefficient means higher hazard
(worse survival) compared to reference group.
Robust Variance:
When robust = TRUE, the sandwich variance estimator accounts for
weighting and provides more conservative standard errors. This is recommended
for propensity score weighted analyses.