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

Enhance tidy_combine_distributions() to suppress NULL values from list input #213

Closed
spsanderson opened this issue Jun 13, 2022 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@spsanderson
Copy link
Owner

This is a requirement in order to help #212 become more robust.

If a tidy_ distribution function is passed to tidy_combine_distirbutions() and the output is NULL then this will cause a failure in building the final output.

Can use purrr::compact() to drop NULLS from input list.

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

Function:

#' Combine Multiple Tidy Distributions of Different Types
#'
#' @family Multiple Distribution
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @details Allows a user to generate a tibble of different `tidy_` distributions
#'
#' @description This allows a user to specify any `n` number of `tidy_`
#' distributions that can be combined into a single tibble. This is the preferred
#' method for combining multiple distributions of different types, for example
#' a Gaussian distribution and a Beta distribution.
#'
#' This generates a single tibble with an added column of dist_type that will
#' give the distribution family name and its associated parameters.
#'
#' @param ... The `...` is where you can place your different distributions
#'
#' @examples
#'
#' tn <- tidy_normal()
#' tb <- tidy_beta()
#' tc <- tidy_cauchy()
#'
#' tidy_combine_distributions(tn, tb, tc)
#'
#' ## OR
#'
#' tidy_combine_distributions(
#'   tidy_normal(),
#'   tidy_beta(),
#'   tidy_cauchy(),
#'   tidy_logistic()
#' )
#'
#' @return
#' A tibble
#'
#' @export
#'

tidy_combine_distributions <- function(...){
  
  # Add distributions to a list
  dist_list <- list(...)
  
  dist_list <- purrr::compact(dist_list)
  
  # Checks ----
  if (length(dist_list) < 2){
    rlang::abort(
      message = "You must add at least two distributions to the function",
      use_cli_format = TRUE
    )
  }
  
  # Get the distribution type
  dist_final_tbl <- purrr::map(
    .x = dist_list,
    .f = ~ .x %>%
      dplyr::mutate(dist_type = attributes(.x)[["dist_with_params"]]) %>%
      dplyr::mutate(dist_type = as.factor(dist_type))
  ) %>%
    purrr::map_dfr(dplyr::as_tibble)
  
  attr(dist_final_tbl, "tibble_type") <- "tidy_multi_dist_combine"
  
  # Return ---
  return(dist_final_tbl)
  
}

Examples:

> tidy_combine_distributions(tidy_normal(), tidy_exponential(), NULL) %>% tidy_distribution_summary_tbl(dist_type)
# A tibble: 2 × 11
  dist_type    mean_val median_val std_val  min_val max_val skewness kurtosis range
  <fct>           <dbl>      <dbl>   <dbl>    <dbl>   <dbl>    <dbl>    <dbl> <dbl>
1 Gaussian c(…   0.0278     -0.230    1.05 -2.50e+0    3.67    0.820     4.74  6.17
2 Exponential1.01        0.743    1.00  9.69e-4    4.52    1.52      5.12  4.52
# … with 2 more variables: iqr <dbl>, variance <dbl>

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