Closed spsanderson closed 2 years ago
Function:
#' Distribution Statistics
#'
#' @family Cauchy
#' @fmaily Distribution Statistics
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @details This function will take in a tibble and returns the statistics
#' of the given type of `tidy_` distribution. It is required that data be
#' passed from a `tidy_` distribution function.
#'
#' @description Returns distribution statistics in a tibble.
#'
#' @param .data The data being passed from a `tidy_` distribution function.
#'
#' @examples
#' tidy_cauchy() %>%
#' util_cauchy_stats_tbl()
#'
#' @return
#' A tibble
#'
#' @export
#'
util_cauchy_stats_tbl <- function(.data){
# Immediate check for tidy_ distribution function
if (!"tibble_type" %in% names(attributes(.data))){
rlang::abort(
message = "You must pass data from the 'tidy_dist' function.",
use_cli_format = TRUE
)
}
if (attributes(.data)$tibble_type != "tidy_cauchy"){
rlang::abort(
message = "You must use 'tidy_cauchy()'",
use_cli_format = TRUE
)
}
# Data
data_tbl <- tibble::as_tibble(.data)
atb <- attributes(data_tbl)
stat_mean <- "undefined"
stat_median <- atb$.location
stat_mode <- atb$.location
stat_sd <- "undefined"
stat_coef_var <- "undefined"
stat_skewness <- 0
stat_kurtosis <- "undefined"
# Data Tibble
ret <- tibble::tibble(
tidy_function = atb$tibble_type,
function_call = atb$dist_with_params,
distribution = atb$tibble_type %>%
stringr::str_remove("tidy_") %>%
stringr::str_to_title(),
distribution_type = atb$distribution_family_type,
points = atb$.n,
simulations = atb$.num_sims,
mean = stat_mean,
median = stat_median,
mode = stat_mode,
std_dv = stat_sd,
coeff_var = stat_coef_var,
skewness = stat_skewness,
kurtosis = stat_kurtosis,
computed_std_skew = tidy_skewness_vec(data_tbl$y),
computed_std_kurt = tidy_kurtosis_vec(data_tbl$y)
)
# Return
return(ret)
}
Examples:
> tidy_cauchy() %>%
+ util_cauchy_stats_tbl() %>%
+ glimpse()
Rows: 1
Columns: 15
$ tidy_function <chr> "tidy_cauchy"
$ function_call <chr> "Cauchy c(0, 1)"
$ distribution <chr> "Cauchy"
$ distribution_type <chr> "continuous"
$ points <dbl> 50
$ simulations <dbl> 1
$ mean <chr> "undefined"
$ median <dbl> 0
$ mode <dbl> 0
$ std_dv <chr> "undefined"
$ coeff_var <chr> "undefined"
$ skewness <dbl> 0
$ kurtosis <chr> "undefined"
$ computed_std_skew <dbl> 5.064171
$ computed_std_kurt <dbl> 30.08631
> util_cauchy_stats_tbl(rcauchy(50))
Error in `util_cauchy_stats_tbl()`:
! You must pass data from the 'tidy_dist' function.
Run `rlang::last_error()` to see where the error occurred.
Called from: signal_abort(cnd, .file)
Browse[1]>
> util_cauchy_stats_tbl(tidy_uniform())
Error in `util_cauchy_stats_tbl()`:
! You must use 'tidy_cauchy()'
Run `rlang::last_error()` to see where the error occurred.
Called from: signal_abort(cnd, .file)
Browse[1]> Q
https://www.itl.nist.gov/div898/handbook/eda/section3/eda3663.htm