Skip to content

Fine-Gray rows in cox and cox_counting models, without expanding rows - #125

Merged
msuchard merged 2 commits into
developfrom
cox-counting-fine-gray
Nov 20, 2026
Merged

msuchard merged 2 commits into
developfrom
cox-counting-fine-gray

Conversation

@msuchard

Copy link
Copy Markdown
Member

What

Fine-Gray (subdistribution hazard) models in cox and cox_counting, with counting-process data, case weights and one censoring distribution per group, without expanding rows.

outcomes$fineGrayGroup <- ifelse(competingEvent, group, 0)   # y must be 0 on these rows
data <- convertToCyclopsData(outcomes, covariates, modelType = "cox_counting",
                             fineGrayCensoring = data.frame(fineGrayGroup, time, survival))

A row with fineGrayGroup g > 0 ended with a competing event at its stop time T. It stays in the risk set of every later event time t of its stratum, with its weight multiplied by G_g(t−) / G_g(T−). G_g is the right-continuous step function of group g in fineGrayCensoring, and 1 for a group with no rows there.

Why

CloneCensorWeight's Fine-Gray MSM needs time-varying clone weights, which means (start, stop] segments, and a censoring distribution per strategy. fgr takes one right-censored row per subject, a fixed weight and a single G, so CCW has been expanding the rows itself: one extra row per competing death and later jump of G. That is about 4.3× the segments on simulated data, and the count grows as deaths × follow-up days.

How

CountingProcessModelSpecifics treats each competing-event row as having a "tail": the groups of its stratum after its stop time. Each risk-set sum becomes

S(i) = (ordinary rows) + Σ_g m_g(i) · (running sum of group g's tails),

with m_g(i) = G_g(t_i−) and a per-row factor 1 / G_g(T−). A fit is then O(rows + event times × groups).

The tails are included in:

  • the denominators and log-likelihood (so the cross-validation predictive likelihood too);
  • the coordinate-descent gradient and Hessian (a per-column scan over all groups, used in place of the change-point hazard path when tails are present);
  • the Fisher information (vcov()), the robust score contributions (vcov(type = "robust")) and the Schoenfeld residuals;
  • the separation check;
  • the second-order API: fullGradient, hessianBlock, hessianVector, trialLogLikelihood / commitStep, and woodburyParts. So dense and conjugate-gradient Newton steps, with or without the Woodbury preconditioner, work.

Tails that run to the end of their stratum exit at the unused group N, so running sums never leak into the next stratum.

Not implemented, and refused rather than wrong:

  • predict(times = ) and survfit() stop with an error;
  • third derivatives throw;
  • double precision only.

Rows without fineGrayGroup take the existing code paths unchanged.

Tests

tests/testthat/test-fineGrayCounting.R: 10 tests, 58 expectations, 2.2 s. Every comparison is against the cox_counting fit on the explicitly expanded rows:

  • coefficients (1e-8), log-likelihood (1e-10), and model-based and robust vcov (1e-7), with and without strata, with case weights and (start, stop] rows;
  • ccd, newton and auto, at 1 and 4 threads; a Laplace prior;
  • the second-order kernels at fixed coefficients, through .cyclopsSecondOrder(): gradient, Hessian, Hv, trial and committed log-likelihood (1e-9), at 1 and 4 threads;
  • Newton-CG with and without Woodbury;
  • one covariate (the CCW case), including Schoenfeld residuals;
  • a covariate seen only on competing-event rows: the same separation, limit and coefficients;
  • cross-validation with the same folds per subject: the same interior variance on a 25-step grid, and the same coefficients;
  • the input checks, and survfit() refused.

Full suite (NOT_CRAN, installed package, on the merge with develop 4130cc1): 381 tests, 2,043 expectations, 0 failed; the 8 file-level errors (test-bootstrap, conditionalPoisson, dataConversionStratified/Unstratified, fisherEvents, smallCLR, smallFineGray, weighting) are the same as on develop.

In CloneCensorWeight (branch ccw-v2-finegray, not pushed), the fineGray MSM uses these rows when the installed Cyclops has them. At 10^5 simulated persons it takes 2.1 s instead of 11.3 s, fitting 1.0M segments instead of 4.3M expanded rows, with the same SHR and SE. The CCW default tier passes (5,819 expectations).

NEWS item 52. develop's #124 took 49–51, merged in here.

🤖 Generated with Claude Code

msuchard and others added 2 commits November 20, 2026 14:31
convertToCyclopsData(..., fineGrayCensoring = data.frame(fineGrayGroup, time,
survival)) with an outcome column fineGrayGroup: a row of group g > 0 ended
with a competing event at its stop time T and stays in the risk set of every
later event time t of its stratum, its weight multiplied by G_g(t-) / G_g(T-).
CountingProcessModelSpecifics keeps one running sum per group in each risk
set, so a fit is O(rows + event times x groups).

Covered: denominators and log likelihood, CCD gradient and Hessian, Fisher
information (vcov), robust score contributions, Schoenfeld residuals,
separation checks, cross-validation, and the second-order API (gradient,
Hessian block, Hv, trial/commit, Woodbury) for dense and CG Newton steps.
predict(times = ) and survfit() stop with an error; third derivatives throw;
double precision only.

Tests (test-fineGrayCounting.R, 58 expectations) compare every fit with the
cox_counting fit on the expanded rows. NEWS 49.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
NEWS: develop's #124 took items 49-51; the Fine-Gray item is 52.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.54499% with 29 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.03%. Comparing base (4130cc1) to head (985c37a).

Files with missing lines Patch % Lines
src/cyclops/ModelData.cpp 63.26% 18 Missing ⚠️
...c/cyclops/engine/CountingProcessModelSpecifics.hpp 98.03% 6 Missing ⚠️
R/NewDataConversion.R 83.33% 4 Missing ⚠️
R/Predict.R 50.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #125      +/-   ##
===========================================
+ Coverage    85.87%   86.03%   +0.15%     
===========================================
  Files           73       73              
  Lines        16338    16690     +352     
===========================================
+ Hits         14031    14359     +328     
- Misses        2307     2331      +24     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@msuchard
msuchard merged commit ffe9c47 into develop Nov 20, 2026
26 checks passed
@msuchard
msuchard deleted the cox-counting-fine-gray branch November 20, 2026 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant