Package 'retrogroup'

Title: Reconstruct Patient-Level Subgroup Membership in Survival Data
Description: 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.
Authors: William Michael Landau [aut, cre] (ORCID: <https://orcid.org/0000-0003-1878-3253>), Eli Lilly and Company [cph, fnd]
Maintainer: William Michael Landau <[email protected]>
License: MIT + file LICENSE
Version: 0.0.3
Built: 2026-12-04 09:19:26 UTC
Source: https://github.com/wlandau/retrogroup

Help Index


Posterior subgroup probabilities

Description

Estimate the posterior probability that each patient belongs to each level of each subgroup variable, given the patient-level survival data and the assumed subgroup statistics.

Usage

group_analysis_probability(patient, summary)

Arguments

patient

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

summary

Summary subgroup statistics, the output of group_data_summary().

Details

group_analysis_probability() computes the estimated posterior probabilities pikp_{ik} defined in the "Posterior probability estimation" section of vignette("model", package = "retrogroup"), separately for each subgroup variable. See that section for the formulas, including the error when any probability is numerically indistinguishable from zero.

The probabilities depend on the estimated prognostic effects γ^k\hat{\gamma}_k and the estimated baseline cumulative hazard Λ^0(t)\hat{\Lambda}_0(t) of vignette("estimates", package = "retrogroup"). The prognostic effects come from the event counts, the patient counts, and the hazard ratios in summary, after clamping each event count to between 1/2 and nk−1/2n_k - 1/2. The baseline cumulative hazard is a modified Breslow estimate that uses the prognostic effects.

The total number of patients of every subgroup variable in summary must equal the number of rows of patient, and the total number of events of every subgroup variable must equal the number of events in patient. See the details of group_data_summary() for how to choose the counts.

Value

A named list with one element per subgroup variable, in the order the subgroup variables first appear in summary$variable. Each element is a numeric matrix of the estimated posterior probabilities pikp_{ik} of that subgroup variable:

  • Rows correspond to patients, in the same order as the rows of patient.

  • Columns correspond to the levels of the subgroup variable. The column names are exactly the levels, in the same order as in summary.

  • Each row sums to 1.

See Also

Other analysis: group_analysis_summary()

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"
  )
  probability <- group_analysis_probability(patient, summary)
  names(probability)
  head(probability$age)

Subgroup-level statistics

Description

Compute the number of patients, the number of events, and the hazard ratio of treatment versus control in each level of each subgroup variable of a patient-level dataset.

Usage

group_analysis_summary(
  patient,
  variables = setdiff(colnames(patient), c("treatment", "time", "event"))
)

Arguments

patient

Patient-level survival data with subgroup columns, usually the output of group_sample_unconstrained(), group_sample_constrained(), group_optimize_unconstrained(), or group_optimize_constrained(). Must have the columns of the output of group_data_patient() and the subgroup columns named in variables.

variables

Character vector of unique names of subgroup columns in patient. Each subgroup column must be character or factor with no missing values. Defaults to every column of patient other than treatment, time, and event.

Details

For each level of each subgroup variable, group_analysis_summary() counts the patients and events in that level. These are the quantities nkn_k and eke_k of vignette("framing", package = "retrogroup"), computed from the subgroup assignments in patient instead of assumed.

group_analysis_summary() also fits a Cox proportional hazards model by partial likelihood to the patients in each level, with treatment as the only covariate and the Efron method for ties. The fit uses a compiled Newton solver specialized to one binary covariate, and its estimate agrees with survival::coxph(ties = "efron") up to rounding error whenever the partial likelihood has a finite maximum. The hazard ratio is the exponential of the estimated coefficient of treatment versus control, the estimate of exp⁡(βk)\exp(\beta_k) of vignette("framing", package = "retrogroup"). The log hazard ratio is the observed statistic β^k(x)\hat\beta_k(x) of the "Loss function" section of vignette("optimization", package = "retrogroup").

If the partial likelihood of some subgroup level has no finite maximum, for example because one of its arms has no patients or no events, the hazard_ratio of that level is NA. In those cases, survival::coxph() instead stops at a large finite estimate with a warning.

