Skip to contents

vignette("sampling", package = "retrogroup") defines methods for sampling subgroup levels based on estimated posterior probabilities. Over a large number of simulations, subgroup hazard ratios computed on the simulated datasets should approximately match the assumed hazard ratios on average, provided the patient-level data are consistent with the model. However, each individual simulated dataset is subject to random variation and may not exactly match the assumed statistics. This vignette defines methods to refine an individual simulated dataset so that the observed subgroup hazard ratios match the assumed hazard ratios.

We define two optimization methods: one for subgroup variables produced with unconstrained sampling, and another for those produced with constrained sampling. Both methods are the same greedy local search. Starting from sampled subgroup levels \(x_1, \ldots, x_n\), each iteration proposes a small random change to them and keeps the change only if it reduces a loss function. The methods share the loss function and differ only in the moves they propose: the constrained method uses only swaps, which preserve the subgroup counts, and the unconstrained method uses both flips and swaps.

The sections below define the loss function, the two moves, the two methods, and the stopping rule.

Loss function

The loss function measures the distance between the assumed subgroup hazard ratios and the hazard ratios observed under the current subgroup levels \(x_1, \ldots, x_n\).

For the hazard ratio of subgroup level \(k\), we fit a Cox proportional hazards model to the patients currently assigned to that level, \(\{i : x_i = k\}\), with treatment \(z_i\) as the only covariate. The model is fit by partial likelihood, which leaves the baseline hazard unspecified (Cox 1972, 1975). Tied event times use the approximation of Efron (1977), which Therneau (2022) explains and works through on a small example. If the partial likelihood has no finite maximum, for example because one arm of level \(k\) has no events, then \(\hat{\beta}_k\) is undefined and the loss is infinite. Let \(\hat{\beta}_k\) be the estimated log hazard ratio of treatment versus control, and let \(\beta_k\) be the assumed log hazard ratio for subgroup level \(k\) from vignette("framing", package = "retrogroup").

The loss is the Euclidean distance between the observed and assumed hazard ratios:

\[ \Delta := \sqrt{\sum_{k = 1}^K \left( \exp \left (\hat{\beta}_k\right) - \exp \left (\beta_k\right) \right)^2} \]

Both \(\hat{\beta}_k\) and \(\Delta\) depend on the current subgroup levels and change as the search proceeds.

The loss does not steer the observed number of patients or the observed number of events in each level. Under unconstrained sampling, the observed number of patients in level \(k\) can drift away from \(n_k\) during the search. Under constrained sampling, swaps keep it at \(n_k\). The square root puts the loss, and therefore the user-specified tolerance, on the same scale as the hazard ratios.

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, we set \(\Delta = \infty\).

Moves

Each iteration of either method takes exactly one step. There are two kinds of step: a flip step, which reassigns one patient, and a swap step, which exchanges the subgroup levels of two patients.

Flip

Let \(\Delta^{(i \to k)}\) denote the loss that would result from moving patient \(i\) to subgroup level \(k\) and leaving every other patient in place. In particular, \(\Delta^{(i \to x_i)}\) is the current loss \(\Delta\). A flip step:

  1. Draws a patient \(i\) uniformly at random from \(1, \ldots, n\).
  2. Computes the counterfactual loss \(\Delta^{(i \to k)}\) for every subgroup level \(k = 1, \ldots, K\).
  3. Sets \(x_i \leftarrow k^*\), where \(k^* := \operatorname{argmin}_k \Delta^{(i \to k)}\).

The candidates in step 2 include the current level \(x_i\), so a flip step never increases the loss. If the current level \(x_i\) ties for the minimum, then \(k^* = x_i\) and the patient stays put.

Step 2 needs \(K\) model fits: one for level \(x_i\) without patient \(i\), and one for each of the other \(K - 1\) levels with patient \(i\) added. The candidate \(k = x_i\) needs no fit because its loss is the current loss \(\Delta\).

A flip that moves a patient changes the number of patients in both the level the patient leaves and the level the patient joins, so flips do not preserve the subgroup counts.

Swap

