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 bootstrap_unnest_tbl() #190

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

Add function bootstrap_unnest_tbl() #190

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:

bootstrap_unnest_tbl <- function(.data){
  
  # Checks ----
  atb <- attributes(.data)
  distribution_family_type <- atb$dist_type
  
  if (!atb$tibble_type == "tidy_bootstrap"){
    rlang::abort(
      message = "You must provide the output from the tidy_bootstrap() function.",
      use_cli_format = TRUE
    )
  }
  
  # Data ----
  df <- tidyr::unnest(.data, bootstrap_samples) %>%
    purrr::set_names("sim_number", "y")
  
  # Return ----
  attr(df, ".num_sims") <- atb$.num_sims
  attr(df, "distribution_family_tpe") <- distribution_family_type
  attr(df, "tibble_type") <- "tidy_bootstrap"
  attr(df, "dist_with_params") <- "Empirical"
  
  return(df)
  
}

Example:

> tb <- tidy_bootstrap(.x = mtcars$mpg)
> bootstrap_unnest_tbl(tb)
# A tibble: 50,000 x 2
   sim_number     y
   <fct>      <dbl>
 1 1           22.8
 2 1           17.3
 3 1           22.8
 4 1           19.2
 5 1           14.3
 6 1           24.4
 7 1           18.7
 8 1           21  
 9 1           10.4
10 1           21.5
# ... with 49,990 more rows
> 
> bootstrap_unnest_tbl(tb) %>%
+   tidy_distribution_summary_tbl(sim_number)
# A tibble: 2,000 x 11
   sim_number mean_val median_val std_val min_val max_val skewness kurtosis range   iqr
   <fct>         <dbl>      <dbl>   <dbl>   <dbl>   <dbl>    <dbl>    <dbl> <dbl> <dbl>
 1 1              19.4       19.2    5.19    10.4    30.4    0.251     2.87  20    7.8 
 2 2              19.9       19.2    5.23    10.4    33.9    0.487     3.69  23.5  3.7 
 3 3              19.3       18.7    5.17    10.4    33.9    0.761     4.14  23.5  4.10
 4 4              20.0       19.7    5.92    10.4    33.9    0.701     3.33  23.5  6.3 
 5 5              18.4       18.7    4.65    10.4    32.4    0.762     4.62  22    5.5 
 6 6              19.2       17.8    6.27    10.4    33.9    0.951     3.62  23.5  6   
 7 7              22.2       19.7    6.48    14.7    33.9    0.682     1.93  19.2 13.1 
 8 8              20.2       19.2    6.83    10.4    33.9    0.556     2.50  23.5  7.6 
 9 9              19.5       17.3    7.13    10.4    33.9    0.747     2.36  23.5  6.3 
10 10             19.8       18.1    5.72    10.4    32.4    0.999     3.56  22    5.9 
# ... with 1,990 more rows, and 1 more variable: variance <dbl>
> 
> bootstrap_unnest_tbl(tb) %>%
+   tidy_distribution_summary_tbl()
# A tibble: 1 x 10
  mean_val median_val std_val min_val max_val skewness kurtosis range   iqr variance
     <dbl>      <dbl>   <dbl>   <dbl>   <dbl>    <dbl>    <dbl> <dbl> <dbl>    <dbl>
1     20.1       19.2    5.92    10.4    33.9    0.641     2.81  23.5   7.3     35.0
> 
> mean(mtcars$mpg)
[1] 20.09062
@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