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

geometric #150

Closed
Tracked by #124
spsanderson opened this issue Mar 31, 2022 · 1 comment
Closed
Tracked by #124

geometric #150

spsanderson opened this issue Mar 31, 2022 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@spsanderson
Copy link
Owner

spsanderson commented Mar 31, 2022

@spsanderson spsanderson added this to the TidyDensity v1.0.2 milestone Mar 31, 2022
@spsanderson spsanderson added the enhancement New feature or request label Mar 31, 2022
@spsanderson spsanderson self-assigned this Mar 31, 2022
@spsanderson
Copy link
Owner Author

Function:

#' Distribution Statistics
#' 
#' @family Geometric
#' @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_geometric() %>%
#'   util_geometric_stats_tbl()
#' 
#' @return 
#' A tibble
#' 
#' @export
#' 

util_geometric_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_geometric"){
    rlang::abort(
      message = "You must use 'tidy_geometric()'",
      use_cli_format = TRUE
    )
  }
  
  # Data
  data_tbl <- tibble::as_tibble(.data)
  
  atb <- attributes(data_tbl)
  p <- atb$.prob
  
  stat_mean   <- (1 - p)/p
  stat_mode   <- data_tbl %>% 
    dplyr::filter(p == max(p)) %>% 
    dplyr::pull(y) %>% 
    max()
  stat_sd <- sqrt((1 - p)/p)
  stat_skewness <- (2 - p)/sqrt(1 - p)
  stat_kurtosis <- 6 + ((p*p)/(1 - p))
  stat_coef_var <- (1-p)/(p * p)
  
  # 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 = 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_geometric(.prob = .1) %>%
  util_geometric_stats_tbl() %>%
  glimpse()

Rows: 1
Columns: 15
$ tidy_function     <chr> "tidy_geometric"
$ function_call     <chr> "Geometric c(0.1)"
$ distribution      <chr> "Geometric"
$ distribution_type <chr> "continuous"
$ points            <dbl> 50
$ simulations       <dbl> 1
$ mean              <dbl> 9
$ mode              <int> 0
$ range             <chr> "0 to Inf"
$ std_dv            <dbl> 3
$ coeff_var         <dbl> 90
$ skewness          <dbl> 2.002776
$ kurtosis          <dbl> 6.011111
$ computed_std_skew <dbl> 3.250976
$ computed_std_kurt <dbl> 16.82151

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