Learn R Programming

asympDiag (version 0.3.1)

parametric_bootstrap: Perform Parametric Bootstrap Simulations

Description

This function performs parametric bootstrap simulations by generating new response values based on the fitted model, refitting the model on these new responses, and computing a user-defined statistic for each simulated model.

Usage

parametric_bootstrap(
  model,
  statistic,
  nsim,
  responses = NULL,
  refit_fn = NULL,
  show_progress = TRUE,
  simplify = TRUE,
  stat_hc = NULL,
  show_message_count = TRUE,
  show_warning_count = TRUE,
  show_not_converged_count = TRUE,
  ...
)

Value

A list containing the following elements:

result

A list of length nsim if simplify is FALSE. Otherwise an atomic vector or matrix or list of length nsim.

responses

The list of simulated response values.

simulation_warning

A logical vector indicating whether a warning occurred during model refitting for each simulation.

converged

A logical vector indicating whether the model refit converged for each simulation.

Arguments

model

A fitted model object that will be used to simulate responses.

statistic

A function that computes the desired statistic from the refitted model. It must take the refitted model as an argument.

nsim

The number of simulations to perform.

responses

An optional list of values to be used as response variables to refit the model.

refit_fn

Function to refit the model with new responses. If NULL, defaults to get_refit(model, y, ...).

show_progress

Display a progress bar for the simulation iteration.

simplify

logical or character string; should the result be simplified to a vector, matrix or higher dimensional array if possible? If occurs any errors during the refit procedure, the results will be a list, regardless of the value of this argument.

stat_hc

A function that verifies if the computed statistic is correct. It should return nothing, just throw errors to halt execution.

show_message_count

Show total of captured messages from refit_fn as a message. It only shows if the number of messages is greater than 0.

show_warning_count

Show total of captured warnings from refit_fn as a warning. It only shows if the number of warnings is greater than 0.

show_not_converged_count

Show total of models that didn't converge as a warning. It only shows if the number of models that didn't converged is greater than 0.

...

Additional arguments to be passed to refit_fn.

Details

This function implements a parametric bootstrap procedure. It generates new response values from the fitted model, refits the model for each simulated response, and computes a user-defined statistic on the refitted model. The refit function can be customized through the refit_fn argument.

If show_progress is TRUE, the progress of the simulations will be displayed using a progress bar from the cli package.

Examples

Run this code

model <- lm(mpg ~ wt + hp, data = mtcars)
statistic <- function(model) coef(model)[["wt"]]
bootstrap_results <- parametric_bootstrap(model, statistic, nsim = 100)
wt_coefs <- Reduce(c, bootstrap_results$result)
summary(wt_coefs)

Run the code above in your browser using DataLab