Patient-level survival outcomes for a whole clinical trial are frequently available alongside published subgroup hazard ratios, while the subgroup membership of each patient is not. retrogroup reconstructs subgroup membership from these two sources of information. A Bayesian survival model estimates the posterior probability that each patient belongs to each subgroup level, and a sampling method uses these probabilities to impute a subgroup level for each patient.
Installation
To install the latest release version of retrogroup, open R and run:
install.packages("pak")
pak::pak("wlandau/retrogroup@*release")To install the development version of retrogroup:
pak::pak("wlandau/retrogroup")Documentation
The documentation website provides a function reference, usage tutorial, and vignettes. The vignettes explain the method and check its performance:
Usage
A typical workflow has four steps: gather the patient-level data and the published subgroup statistics, convert them to the standard format of retrogroup, sample subgroup membership for each patient, and optionally optimize the sampled subgroups to match the assumed statistics.
Example data
The example below uses the colon dataset from the survival package, which comes from the adjuvant colon cancer trial of levamisole plus fluorouracil (Moertel et al., 1990). group_example_patient() returns the patient-level survival data, with no subgroup information:
library(retrogroup)
group_example_patient()
#> # A tibble: 619 × 3
#> arm time event
#> <chr> <dbl> <int>
#> 1 Lev+5FU 1521 1
#> 2 Lev+5FU 3087 0
#> 3 Obs 963 1
#> 4 Lev+5FU 293 1
#> 5 Obs 659 1
#> 6 Lev+5FU 1767 1
#> 7 Obs 3192 0
#> 8 Lev+5FU 3308 0
#> 9 Lev+5FU 3309 0
#> 10 Obs 2085 1
#> # ℹ 609 more rowsgroup_example_summary() returns subgroup statistics like the ones a publication would report. For each level of age, nodal status, and sex, it gives the number of patients, the number of events, the hazard ratio of treatment versus control, and the 95% confidence interval of the hazard ratio.
group_example_summary()
#> # A tibble: 6 × 7
#> variable level patients events hazard_ratio lower upper
#> <chr> <chr> <int> <int> <dbl> <dbl> <dbl>
#> 1 age under 60 280 129 0.844 0.596 1.20
#> 2 age 60 or over 339 162 0.579 0.423 0.792
#> 3 node4 no 453 177 0.659 0.489 0.889
#> 4 node4 yes 166 114 0.732 0.505 1.06
#> 5 sex female 312 152 0.863 0.628 1.19
#> 6 sex male 307 139 0.519 0.365 0.737Pre-processing
group_data_patient() and group_data_summary() check the input data for consistency and convert it to the standard format the rest of the package expects. You supply the column names of your own data and state which study arm is the treatment arm. In the summary, the patient counts must add up to the number of patients in the patient-level data for every subgroup variable.
patient <- group_data_patient(
group_example_patient(),
arm = "arm",
treatment = "Lev+5FU",
time = "time",
event = "event"
)
summary <- group_data_summary(
group_example_summary(),
variable = "variable",
level = "level",
patients = "patients",
events = "events",
hazard_ratio = "hazard_ratio"
)Event counts (optional)
Some publications report confidence intervals of the subgroup hazard ratios, but not the subgroup event counts. In that case, group_data_events() can approximate the event counts from the confidence bounds. First, call group_data_summary() with events = NULL to skip the event counts.
summary_no_events <- group_data_summary(
group_example_summary(),
variable = "variable",
level = "level",
patients = "patients",
events = NULL,
hazard_ratio = "hazard_ratio"
)Then pass the lower and upper confidence bounds of each hazard ratio to group_data_events(). It returns a new summary object with approximate event counts.
summary_approximate <- group_data_events(
patient,
summary_no_events,
lower = group_example_summary()$lower,
upper = group_example_summary()$upper
)
summary_approximate
#> # A tibble: 6 × 5
#> variable level patients events hazard_ratio
#> <chr> <chr> <int> <int> <dbl>
#> 1 age under 60 280 131 0.844
#> 2 age 60 or over 339 160 0.579
#> 3 node4 no 453 176 0.659
#> 4 node4 yes 166 115 0.732
#> 5 sex female 312 159 0.863
#> 6 sex male 307 132 0.519The approximate event counts are close to the true counts in summary, but they are not exact. Choosing event counts is a decision about the reconstruction, so please disclose the counts used and how they were chosen. See Approximating event counts for the method. The rest of this example uses summary, which has the true event counts.
Sampling
group_sample_unconstrained() estimates1 the posterior probability that each patient belongs to each subgroup level, then samples a subgroup level independently for each patient. group_sample_constrained() is similar, but it ensures that the number of patients in each subgroup level exactly matches summary$patients.
set.seed(0)
sample <- group_sample_constrained(patient, summary)
sample
#> # A tibble: 619 × 6
#> treatment time event age node4 sex
#> <int> <dbl> <int> <fct> <fct> <fct>
#> 1 1 1521 1 60 or over no male
#> 2 1 3087 0 under 60 yes female
#> 3 0 963 1 under 60 no male
#> 4 1 293 1 60 or over no male
#> 5 0 659 1 60 or over no male
#> 6 1 1767 1 under 60 no male
#> 7 0 3192 0 60 or over no female
#> 8 1 3308 0 60 or over no female
#> 9 1 3309 0 60 or over no male
#> 10 0 2085 1 60 or over no female
#> # ℹ 609 more rowsEach sampled dataset is one random draw, so its subgroup hazard ratios differ somewhat from the assumed ones. Across many draws, however, they match the assumed hazard ratios on average. The sampling benchmarks show this for both the colon cancer example and simulated data.
The model includes a prognostic effect for each subgroup level, estimated from the assumed event counts, but the sampled event counts are not guaranteed to match summary$events.
Optimization
Optimization refines a single sampled dataset so that its subgroup hazard ratios match the assumed ones. Optimization proposes two kinds of moves: a flip moves one patient to the subgroup level that best reduces the discrepancy, and a swap exchanges the subgroup levels of two patients. group_optimize_unconstrained() starts from the output of group_sample_unconstrained() and mixes flips and swaps. group_optimize_constrained() starts from the output of group_sample_constrained() and uses only swaps, so the number of patients in each subgroup level never changes. Both optimizers target hazard ratios only, so the event counts are not guaranteed to match summary$events.
set.seed(0)
optimized <- group_optimize_constrained(sample, summary)group_analysis_summary() computes the subgroup statistics of a patient-level dataset.
group_analysis_summary(optimized)
#> # A tibble: 6 × 5
#> variable level patients events hazard_ratio
#> <chr> <chr> <int> <int> <dbl>
#> 1 age under 60 280 125 0.845
#> 2 age 60 or over 339 166 0.579
#> 3 node4 no 453 174 0.660
#> 4 node4 yes 166 117 0.732
#> 5 sex female 312 157 0.863
#> 6 sex male 307 134 0.520The optimized dataset matches the assumed patient counts and hazard ratios in summary.
summary
#> # A tibble: 6 × 5
#> variable level patients events hazard_ratio
#> <chr> <chr> <int> <int> <dbl>
#> 1 age under 60 280 129 0.844
#> 2 age 60 or over 339 162 0.579
#> 3 node4 no 453 177 0.659
#> 4 node4 yes 166 114 0.732
#> 5 sex female 312 152 0.863
#> 6 sex male 307 139 0.519Optimization targets the hazard ratios of treatment versus control only. It does not reproduce the timing of events within the subgroup levels of the original data. We make no claims about the prognostic information of optimized subgroups, but the prognostic benchmarks examine it empirically. A reconstructed dataset is not the original data, and the statistician who uses it in an analysis, for example for covariate adjustment, is responsible for the consequences.
See Optimizing subgroup levels for the method and the optimization benchmarks for its performance.
Code of conduct
Please note that this package is released with a Contributor Code of Conduct.
AI usage
This package was designed and implemented with the help of generative AI. The human author used Claude Fable 5.2 to search for the statistical resources that serve as the foundation of the methodology (including but not limited to Rodríguez (2010), Lin (2007), and Klein and Moeschberger (2003)) and discuss their contents. Then, the human author wrote almost all the primary methodology vignettes from scratch, with only a light touch of GitHub Copilot (powered by Claude Sonnet 5) just to catch spelling and notation errors:
- The motivating problem
- The model and posterior
- Sampling subgroups
- Approximating event counts
- Estimates of unknown model parameters
The remaining vignettes were drafted by Claude Fable 5.2 and Claude Opus 5.5, then carefully reviewed by the human author for correctness and consistency:
- The usage guide
- Optimizing subgroup levels
- Sampling benchmarks
- Optimization benchmarks
- Prognostic benchmarks
The vignettes dictate every part of the method from a combination of first principles and explicitly cited sources. Together, they form a complete specification of all the methods and algorithms implemented in the package, with the exception of retrogroup:::analysis_fit(), which is already specified by Efron (1977) and tested extensively in tests/testthat/test-analysis_fit.R against survival::coxph(ties = "efron"), tests thoroughly reviewed by the human author. Using the vignettes and Efron (1977) as guides, Claude Opus 5.5 with Claude Code version 2.1.326 implemented the remaining functions, function documentation pages, and tests of the package, which were then carefully reviewed by the human author for correctness and consistency. The human author takes full responsibility for the correctness and consistency of the statistical methodology and its implementation.
Citation
citation("targets")