The levels of a factor subgroup column are its factor levels, in order, excluding levels with no patients. The levels of a character subgroup column are its sorted unique values.

Value

A tibble with one row per subgroup level, ordered by subgroup variable as in variables and then by level, and the following columns:

  • variable: character, name of the subgroup variable.

  • level: character, level of the subgroup variable.

  • patients: integer, number of patients in the subgroup level.

  • events: integer, number of events in the subgroup level.

  • hazard_ratio: numeric, estimated hazard ratio of treatment versus control in the subgroup level, or NA if the model cannot be fit.

See Also

Other analysis: group_analysis_probability()

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"
  )
  set.seed(0)
  sample <- group_sample_constrained(patient, summary)
  group_analysis_summary(sample)
  summary

Approximate subgroup event counts

Description

Approximate the number of events in each subgroup level from the confidence intervals of the hazard ratios, for publications that do not report subgroup event counts.

Usage

group_data_events(patient, summary, lower, upper, confidence = 0.95)

Arguments

patient

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

summary

Summary subgroup statistics, the output of group_data_summary(), usually with events = NULL. The event counts in summary, if any, are ignored.

lower

Numeric vector with one element per row of summary: the lower bound bk−b_k^- of the two-sided confidence interval of the hazard ratio of each subgroup level, on the natural scale (not the log scale). Every element must be finite and strictly positive.

upper

Numeric vector with one element per row of summary: the upper bound bk+b_k^+ of the two-sided confidence interval of the hazard ratio of each subgroup level, on the natural scale (not the log scale). Every element must be finite, strictly positive, and strictly greater than the corresponding element of lower.

confidence

Number strictly between 0 and 1, the confidence level 1−α1 - \alpha of the confidence intervals.

Details

group_data_events() implements vignette("events", package = "retrogroup"), separately for each subgroup variable. It assumes the confidence interval of each hazard ratio is a Wald interval computed on the log scale, so the interval of the log hazard ratio βk\beta_k is symmetric about βk\beta_k with half-width cσkc \sigma_k. Equating σk2\sigma_k^2 with the variance approximation of Schoenfeld (1983), as in Parmar et al. (1998), the approximate number of events in subgroup level kk is

e^k=1zˉ(1−zˉ)4c2(log⁡(bk+)−log⁡(bk−))2\hat{e}_k = \frac{1}{\bar{z} (1 - \bar{z})} \frac{4 c^2}{(\log(b_k^+) - \log(b_k^-))^2}

where bk−b_k^- is lower, bk+b_k^+ is upper, cc is the 1−α/21 - \alpha / 2 quantile of the standard normal distribution for confidence =1−α= 1 - \alpha, and zˉ\bar{z} is the proportion of patients in patient randomized to treatment. Using zˉ\bar{z} for every level assumes the treatment allocation within each level matches the overall allocation, which randomization supports. The approximation does not use the hazard ratios in summary.

The approximate counts do not necessarily add up to the total number of events in patient, and they may exceed the number of patients of the level. So group_data_events() rounds each e^k\hat{e}_k to the nearest integer and clamps it to between 0 and nkn_k. Then it makes passes over the levels in the order of summary, adding 1 to (or subtracting 1 from) each level that has room, until the counts of each subgroup variable add up to the total number of events in patient. The result satisfies all the requirements on event counts of group_data_summary().

This is only one of many possible ways to choose event counts. The choice is a decision about the reconstruction, and it is your responsibility. Please disclose the event counts used and how they were chosen wherever the reconstructed data or downstream results are reported.

summary must satisfy the requirements of group_analysis_probability(), except that its event counts are ignored. Usually summary is the output of group_data_summary() with events = NULL.

Value

A tibble with the same rows and columns as the output of group_data_summary(), in the same order as summary. The events column has the reconciled event counts. All other columns are the same as in summary.

References

Parmar MKB, Torri V, Stewart L (1998). Extracting summary statistics to perform meta-analyses of the published literature for survival endpoints. Statistics in Medicine, 17(24), 2815-2834. doi:10.1002/(SICI)1097-0258(19981230)17:24<2815::AID-SIM110>3.0.CO;2-8

