Skip to contents

A function to return the stat function values of a given tidy_ distribution output.

Usage

tidy_stat_tbl(
  .data,
  .x = y,
  .fns,
  .return_type = "vector",
  .use_data_table = FALSE,
  ...
)

Arguments

.data

The input data coming from a tidy_ distribution function.

.x

The default is y but can be one of the other columns from the input data.

.fns

The default is IQR, but this can be any stat function like quantile or median etc.

.return_type

The default is "vector" which returns an sapply object.

.use_data_table

The default is FALSE, TRUE will use data.table under the hood and still return a tibble. If this argument is set to TRUE then the .return_type parameter will be ignored.

...

Addition function arguments to be supplied to the parameters of .fns

Value

A return of object of either sapply

lapply or tibble based upon user input.

Details

A function to return the value(s) of a given tidy_ distribution function output and chosen column from it. This function will only work with tidy_ distribution functions.

There are currently three different output types for this function. These are:

  • "vector" - which gives an sapply() output

  • "list" - which gives an lapply() output, and

  • "tibble" - which returns a tibble in long format.

Currently you can pass any stat function that performs an operation on a vector input. This means you can pass things like IQR, quantile and their associated arguments in the ... portion of the function.

This function also by default will rename the value column of the tibble to the name of the function. This function will also give the column name of sim_number for the tibble output with the corresponding simulation numbers as the values.

For the sapply and lapply outputs the column names will also give the simulation number information by making column names like sim_number_1 etc.

There is an option of .use_data_table which can greatly enhance the speed of the calculations performed if used while still returning a tibble. The calculations are performed after turning the input data into a data.table object, performing the necessary calculation and then converting back to a tibble object.

See also

Author

Steven P. Sanderson II, MPH

Examples

tn <- tidy_normal(.num_sims = 3)

p <- c(0.025, 0.25, 0.5, 0.75, 0.95)

tidy_stat_tbl(tn, y, quantile, "vector", probs = p, na.rm = TRUE)
#>      sim_number_1 sim_number_2 sim_number_3
#> 2.5% -1.155744347   -1.5520265   -1.7105114
#> 25%  -0.546097181   -0.5562330   -0.6214001
#> 50%  -0.003455774   -0.1364431   -0.2174605
#> 75%   0.791361807    0.9394630    0.7260055
#> 95%   1.652023410    1.4163207    1.4707561
tidy_stat_tbl(tn, y, quantile, "list", probs = p)
#> $sim_number_1
#>         2.5%          25%          50%          75%          95% 
#> -1.155744347 -0.546097181 -0.003455774  0.791361807  1.652023410 
#> 
#> $sim_number_2
#>       2.5%        25%        50%        75%        95% 
#> -1.5520265 -0.5562330 -0.1364431  0.9394630  1.4163207 
#> 
#> $sim_number_3
#>       2.5%        25%        50%        75%        95% 
#> -1.7105114 -0.6214001 -0.2174605  0.7260055  1.4707561 
#> 
tidy_stat_tbl(tn, y, quantile, "tibble", probs = p)
#> # A tibble: 15 × 3
#>    sim_number name  quantile
#>    <fct>      <chr>    <dbl>
#>  1 1          2.5%  -1.16   
#>  2 1          25%   -0.546  
#>  3 1          50%   -0.00346
#>  4 1          75%    0.791  
#>  5 1          95%    1.65   
#>  6 2          2.5%  -1.55   
#>  7 2          25%   -0.556  
#>  8 2          50%   -0.136  
#>  9 2          75%    0.939  
#> 10 2          95%    1.42   
#> 11 3          2.5%  -1.71   
#> 12 3          25%   -0.621  
#> 13 3          50%   -0.217  
#> 14 3          75%    0.726  
#> 15 3          95%    1.47   
tidy_stat_tbl(tn, y, quantile, .use_data_table = TRUE, probs = p, na.rm = TRUE)
#> # A tibble: 15 × 3
#>    sim_number name  quantile
#>    <fct>      <fct>    <dbl>
#>  1 1          2.5%  -1.16   
#>  2 1          25%   -0.546  
#>  3 1          50%   -0.00346
#>  4 1          75%    0.791  
#>  5 1          95%    1.65   
#>  6 2          2.5%  -1.55   
#>  7 2          25%   -0.556  
#>  8 2          50%   -0.136  
#>  9 2          75%    0.939  
#> 10 2          95%    1.42   
#> 11 3          2.5%  -1.71   
#> 12 3          25%   -0.621  
#> 13 3          50%   -0.217  
#> 14 3          75%    0.726  
#> 15 3          95%    1.47