Skip to contents

Refine sampled subgroup levels so that the subgroup statistics observed in the data match the assumed subgroup statistics in summary. Patient counts are not preserved.

Usage

group_optimize_unconstrained(
  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. A flip step fits one Cox model per subgroup level, and a swap step 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. All other columns are unchanged.

Details

group_optimize_unconstrained() implements the "Optimization for unconstrained sampling" section of vignette("optimization", package = "retrogroup"), separately for each subgroup variable. The loss \(\Delta\) is the Euclidean distance between the observed and assumed hazard ratios of the levels of the subgroup variable, as defined in the "Loss function" section of that vignette. The loss does not steer the number of patients or the number of events in each level. 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 the level is empty or one of its arms has no events, the loss is infinite.

Each iteration takes a flip step with probability 1/2 and a swap step otherwise, as defined in the "Moves" section of the vignette. If no swap is possible, the iteration takes a flip step instead. A flip step draws a patient \(i\) uniformly at random and moves that patient to the subgroup level \(k\) that minimizes \(\Delta^{(i \to k)}\), or leaves the patient in place if the current level ties for the minimum. 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. Neither step ever increases the loss. 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_unconstrained() 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. The number of patients and the number of events in each subgroup level are not guaranteed to equal the assumed counts in summary. Use group_optimize_constrained() on the output of group_sample_constrained() to preserve the assumed number of patients.

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 output of group_sample_unconstrained() 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_constrained()

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_unconstrained(patient, summary)
  optimized <- group_optimize_unconstrained(
    sample,
    summary,
    tolerance = 1e-2,
    iterations = 100L
  )
  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        284    126        0.838
#> 2 age      60 or over      335    165        0.585
  group_analysis_summary(optimized, "age")
#> # A tibble: 2 × 5
#>   variable level      patients events hazard_ratio
#>   <chr>    <chr>         <int>  <int>        <dbl>
#> 1 age      under 60        284    126        0.838
#> 2 age      60 or over      335    165        0.585