Tidy Stats of Tidy Distribution — tidy_stat_tbl • TidyDensity
Description: A function to return the stat function values of a given tidy_ distribution output.

Skip to contents

# Tidy Stats of Tidy Distribution

Source: `R/stats-tidy-stat-func-tbl.R`
`tidy_stat_tbl.Rd`

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

Other Statistic: `ci_hi()`, `ci_lo()`, `tidy_kurtosis_vec()`, `tidy_range_statistic()`, `tidy_skewness_vec()`

## 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%   -2.0532484   -1.5091440  -1.61103416
    #> 25%    -0.7107474   -0.7929299  -0.50042232
    #> 50%    -0.1266762   -0.2242843   0.00983759
    #> 75%     0.5520628    0.5760688   0.54475305
    #> 95%     1.3622959    1.2628832   1.31342779
    tidy_stat_tbl(tn, y, quantile, "list", probs = p)
    #> $sim_number_1
    #>       2.5%        25%        50%        75%        95%
    #> -2.0532484 -0.7107474 -0.1266762  0.5520628  1.3622959
    #>
    #> $sim_number_2
    #>       2.5%        25%        50%        75%        95%
    #> -1.5091440 -0.7929299 -0.2242843  0.5760688  1.2628832
    #>
    #> $sim_number_3
    #>        2.5%         25%         50%         75%         95%
    #> -1.61103416 -0.50042232  0.00983759  0.54475305  1.31342779
    #>
    tidy_stat_tbl(tn, y, quantile, "tibble", probs = p)
    #> # A tibble: 15 × 3
    #>    sim_number name  quantile
    #>    <fct>      <chr>    <dbl>
    #>  1 1          2.5%  -2.05
    #>  2 1          25%   -0.711
    #>  3 1          50%   -0.127
    #>  4 1          75%    0.552
    #>  5 1          95%    1.36
    #>  6 2          2.5%  -1.51
    #>  7 2          25%   -0.793
    #>  8 2          50%   -0.224
    #>  9 2          75%    0.576
    #> 10 2          95%    1.26
    #> 11 3          2.5%  -1.61
    #> 12 3          25%   -0.500
    #> 13 3          50%    0.00984
    #> 14 3          75%    0.545
    #> 15 3          95%    1.31
    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%  -2.05
    #>  2 1          25%   -0.711
    #>  3 1          50%   -0.127
    #>  4 1          75%    0.552
    #>  5 1          95%    1.36
    #>  6 2          2.5%  -1.51
    #>  7 2          25%   -0.793
    #>  8 2          50%   -0.224
    #>  9 2          75%    0.576
    #> 10 2          95%    1.26
    #> 11 3          2.5%  -1.61
    #> 12 3          25%   -0.500
    #> 13 3          50%    0.00984
    #> 14 3          75%    0.545
    #> 15 3          95%    1.31