Let \(\Delta^{(i \leftrightarrow j)}\) denote the loss that would result from exchanging the subgroup levels of patients \(i\) and \(j\) and leaving every other patient in place. A swap step:

  1. Draws a pair of patients \((i, j)\) uniformly at random from the pairs with \(x_i \ne x_j\).
  2. Computes the loss \(\Delta^{(i \leftrightarrow j)}\) of the swap.
  3. Exchanges \(x_i\) and \(x_j\) if \(\Delta^{(i \leftrightarrow j)} < \Delta\), and otherwise leaves the subgroup levels unchanged.

Step 3 accepts only a strict reduction, so a swap step never increases the loss. If no pair has \(x_i \ne x_j\), no swap is possible and the subgroup levels stay unchanged.

Step 2 needs two model fits: one for each of the two levels that exchange a patient.

A swap removes one patient from each of the two levels and adds one patient to each, so swaps preserve the number of patients in every level.

Comparing the moves

Flip Swap
Patients reassigned at most one at most two
Candidates compared all \(K\) levels of patient \(i\) one exchange
Model fits \(K\) 2
Subgroup counts not preserved preserved
Loss never increases never increases

The two moves have three properties in common. First, each draw is with replacement and independent of previous iterations. Second, a flip or a swap changes the patients in two subgroup levels only, so every other term of the loss carries over from the previous iteration. Third, neither move ever increases the loss.

The two moves also complement each other. Flips change the sizes and event counts of subgroup levels. Swaps make fine adjustments to the hazard ratios while the sizes stay fixed. A search can therefore reach a point where no flip reduces the loss but some swap does.

Optimization for unconstrained sampling

Unconstrained sampling draws subgroup levels independently. We expect the observed number of patients in each subgroup level to deviate from the assumed subgroup counts \(n_1, \ldots, n_K\), and likewise for the event counts \(e_1, \ldots, e_K\).

Each iteration takes a flip step with probability \(1/2\) and a swap step otherwise. If no swap is possible, the iteration takes a flip step instead.

Nothing in the optimization preserves the subgroup counts. To preserve the subgroup counts, we need constrained sampling and its optimization.

Optimization for constrained sampling

Constrained sampling ensures that the observed number of patients in each subgroup level matches the assumed subgroup counts \(n_1, \ldots, n_K\). The optimization method for constrained sampling adjusts subgroup assignments to approximate the assumed hazard ratios while maintaining the assumed subgroup counts.

Every iteration takes a swap step, so the subgroup counts stay at \(n_1, \ldots, n_K\).

Stopping

Both methods stop when the loss falls below a tolerance or after a maximum number of iterations, whichever comes first.

Tolerance

Because the loss is a Euclidean distance on the linear scale of the hazard ratio, the tolerance is on the linear scale, not the log scale. For every level \(k\), each term of the loss is bounded by the loss itself:

\[ \left|\exp \left(\hat{\beta}_k\right) - \exp \left(\beta_k\right)\right| \le \Delta \]

So a loss below the tolerance means that the observed hazard ratio of every subgroup level is within the tolerance of its assumed value.

The default tolerance of \(10^{-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 until the maximum number of iterations.

Convergence

The loss is non-increasing and bounded below by zero, so it converges. The search is greedy, however, so it may settle in a local minimum where no available move improves the loss: no flip or swap for unconstrained sampling, and no swap for constrained sampling. In particular, if the starting loss is infinite and no available move makes every model estimable, the search cannot make progress. Rerunning from freshly sampled subgroup levels gives a different starting point.

References

Cox, D. R. 1972. “Regression Models and Life-Tables.” Journal of the Royal Statistical Society: Series B (Methodological) 34 (2): 187–220. https://doi.org/10.1111/j.2517-6161.1972.tb00899.x.
Cox, D. R. 1975. “Partial Likelihood.” Biometrika 62 (2): 269–76. https://doi.org/10.1093/biomet/62.2.269.
Efron, Bradley. 1977. “The Efficiency of Cox’s Likelihood Function for Censored Data.” Journal of the American Statistical Association 72 (359): 557–65. https://doi.org/10.1080/01621459.1977.10480613.
Therneau, Terry M. 2022. Software Validation. https://CRAN.R-project.org/package=survival/vignettes/validate.pdf.