Weighted Cox Model Estimation Methodology
Source:vignettes/articles/weightedcox_methodology.Rmd
weightedcox_methodology.RmdOverview
This article describes the weighted Cox model estimation methodology
implemented in the weightedsurv package. Weighted Cox
models extend the standard Cox proportional hazards framework by
incorporating time-dependent weight functions into the partial
likelihood. These weights allow the analyst to emphasize treatment
effects at different points on the survival distribution — for example,
weighting later time-points more heavily in late-separation scenarios
common in immunotherapy trials.
The methodological foundations draw on the work of Lin (1991) and Sasieni (1993) who developed the theory for weighted partial-likelihood estimation, and León et al. (2020) who proposed companion weighted Cox estimators linked to weighted log-rank combination tests.
The weightedsurv package provides a unified interface
for computing weighted Cox model estimates across multiple weighting
schemes, with variance estimation based on both asymptotic and
synthetic-martingale resampling approaches.
Background: Non-Proportional Hazards and the Standard Cox Model
The Two-Sample Setting
Consider the standard two-sample random censorship model used in clinical trials. For let if observation is from group with . Let denote the survival time and the censoring time. We observe and . Survival times are assumed independent of censoring conditional on treatment.
The at-risk and counting processes are
The standard Cox model assumes , yielding a constant hazard ratio . The maximum partial-likelihood estimator solves the score equation
where for .
Why Weighted Cox Models?
Under non-proportional hazards the true model is with a time-varying coefficient . In this case the standard Cox estimator converges to a weighted average of that depends on the censoring distribution, risk-set composition, and the pattern of non-proportional hazards. The resulting “average hazard ratio” may not accurately reflect the treatment effect at clinically relevant time periods.
For example, in a late-separation scenario where for and for , the standard Cox estimator attenuates the true later-term effect toward the null because it gives substantial weight to the early period where there is no treatment difference.
Weighted Cox models address this by introducing a time-dependent weight function into the partial likelihood, allowing the analyst to emphasize contributions from specific time regions.
Weighted Partial Likelihood
Definition
The -weighted partial likelihood is
where is a predictable weight function. The weighted Cox estimator maximizes and solves
Note that for this reduces to the standard Cox score equation, and in the two-sample setting evaluated at is the weighted log-rank statistic.
Weighting Schemes and Their Rationale
The weightedsurv package implements several weighting
schemes through the wt.rg.S() function and the
cox_rhogamma() interface. Each scheme corresponds to a
different emphasis on the survival time scale:
Fleming-Harrington FH(, ): The weight function is where is the left-continuous pooled Kaplan-Meier estimator and , . The standard log-rank / Cox model corresponds to FH(0, 0). Increasing (with ) emphasizes late differences; increasing (with ) emphasizes early differences.
Magirr-Burman (MB): which provides modest downweighting of early time-points relative to the user-specified cutoff .
Schemper: where is the left-continuous pooled Kaplan-Meier censoring survivor function (the weight is 0 where ). This scheme produces an “average hazard ratio” estimator that is approximately free of the censoring distribution.
Xu-O’Quigley (XO): where is the pooled number at risk (with subject weights, the weighted number at risk; the weight is 0 where ), which also yields a censoring-independent average hazard ratio estimator.
Custom time-based: Step-function weights of the form , which allow the analyst to zero-out contributions before or after a pre-specified time-point.
Variance Estimation
The variance of is estimated through the weighted observed information and the variance of the weighted score. The weighted information is
and the standard error of is . The estimate of the score variance depends on whether there are subject-specific (case) weights.
No subject weights, or all subject weights equal. The score variance is estimated (evaluated at ) following the martingale-based approach of Fleming and Harrington (1991):
where . At the score statistic and its variance reduce to the weighted log-rank statistic and its standard variance estimator.
Subject weights that are not all equal (score-residual variance). Let subject have time , event indicator , arm and subject weight (for example a propensity-score weight), and write
The score is a sum of per-subject contributions, in the sign convention of the package, with
the sum over running over the distinct event times. The variance of the score is estimated by the sum of squared contributions,
The weighted log-rank z-score is
and the standard error of the log hazard ratio is
.
For FH(0, 0) weights these are the robust score test and the robust
standard error of
survival::coxph(weights = , robust = TRUE, ties = "breslow").
They do not depend on how the subject weights are scaled. The point
estimate, the score and the information are the same under both variance
estimates.
Estimated subject weights. The formulas above treat
the subject weights as fixed, known numbers
(weight_variance = "known", the default). Propensity-score
weights are estimated; the section “Estimated weights” below describes
what weight_variance = "estimated" adds.
Synthetic-Martingale Resampling
In addition to asymptotic standard errors, weightedsurv
implements synthetic-martingale resampling for variance estimation
(draws > 0 in cox_rhogamma()). Without
subject weights (or with all subject weights equal) the idea (Dobler et al. (2017)) is to replace the
unobservable martingale increments
with independent standard normal variates multiplied by the observable
counting-process increments:
where are independent of the data. With subject weights that are not all equal, each resampled score is built from the per-subject score contributions of the previous section,
so that its variance over draws is the score-residual variance and the resampling standard error estimates the same quantity as the asymptotic one.
In both cases the resampled distribution of
approximates the sampling distribution of
.
From the draws resampled values cox_rhogamma()
takes the resampling standard error (their standard deviation) and a
de-biased estimate
(
minus their mean); the resampling-based confidence interval
(hr_ci_star) is the de-biased estimate plus and minus the
normal quantile times the resampling standard error.
The same multipliers can be used for several weighting schemes (they are generated from the same seed in every call), which preserves the correlation between the schemes; this is what is needed for simultaneous confidence intervals across multiple weighting schemes when the weighting scheme is selected via a combination test (see León et al. (2020)).
Estimated weights
Propensity-score weights are estimated from the data.
ps_weights() fits a logistic regression for the treatment
(or takes a fitted one) and returns an object holding the weights
(inverse-probability weights, or stabilized weights with
type = "stabilized"), the fitted probabilities, the
influence
of each subject on the fitted coefficients, and the derivative
of each subject’s weight in those coefficients.
Passed as weight_model to df_counting()
with weight_variance = "estimated", the object lets the
score contributions carry the estimation of the weights:
The log-rank z-score, the standard error of the log hazard ratio and
the resampled scores are formed as above with
in place of
.
cox_rhogamma() takes the option from the
df_counting() result it is given. No estimate changes: only
standard errors, intervals and tests do. The derivation and the
statistical properties of this option are in the article
estimated_weights_methodology (“Estimated propensity-score
weights: statistical details and properties”).
Limits of this version: the model must be a logistic glm
fitted on exactly the rows analysed; the dependence of the time weights
on the estimated subject weights (through the weighted pooled
Kaplan-Meier curve) is ignored; and the resampling-based
pointwise variance of the Kaplan-Meier curves
(draws > 0 in df_counting(),
KM_diff() and plotKM.band_subgroups()) is not
available with "estimated", while the resampling of
cox_rhogamma() and the simultaneous bands
(draws.band) are. Neither option repairs concentrated
weights: when the effective sample size of an arm
(df_counting()$ess) is a small fraction of its size,
intervals and tests can be too optimistic under both. The evaluation is
in the section “Simulation results by time” of the article
estimated_weights_methodology.
Asymptotic Properties Under Misspecification
The Estimand: What Does Estimate?
Under regularity conditions (Lin (1991)), converges in probability to , the solution to where
with the probability limit of , and , .
This shows that is a weighted average (on the log-hazard scale) of the time-varying , with weights determined jointly by , the censoring distribution, and the risk-set dynamics.
Late-Separation Scenario
To build intuition, consider the change-point model with equal randomization () and censoring independent of treatment. Then satisfies
where .
The first integral covers the null period () where the contribution pushes toward zero; the second integral covers the treatment-effect period () and pushes toward . Weighting schemes that downweight the first integral (e.g., FH(0,1)) therefore produce estimands closer to the true treatment effect .
Using weightedsurv for Weighted Cox Estimation
Workflow
The typical workflow for weighted Cox estimation in
weightedsurv involves:
- Prepare a counting-process dataset via
df_counting()orget_dfcounting() - Fit weighted Cox models via
cox_rhogamma()with the desired weighting scheme - Optionally, use resampling (the
drawsargument) for improved inference
The df_counting() function computes risk sets, event
counts, Kaplan-Meier estimates, and the specified weighting scheme. The
cox_rhogamma() function then uses this counting-process
representation to solve the weighted score equation.
Example: GBSG Trial Data
We illustrate using the GBSG randomized trial data available in the
survival package.
library(survival)
library(weightedsurv)
if (has_tables) library(data.table)
# Prepare GBSG data
df_gbsg <- gbsg
df_gbsg$tte <- df_gbsg$rfstime / 30.4375 # Convert days to months
df_gbsg$event <- df_gbsg$status
df_gbsg$treat <- df_gbsg$hormon
tte.name <- "tte"
event.name <- "event"
treat.name <- "treat"
arms <- c("treat", "control")Step 1: Counting-Process Dataset
The df_counting() function computes the full
counting-process representation along with the specified weighting
scheme:
# Standard (unweighted) analysis with FH(0,0) = standard log-rank / Cox
dfcount_gbsg <- df_counting(
df = df_gbsg,
tte.name = tte.name,
event.name = event.name,
treat.name = treat.name,
arms = arms,
by.risk = 12,
scheme = "fh",
scheme_params = list(rho = 0, gamma = 0)
)Step 2: Standard Cox Model
With FH(0,0) weights the weighted Cox model reduces to the standard Cox model:
# Standard Cox via cox_rhogamma with FH(0,0)
fit_cox <- cox_rhogamma(
dfcount = dfcount_gbsg,
scheme = "fh",
scheme_params = list(rho = 0, gamma = 0),
draws = 1000,
verbose = FALSE
)
cat("Standard Cox model\n")## Standard Cox model
## HR: 0.695
cat(" 95% CI (asymptotic):",
round(fit_cox$hr_ci_asy$lower, 3), "-",
round(fit_cox$hr_ci_asy$upper, 3), "\n")## 95% CI (asymptotic): 0.544 - 0.888
cat(" 95% CI (resampling):",
round(fit_cox$hr_ci_star$lower, 3), "-",
round(fit_cox$hr_ci_star$upper, 3), "\n")## 95% CI (resampling): 0.54 - 0.889
## Z-score: 2.927
Step 3: Weighted Cox Models with Different Schemes
Now we fit weighted Cox models with several weighting schemes to examine how emphasis on different time regions affects the hazard ratio estimate:
# FH(0,1): Late-emphasis weighting
fit_fh01 <- cox_rhogamma(
dfcount = dfcount_gbsg,
scheme = "fh",
scheme_params = list(rho = 0, gamma = 1),
draws = 1000,
verbose = FALSE
)
# FH(1,0): Early-emphasis weighting
fit_fh10 <- cox_rhogamma(
dfcount = dfcount_gbsg,
scheme = "fh",
scheme_params = list(rho = 1, gamma = 0),
draws = 1000,
verbose = FALSE
)
# Magirr-Burman with 12-month cutoff
fit_mb <- cox_rhogamma(
dfcount = dfcount_gbsg,
scheme = "MB",
scheme_params = list(mb_tstar = 12),
draws = 1000,
verbose = FALSE
)
# Custom: Zero weight before 6 months, unit weight after
fit_custom <- cox_rhogamma(
dfcount = dfcount_gbsg,
scheme = "custom_time",
scheme_params = list(t.tau = 6, w0.tau = 0, w1.tau = 1),
draws = 1000,
verbose = FALSE
)Comparison of Results
The table needs the data.table and gt packages.
library(gt)
results <- data.table(
Scheme = c("FH(0,0) [Standard Cox]", "FH(0,1) [Late emphasis]",
"FH(1,0) [Early emphasis]", "MB(12)", "Custom 0/1 at 6 months"),
HR = c(fit_cox$hr_ci_asy$hr, fit_fh01$hr_ci_asy$hr,
fit_fh10$hr_ci_asy$hr, fit_mb$hr_ci_asy$hr, fit_custom$hr_ci_asy$hr),
Z = c(fit_cox$z.score, fit_fh01$z.score,
fit_fh10$z.score, fit_mb$z.score, fit_custom$z.score),
Lower = c(fit_cox$hr_ci_star$lower, fit_fh01$hr_ci_star$lower,
fit_fh10$hr_ci_star$lower, fit_mb$hr_ci_star$lower, fit_custom$hr_ci_star$lower),
Upper = c(fit_cox$hr_ci_star$upper, fit_fh01$hr_ci_star$upper,
fit_fh10$hr_ci_star$upper, fit_mb$hr_ci_star$upper, fit_custom$hr_ci_star$upper)
)
results %>%
gt() %>%
fmt_number(columns = c(HR, Z, Lower, Upper), decimals = 3) %>%
cols_label(
Scheme = "Weighting Scheme",
HR = "Hazard Ratio",
Z = "Z-score",
Lower = "95% CI Lower",
Upper = "95% CI Upper"
) %>%
tab_header(
title = "Weighted Cox Model Estimates: GBSG Trial",
subtitle = "Resampling-based 95% confidence intervals (1000 draws)"
) %>%
tab_footnote(
footnote = "FH(rho, gamma) = Fleming-Harrington weights; MB = Magirr-Burman.",
locations = cells_column_labels(columns = Scheme)
)| Weighted Cox Model Estimates: GBSG Trial | ||||
| Resampling-based 95% confidence intervals (1000 draws) | ||||
| Weighting Scheme1 | Hazard Ratio | Z-score | 95% CI Lower | 95% CI Upper |
|---|---|---|---|---|
| FH(0,0) [Standard Cox] | 0.695 | 2.927 | 0.540 | 0.889 |
| FH(0,1) [Late emphasis] | 0.721 | 2.261 | 0.540 | 0.952 |
| FH(1,0) [Early emphasis] | 0.687 | 2.952 | 0.530 | 0.885 |
| MB(12) | 0.696 | 2.918 | 0.541 | 0.890 |
| Custom 0/1 at 6 months | 0.691 | 2.906 | 0.532 | 0.890 |
| 1 FH(rho, gamma) = Fleming-Harrington weights; MB = Magirr-Burman. | ||||
Visualizing Weight Functions
The plot_weight_schemes() function displays the weight
profiles over the event time-scale, illustrating how different schemes
emphasize different regions:
g <- plot_weight_schemes(dfcount_gbsg)
print(g)
Weighted Cox with Propensity-Score Adjustment
The weightedsurv package supports subject-specific
(case) weights in combination with time-dependent weighting. This is
particularly useful for observational studies where propensity-score
weighting is used to adjust for confounding.
The example uses the node-positive women of the Rotterdam study. With all of them, the stabilized weights are concentrated on a few treated women; the effective sample size table shows it:
fmt <- function(x, digits = 2) formatC(x, format = "f", digits = digits)
# Prepare Rotterdam data (observational study)
gbsg_validate <- within(rotterdam, {
rfstime <- ifelse(recur == 1, rtime, dtime)
time_months <- rfstime / 30.4375
status2 <- ifelse(recur == 1 | (recur == 0 & death == 1 & rtime < dtime), recur, death)
grade3 <- ifelse(grade == "3", 1, 0)
treat <- hormon
event <- status2
tte <- time_months
})
# node-positive women, as in the GBSG trial
df_rotterdam_np <- subset(gbsg_validate, nodes > 0)
# Propensity score model and stabilized weights, all node-positive women
pw_np <- ps_weights(treat ~ age + meno + size + grade3 + nodes + pgr + chemo + er,
data = df_rotterdam_np, type = "stabilized")
df_rotterdam_np$sw.weights <- pw_np$weights
ess_np <- get_dfcounting(
df = df_rotterdam_np,
tte.name = "tte", event.name = "event", treat.name = "treat",
arms = arms, by.risk = 24, weight.name = "sw.weights"
)$ess
knitr::kable(ess_np, digits = c(0, 0, 1, 3))| group | n | ess | ratio |
|---|---|---|---|
| treat | 339 | 105.2 | 0.310 |
| control | 1207 | 1162.9 | 0.964 |
The effective sample size of the treated arm is 0.31 of its size and the largest weight is 12.9: hormonal therapy went mostly to postmenopausal women without chemotherapy (291 of the 339 treated women). The example is therefore restricted to postmenopausal women without chemotherapy, with the propensity model fitted on those rows. The standard Cox model, FH(0,0), is fitted with the weights treated as estimated; the FH(0,1) model with the weights treated as known.
df_rotterdam <- subset(df_rotterdam_np, meno == 1 & chemo == 0)
# Propensity score model and stabilized weights on the rows analysed
pw <- ps_weights(treat ~ age + size + grade3 + nodes + pgr + er,
data = df_rotterdam, type = "stabilized")
df_rotterdam$sw.weights <- pw$weights
# Weighted counting-process dataset (propensity-score weighted), weights treated as estimated
dfcount_wtd <- get_dfcounting(
df = df_rotterdam,
tte.name = "tte", event.name = "event", treat.name = "treat",
arms = arms, by.risk = 24, weight.name = "sw.weights",
weight_variance = "estimated", weight_model = pw
)
knitr::kable(dfcount_wtd$ess, digits = c(0, 0, 1, 3))| group | n | ess | ratio |
|---|---|---|---|
| treat | 291 | 270.7 | 0.930 |
| control | 538 | 529.3 | 0.984 |
In the restricted population (829 women; 538 control, 291 treated) both effective sample size ratios are close to 1 and the largest weight is 2.7.
# The same dataset with the weights treated as known (the default)
dfcount_known <- get_dfcounting(
df = df_rotterdam,
tte.name = "tte", event.name = "event", treat.name = "treat",
arms = arms, by.risk = 24, weight.name = "sw.weights"
)
# Weighted Cox models with propensity-score adjustment
# FH(0,0): weights treated as estimated, asymptotic interval
fit_ps_cox <- cox_rhogamma(dfcount = dfcount_wtd, scheme = "fh",
scheme_params = list(rho = 0, gamma = 0),
verbose = FALSE)
# FH(0,1): weights treated as known, resampling interval
fit_ps_fh01 <- cox_rhogamma(dfcount = dfcount_known, scheme = "fh",
scheme_params = list(rho = 0, gamma = 1),
draws = 1000, verbose = FALSE)
cat("Rotterdam propensity-score weighted analyses:\n")## Rotterdam propensity-score weighted analyses:
cat(" Standard Cox HR (weights", fit_ps_cox$weight_variance, "):", round(fit_ps_cox$hr_ci_asy$hr, 3),
" 95% CI (asymptotic):", round(fit_ps_cox$hr_ci_asy$lower, 3), "-",
round(fit_ps_cox$hr_ci_asy$upper, 3), "\n")## Standard Cox HR (weights estimated ): 0.665 95% CI (asymptotic): 0.564 - 0.784
cat(" FH(0,1) Cox HR (weights", fit_ps_fh01$weight_variance, "):", round(fit_ps_fh01$hr_ci_asy$hr, 3),
" 95% CI (resampling):", round(fit_ps_fh01$hr_ci_star$lower, 3), "-",
round(fit_ps_fh01$hr_ci_star$upper, 3), "\n")## FH(0,1) Cox HR (weights known ): 0.763 95% CI (resampling): 0.623 - 0.941
Connection to Weighted Log-Rank Tests
Score-Test Equivalence
The weighted Cox score statistic evaluated at is algebraically equivalent to the weighted log-rank statistic. That is, where
with
,
as score_calculation() computes it (without a factor
).
Such a constant factor would multiply the statistic and its standard
error alike, so the z-statistic is unchanged by it.
This duality connects hypothesis testing (via the weighted log-rank
test) and estimation (via the weighted Cox model) under the same
weighting scheme, and justifies referring to the weighted Cox estimator
as the “companion” to the weighted log-rank test. In
weightedsurv, the check_results() function
verifies this equivalence numerically.
Companion Estimators and Combination Tests
When multiple weighted log-rank tests are combined in a max-combo test (León et al. (2020)), the “companion” Cox estimator is the weighted Cox estimate corresponding to the weighting scheme that yielded the largest test statistic. Since is selected via a data-dependent process, valid inference requires simultaneous confidence intervals that hold across all weighting schemes.
The critical value is determined such that
which can be computed via the same synthetic-martingale resampling
using common
draws across all
weighting schemes, thereby preserving the correlation structure.
cox_rhogamma() does not return
;
it returns, for each scheme, the standardized resampled values
(fit_resamples$Z_bstar), which are based on the same
multipliers
in every call (same seed), and
is the
quantile over draws of the largest absolute value across the
schemes.
Interpretation and Estimands
What Does the Weighted Hazard Ratio Mean?
Under non-proportional hazards each weighted Cox estimator converges to a different estimand that reflects the weighting-specific average of the time-varying hazard ratio. The following interpretive principles apply:
- FH(0,0) (standard Cox): Estimates an overall average hazard ratio. Under proportional hazards it estimates the true constant HR.
- FH(0, ) with (late emphasis): Estimates a hazard ratio that more heavily reflects later-term treatment effects. Under late-separation, this is closer to the true treatment benefit after the change-point.
- FH(, 0) with (early emphasis): Estimates a hazard ratio reflecting early treatment effects. Under early-separation, this captures the initial treatment benefit.
- Schemper / XO: Estimate “average hazard ratios” that are approximately independent of the censoring distribution.
Censoring Dependence
In general, the estimand depends on the censoring distribution through its influence on the risk-set dynamics and the probability limit of the weights. The Schemper and XO weighting schemes were specifically designed to yield estimands that are (asymptotically) free of the censoring distribution . For FH weights, the degree of censoring dependence can be evaluated numerically using the limiting equation described above.
Cautions
As discussed by Hernán (2010), Aalen et al. (2015), and Stensrud et al. (2019), hazard ratios — even under randomization and ideal proportional hazards conditions — are subject to potential selection bias through temporal conditioning on risk sets. Weighted Cox estimators may further complicate these dynamics through the additional time-dependent weighting. The weighted hazard ratio should therefore be interpreted as a complementary summary of treatment effect with careful attention to what the specific weighting pattern emphasizes.
Summary
The weighted Cox model estimation framework in
weightedsurv provides:
-
Flexible weighting via multiple schemes (FH, MB,
Schemper, XO, custom) through a unified
cox_rhogamma()interface - Dual inference: Both asymptotic and resampling-based standard errors and confidence intervals
- Score-test duality: Direct connection between weighted Cox estimation and weighted log-rank testing
- Propensity-score compatibility: Subject-specific case weights can be combined with time-dependent weighting
-
Simultaneous inference: for each scheme,
cox_rhogamma()returns the standardized resampled values (fit_resamples$Z_bstar), based on the same multipliers in every call; the critical value of simultaneous confidence intervals across schemes (needed when the weighting scheme is selected via a combination test) is computed from them and is not returned
The key functions are:
| Function | Role |
|---|---|
df_counting() / get_dfcounting()
|
Prepare counting-process data with weights |
cox_rhogamma() |
Fit weighted Cox models with asymptotic and resampling inference |
wt.rg.S() |
Compute time-dependent weight functions |
plot_weight_schemes() |
Visualize weight profiles |
check_results() |
Verify score-test equivalence |
wlr_dhat_estimates() |
Weighted log-rank with survival-difference estimates |