Schoenfeld DA (1983). Sample-size formula for the proportional-hazards regression model. Biometrics, 39(2), 499-503. doi:10.2307/2531021

See Also

Other data: group_data_patient(), group_data_summary()

Examples

patient <- group_data_patient(
    group_example_patient(),
    arm = "arm",
    treatment = "Lev+5FU",
    time = "time",
    event = "event"
  )
  data <- group_example_summary()
  summary <- group_data_summary(
    data,
    variable = "variable",
    level = "level",
    patients = "patients",
    events = NULL,
    hazard_ratio = "hazard_ratio"
  )
  summary
  group_data_events(
    patient,
    summary,
    lower = data$lower,
    upper = data$upper
  )
  data[, c("variable", "level", "events")]

Patient-level survival data

Description

Convert a patient-level right-censored survival dataset from a randomized 2-arm trial into the standardized format used by retrogroup.

Usage

group_data_patient(data, arm, treatment, time, event)

Arguments

data

Data frame with one row per patient. Must have the columns named by arm, time, and event. Other columns are ignored.

arm

Character string, name of the column in data with the treatment arm. Must have no missing values and exactly two unique values.

treatment

The value of data[[arm]] that denotes the treatment arm. The other value denotes the control arm.

time

Character string, name of the column in data with the time since randomization of the event or censoring. The column must be numeric, finite, non-negative, and free of missing values.

event

Character string, name of the column in data with the event indicator: 1 or TRUE for an event, 0 or FALSE for right censoring. Must have no missing values and at least one event.

Value

A tibble with one row per patient and the following columns:

  • treatment: integer, 1 for the treatment arm and 0 for the control arm.

  • time: numeric, time since randomization of the event or censoring.

  • event: integer, 1 if the event occurred and 0 if censored.

See Also

Other data: group_data_events(), group_data_summary()

Examples

data <- group_example_patient()
  group_data_patient(
    data,
    arm = "arm",
    treatment = "Lev+5FU",
    time = "time",
    event = "event"
  )

Summary subgroup statistics

Description

Convert assumed subgroup statistics into the standardized format used by retrogroup.

Usage

group_data_summary(data, variable, level, patients, events, hazard_ratio)

Arguments

data

Data frame with one row per subgroup level. Must have the columns named by the other arguments. Other columns are ignored.

variable

Character string, name of the column in data with the name of the subgroup variable. The column must be character with non-empty, non-missing values.

level

Character string, name of the column in data with the level of the subgroup variable. The column must be character or factor with non-empty, non-missing values.

patients

Character string, name of the column in data with the number of patients to assign to each level of the reconstructed subgroup variable, usually taken from the publication. See the details section for when these counts may differ from the publication. The column must contain strictly positive integers with no missing values.

events

Character string, name of the column in data with the number of events to assign to each level of the reconstructed subgroup variable. The column must contain non-negative integers with no missing values, each at most the number of patients in the level. The event counts must have the same total for every subgroup variable. Alternatively, NULL to set every events value to NA and approximate the event counts later with group_data_events().

hazard_ratio

Character string, name of the column in data with the assumed hazard ratio of treatment versus control in each subgroup level, on the natural scale (not the log scale). The column must be numeric, finite, and strictly positive with no missing values.

Details

Each row of data is one level of a subgroup variable, with the number of patients nkn_k, the number of events eke_k, and the hazard ratio exp⁡(βk)\exp(\beta_k) of treatment versus control defined in vignette("framing", package = "retrogroup"). A subgroup variable must have at least two levels, the levels must be unique within the subgroup variable, and every level must have a strictly positive number of patients.

The patient counts must have the same total for every subgroup variable, because every subgroup variable partitions the same patients. The counts are the sizes of the levels of the reconstructed subgroup variables, so they need not copy the publication verbatim. Published counts are the natural starting point, but they sometimes do not add up to the total number of patients in the study, for example because of missing subgroup data or typographical errors. In that case, choosing counts that add up to the total number of patients is a decision about the reconstruction, not an attempt to recover the true counts from the clinical study. Please disclose that decision transparently wherever the reconstructed data or downstream results are reported.

Likewise, the event counts must have the same total for every subgroup variable, and each level must satisfy 0≤ek≤nk0 \le e_k \le n_k. retrogroup uses the event counts to estimate prognostic effects, as in vignette("estimates", package = "retrogroup"). See the "Event counts" section of vignette("framing", package = "retrogroup") for how to choose event counts, and group_data_events() for approximating them from confidence intervals when the publication does not report them. For that purpose, events = NULL skips the checks on event counts and sets every events value to NA. Such a summary is only valid input to group_data_events(). Every other function rejects it.

Value

A tibble with one row per subgroup level, in the same order as data, and the following columns:

  • variable: character, name of the subgroup variable.

  • level: character, level of the subgroup variable.

  • patients: integer, number of patients nkn_k in the subgroup level.

  • events: integer, number of events eke_k in the subgroup level, or NA if events is NULL.

  • hazard_ratio: numeric, assumed hazard ratio exp⁡(βk)\exp(\beta_k) of treatment versus control in the subgroup level.

See Also

Other data: group_data_events(), group_data_patient()

Examples

group_data_summary(
    group_example_summary(),
    variable = "variable",
    level = "level",
    patients = "patients",
    events = "events",
    hazard_ratio = "hazard_ratio"
  )

Example patient-level survival data

Description

Return example patient-level data in the format expected by group_data_patient().

Usage

group_example_patient()

Details

The data come from the adjuvant colon cancer trial of levamisole plus fluorouracil (Moertel et al., 1990), as distributed in survival::colon. The subset keeps the death endpoint (etype == 2) and two arms: observation ("Obs", control) and levamisole plus fluorouracil ("Lev+5FU", treatment). The levamisole-only arm is excluded. See survival::colon for the full dataset and its documentation.

Value

A tibble with one row per patient and the following columns:

  • arm: character, study arm, either "Obs" or "Lev+5FU".

  • time: numeric, days from randomization to death or censoring.

  • event: integer, 1 if the patient died and 0 if censored.

References

Moertel CG, Fleming TR, Macdonald JS, et al. (1990). Levamisole and fluorouracil for adjuvant therapy of resected colon carcinoma. New England Journal of Medicine, 322(6), 352-358.

See Also

Other example: group_example_summary()

Examples

group_example_patient()

Example summary subgroup statistics

Description

Return example summary subgroup statistics in the format expected by group_data_summary().

Usage

group_example_summary()

Details

The statistics are computed from the same patients as group_example_patient(): the death endpoint (etype == 2) of the adjuvant colon cancer trial of levamisole plus fluorouracil (Moertel et al., 1990), as distributed in survival::colon, restricted to the observation ("Obs", control) and levamisole plus fluorouracil ("Lev+5FU", treatment) arms.

There are three subgroup variables:

  • "age": age at randomization, with levels "under 60" (younger than 60 years) and "60 or over" (60 years or older).

  • "node4": more than 4 positive lymph nodes, with levels "no" and "yes".

  • "sex": with levels "female" and "male".

Within each subgroup level, group_analysis_summary() counts the patients and events and fits a Cox proportional hazards model of survival on treatment ("Lev+5FU" versus "Obs") to get the hazard ratio. The columns lower and upper are the bounds of the 95% Wald confidence interval of the hazard ratio from the same model, as a publication would report them. group_data_summary() ignores these columns, but group_data_events() can use them to approximate the event counts.

Value

A tibble with one row per subgroup level and the following columns:

  • variable: character, name of the subgroup variable.

  • level: character, level of the subgroup variable.

  • patients: integer, number of patients in the subgroup level.

  • events: integer, number of events in the subgroup level.

  • hazard_ratio: numeric, estimated hazard ratio of treatment versus control in the subgroup level.

  • lower: numeric, lower bound of the 95% confidence interval of the hazard ratio.

  • upper: numeric, upper bound of the 95% confidence interval of the hazard ratio.

References

Moertel CG, Fleming TR, Macdonald JS, et al. (1990). Levamisole and fluorouracil for adjuvant therapy of resected colon carcinoma. New England Journal of Medicine, 322(6), 352-358.

