
Bootstrap confidence intervals for model coefficients
Source:R/bootstrap-coefs.R
bootstrap_model_coefs.RdResamples 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()orvglm_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
workersparameter instead.- id_var
Name of the patient ID column in the stored data. The default,
NULL, uses theid_vargiven when the model was fitted. Supply it only when the model was fitted withoutid_var.- use_coefstart
Logical. If
TRUE,vglmrefits start from the original model coefficients, which can speed up convergence. Starting values, including acoefstartpassed tovglm_markov(), are used only when a resample contains every outcome state; a refit with starting values that fails is retried once without them. Default isFALSE.
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:
Draws patients with replacement from the stored data
Renumbers outcome and previous-state levels to consecutive integers when a state is absent from the resample
Updates the
rmsdatadist forormmodels (safe with future.callr workers)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
)
} # }