spsanderson / TidyDensity

Create tidy probability/density tibbles and plots of randomly generated and empirical data.
https://www.spsanderson.com/TidyDensity
Other
34 stars 1 forks source link

logistic #148

Closed spsanderson closed 2 years ago

spsanderson commented 2 years ago

https://openacttexts.github.io/Loss-Data-Analytics/C-SummaryDistributions.html

spsanderson commented 2 years ago

Function:

#' Distribution Statistics
#' 
#' @family Logistic
#' @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_logistic() %>%
#'   util_logistic_stats_tbl()
#' 
#' @return 
#' A tibble
#' 
#' @export
#' 

util_logistic_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_logistic"){
    rlang::abort(
      message = "You must use 'tidy_logistic()'",
      use_cli_format = TRUE
    )
  }

  # Data
  data_tbl <- tibble::as_tibble(.data)

  atb <- attributes(data_tbl)
  mu <- atb$.location
  s <- atb$.scale

  stat_mean   <- mu
  stat_mode   <- mu
  stat_coef_var <- ((s^2) * (pi^2)) / 3
  stat_sd <- abs(sqrt(stat_coef_var))
  stat_skewness <- 0
  stat_kurtosis <- 6/5

  # 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,
    mode_lower = stat_mode,
    range = paste0("0 to Inf"),
    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_logistic() %>%
  util_logistic_stats_tbl() %>%
  glimpse()

Rows: 1
Columns: 15
$ tidy_function     <chr> "tidy_logistic"
$ function_call     <chr> "Logistic c(0, 1)"
$ distribution      <chr> "Logistic"
$ distribution_type <chr> "continuous"
$ points            <dbl> 50
$ simulations       <dbl> 1
$ mean              <dbl> 0
$ mode_lower        <dbl> 0
$ range             <chr> "0 to Inf"
$ std_dv            <dbl> 1.813799
$ coeff_var         <dbl> 3.289868
$ skewness          <dbl> 0
$ kurtosis          <dbl> 1.2
$ computed_std_skew <dbl> 0.619276
$ computed_std_kurt <dbl> 6.344419