Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function tidy_bootstrap() that returns a list tibble #189

Closed
spsanderson opened this issue May 19, 2022 · 0 comments
Closed

Add function tidy_bootstrap() that returns a list tibble #189

spsanderson opened this issue May 19, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@spsanderson
Copy link
Owner

spsanderson commented May 19, 2022

Function:

tidy_bootstrap <- function(.x, .num_sims = 2000, .proportion = 0.8,
                           .distribution_type = "continuous"){
  
  # Tidyeval ----
  x_term <- as.numeric(.x)
  n <- length(x_term)
  dist_type <- tolower(as.character(.distribution_type))
  num_sims <- as.integer(.num_sims)
  size <- as.numeric(.proportion)
  
  # Checks ----
  if (!is.vector(x_term)) {
    rlang::abort(
      message = "You must pass a vector as the .x argument to this function.",
      use_cli_format = TRUE
    )
  }
  
  if (size < 0 | size > 1){
    rlang::abort(
      message = "The '.proportion' parameter must be between 0 and 1 inclusive.",
      use_cli_format = TRUE
    )
  }
  
  if (!dist_type %in% c("continuous","discrete")){
    rlang::abort(
      message = "You must choose either 'continuous' or 'discrete'.",
      use_cli_format = TRUE
    )
  }
  
  if (num_sims < 2000){
    rlang::warn(
      message = "Setting '.num_sims' to less than 2000 means that results can be
      potentially unstable. Consider setting to 2000 or more.",
      use_cli_format = TRUE
    )
  }
  
  # Data ----
  df <- dplyr::tibble(sim_number = as.factor(1:num_sims)) %>%
    dplyr::group_by(sim_number) %>%
    dplyr::mutate(bootstrap_samples = list(
      sample(x = x_term, size = floor(size * n) ,replace = TRUE)
      )
    ) %>%
    dplyr::ungroup()
  
  # Attach descriptive attributes to tibble
  attr(df, "distribution_family_type") <- dist_type
  attr(df, ".x") <- .x
  attr(df, ".num_sims") <- .num_sims
  attr(df, "tibble_type") <- "tidy_bootstrap"
  attr(df, "dist_with_params") <- "Empirical"
  
  # Return ----
  return(df)
  
}

Example:

> x <- mtcars$mpg
> tb <- tidy_bootstrap(x, .num_sims = 200)
Warning message:
Setting '.num_sims' to less than 2000 means that results can be potentially unstable.
Consider setting to 2000 or more. 

> tb <- tidy_bootstrap(x, .num_sims = 2000)
> tb
# A tibble: 2,000 x 2
   sim_number bootstrap_samples
   <fct>      <list>           
 1 1          <dbl [25]>       
 2 2          <dbl [25]>       
 3 3          <dbl [25]>       
 4 4          <dbl [25]>       
 5 5          <dbl [25]>       
 6 6          <dbl [25]>       
 7 7          <dbl [25]>       
 8 8          <dbl [25]>       
 9 9          <dbl [25]>       
10 10         <dbl [25]>       
# ... with 1,990 more rows
@spsanderson spsanderson added the enhancement New feature or request label May 19, 2022
@spsanderson spsanderson added this to the TidyDensity v1.2.0 milestone May 19, 2022
@spsanderson spsanderson self-assigned this May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant