Skip to contents

Resamples patients with replacement, refits the model to each resample, and returns the refitted coefficients (intercepts and slopes). Percentiles of each column give bootstrap confidence intervals for that coefficient.

Usage

bootstrap_model_coefs(
  model,
  n_boot,
  workers = NULL,
  parallel = NULL,
  id_var = NULL,
  use_coefstart = FALSE
)

Arguments

model

A model fitted with orm_markov() or vglm_markov(). These wrappers store the data used for fitting, and the bootstrap resamples patients from those stored data.

n_boot

Number of bootstrap samples.

workers

Number of workers for parallel processing. If NULL (default) or 1, sequential processing is used. If > 1, uses parallel processing.

parallel

(Deprecated) Logical indicating whether to use parallel processing. Use the workers parameter instead.

id_var

Name of the patient ID column in the stored data. The default, NULL, uses the id_var given when the model was fitted. Supply it only when the model was fitted without id_var.

use_coefstart

Logical. If TRUE, vglm refits start from the original model coefficients, which can speed up convergence. Starting values, including a coefstart passed to vglm_markov(), are used only when a resample contains every outcome state; a refit with starting values that fails is retried once without them. Default is FALSE.

Value

A data frame with one row per bootstrap sample:

  • boot_id: Bootstrap iteration number

  • One column per model coefficient (intercepts and slopes). Failed refits give missing values.

Details

Patients, not rows, are resampled, so every resampled patient keeps all of their visits and the within-patient correlation is preserved. A patient drawn more than once enters the refit as separate patients. The refit uses the same fitting rows as the original model: rows excluded by subset stay excluded, and fitting weights stay attached to their rows.

For each bootstrap iteration:

  1. Draws patients with replacement from the stored data

  2. Renumbers outcome and previous-state levels to consecutive integers when a state is absent from the resample

  3. Updates the rms datadist for orm models (safe with future.callr workers)

  4. Refits the model and extracts its coefficients

When a state is missing from a resample, the refit has fewer intercepts than the original model, so the intercept columns of that row refer to renumbered states.

Parallelization is handled via future.callr::callr strategy, which provides isolated R processes for each worker, making it safe to modify the global environment (for datadist) without conflicts.

Previous-state type: yprev may be a factor for categorical previous-state effects or numeric for linear/spline effects. Bootstrap releveling preserves the column type used to fit the original model.

Examples

if (FALSE) { # \dontrun{
fit <- vglm_markov(
  ordered(y) ~ tx + yprev + time,
  family = VGAM::cumulative(reverse = TRUE, parallel = TRUE),
  data = my_data,
  id_var = "id"
)

# Resample the patients stored on the fit.
bs_coefs <- bootstrap_model_coefs(fit, n_boot = 1000)

# 95% percentile confidence intervals
apply(
  bs_coefs[, -1],
  2,
  stats::quantile,
  probs = c(0.025, 0.975),
  na.rm = TRUE
)
} # }