See Also

Other example: group_example_patient()

Examples

group_example_summary()

Constrained subgroup optimization

Description

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.

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)(i, j) uniformly at random from the pairs with xi≠xjx_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.

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.

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
  group_analysis_summary(sample, "age")
  group_analysis_summary(optimized, "age")

Unconstrained subgroup optimization

Description

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.

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 ii uniformly at random and moves that patient to the subgroup level kk that minimizes Δ(i→k)\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)(i, j) uniformly at random from the pairs with xi≠xjx_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.

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.

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
  group_analysis_summary(sample, "age")
  group_analysis_summary(optimized, "age")

Constrained subgroup sampling

Description

Sample a level of each subgroup variable for each patient from the posterior probabilities estimated by group_analysis_probability(), such that the number of patients in each subgroup level equals the assumed number of patients in summary.

Usage

group_sample_constrained(patient, summary)

Arguments

patient

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

summary

Summary subgroup statistics, the output of group_data_summary().

Details

group_sample_constrained() implements the "Constrained sampling" section of vignette("sampling", package = "retrogroup"). For each subgroup variable, it iterates through a random permutation of patients and draws the level of each patient from a categorical distribution with probabilities proportional to the pikp_{ik} from group_analysis_probability(), restricted to the subgroup levels that have not yet reached their assumed counts. All the requirements of group_analysis_probability() on patient and summary apply. In addition, no subgroup variable in summary may have the same name as a column of patient.

The sampled number of patients in each subgroup level always equals summary$patients. However, the result is only an approximate draw from independent categorical distributions conditional on those counts. Use group_sample_unconstrained() for independent draws without the count constraint.

The sampled number of events in each subgroup level is not guaranteed to equal summary$events. The event counts enter only through the estimated prognostic effects of group_analysis_probability(), and the loss functions of group_optimize_constrained() and group_optimize_unconstrained() target hazard ratios only. vignette("prognostic", package = "retrogroup") examines the prognostic information of reconstructed subgroups. The statistician who uses the sampled data in an analysis is responsible for the consequences.

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

Value

A tibble with one row per patient, in the same order as the rows of patient. The columns are all the columns of patient, followed by one column per subgroup variable, in the order the subgroup variables first appear in summary$variable. Each subgroup column is a factor of the sampled levels whose levels are those of the subgroup variable in summary$level, in the same order.

See Also

Other sampling: group_sample_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"
  )
  set.seed(0)
  sample <- group_sample_constrained(patient, summary)
  sample
  table(sample$age)
  summary[summary$variable == "age", c("level", "patients")]

Unconstrained subgroup sampling

Description

Sample a level of each subgroup variable for each patient, independently across patients, from the posterior probabilities estimated by group_analysis_probability().

Usage

group_sample_unconstrained(patient, summary)

Arguments

patient

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

summary

Summary subgroup statistics, the output of group_data_summary().

Details

group_sample_unconstrained() implements the "Unconstrained sampling" section of vignette("sampling", package = "retrogroup"). For each subgroup variable and each patient ii, it draws xi∼Categorical(pi1,…,piK)x_i \sim \text{Categorical}(p_{i1}, \ldots, p_{iK}) independently, where the pikp_{ik} come from group_analysis_probability(). All the requirements of group_analysis_probability() on patient and summary apply. In addition, no subgroup variable in summary may have the same name as a column of patient.

The sampled number of patients in each subgroup level is not guaranteed to equal the assumed number of patients in summary. Use group_sample_constrained() to preserve the assumed counts.

vignette("prognostic", package = "retrogroup") examines the prognostic information of reconstructed subgroups.

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

Value

A tibble with one row per patient, in the same order as the rows of patient. The columns are all the columns of patient, followed by one column per subgroup variable, in the order the subgroup variables first appear in summary$variable. Each subgroup column is a factor of the sampled levels whose levels are those of the subgroup variable in summary$level, in the same order.

See Also

Other sampling: group_sample_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"
  )
  set.seed(0)
  sample <- group_sample_unconstrained(patient, summary)
  sample
  table(sample$age)