Skip to contents

Refine sampled subgroup levels so that the subgroup statistics observed in the data match the assumed subgroup statistics in summary, while preserving the assumed number of patients in each subgroup level.

Usage

group_optimize_constrained(
  patient,
  summary,
  iterations = 10000L,
  tolerance = 0.001
)

Arguments

patient

Patient-level survival data, the output of group_data_patient().

summary

Summary subgroup statistics, the output of group_data_summary().

iterations

Non-negative integer, the maximum number of iterations for each subgroup variable. Each iteration draws one pair of patients and fits two Cox models.

tolerance

Non-negative number on the linear (natural) scale of the hazard ratio, not the log scale. The search for each subgroup variable stops when the loss \(\Delta\) falls below tolerance. Because \(\Delta\) is the Euclidean distance between the observed and assumed hazard ratios themselves, a loss below tolerance guarantees that the observed hazard ratio of every subgroup level is within tolerance of its assumed value. The default of 1e-3 is much finer than the two decimal places hazard ratios are usually published with. However, a small tolerance may be unreachable, in which case the search runs for all iterations. Larger values such as 1e-2 give faster but coarser fits.

Value

A tibble with the same rows and columns as patient, in the same order. Each subgroup column is a factor of the optimized levels whose levels are those of the subgroup variable in summary$level, in the same order. The number of patients in each level equals summary$patients. All other columns are unchanged.

Details

group_optimize_constrained() implements the "Optimization for constrained sampling" section of vignette("optimization", package = "retrogroup"), separately for each subgroup variable. The loss \(\Delta\) is the same as in group_optimize_unconstrained(): the Euclidean distance between the observed and assumed hazard ratios. The observed hazard ratios come from a Cox proportional hazards model of each subgroup level with treatment as the only covariate, as in survival::coxph(). If the model cannot be fit for some subgroup level, for example because one of its arms has no events, the loss is infinite.

Each iteration takes a swap step, as defined in the "Moves" section of the vignette. A swap step draws a pair of patients \((i, j)\) uniformly at random from the pairs with \(x_i \ne x_j\) and exchanges their subgroup levels if the exchange reduces the loss. Otherwise, the subgroup levels stay the same, so the loss never increases. If no such pair exists, no swap is possible and the subgroup levels are returned unchanged. The search for each subgroup variable stops when the loss falls below tolerance or after iterations iterations, whichever comes first. If the loss of any subgroup variable is still at least tolerance after iterations iterations, group_optimize_constrained() throws a warning of class "retrogroup_warning" that lists those subgroup variables and their final losses.

The search is greedy, so it may settle in a local minimum. Swaps preserve the number of patients in each subgroup level, so the optimized counts equal summary$patients. Use group_optimize_unconstrained() on the output of group_sample_unconstrained() to optimize without the count constraint.

The loss targets hazard ratios only, so the number of events in each subgroup level is not guaranteed to equal summary$events. vignette("prognostic", package = "retrogroup") examines the prognostic information of reconstructed subgroups. The statistician who uses the optimized data in an analysis, for example for covariate adjustment, is responsible for the consequences.

In addition to the requirements of group_analysis_probability(), patient must have one column per subgroup variable in summary, named after the subgroup variable, with no missing values and only the levels of that subgroup variable in summary. The number of patients in each subgroup level of these columns must equal summary$patients. The output of group_sample_constrained() satisfies these requirements. These columns are the starting subgroup levels, and all other columns of patient are returned unchanged.

Optimization uses R's random number generator. Call set.seed() beforehand for reproducible results.

See also

Other optimization: group_optimize_unconstrained()

Examples

  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"
  )
  summary <- summary[summary$variable == "age", ]
  set.seed(0)
  sample <- group_sample_constrained(patient, summary)
  optimized <- group_optimize_constrained(
    sample,
    summary,
    tolerance = 1e-2,
    iterations = 500L
  )
  summary
#> # A tibble: 2 × 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
  group_analysis_summary(sample, "age")
#> # A tibble: 2 × 5
#>   variable level      patients events hazard_ratio
#>   <chr>    <chr>         <int>  <int>        <dbl>
#> 1 age      under 60        280    125        0.844
#> 2 age      60 or over      339    166        0.583
  group_analysis_summary(optimized, "age")
#> # A tibble: 2 × 5
#>   variable level      patients events hazard_ratio
#>   <chr>    <chr>         <int>  <int>        <dbl>
#> 1 age      under 60        280    125        0.844
#> 2 age      60 or over      339    166        0.583