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

Fix `tidy_uniform` internal param_grid #210

Closed spsanderson closed 2 years ago

spsanderson commented 2 years ago

Current:

param_grid <- dplyr::tibble(.max, .min)

Future:

param_grid <- dplyr::tibble(.min, .max)
spsanderson commented 2 years ago

Function:

#' Tidy Randomly Generated Uniform Distribution Tibble
#'
#' @family Continuous Distribution
#' @family Uniform
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @seealso \url{https://www.itl.nist.gov/div898/handbook/eda/section3/eda3662.htm}
#'
#' @details This function uses the underlying `stats::runif()`, and its underlying
#' `p`, `d`, and `q` functions. For more information please see [stats::runif()]
#'
#' @description This function will generate `n` random points from a uniform
#' distribution with a user provided, `.min` and `.max` values, and number of
#' random simulations to be produced. The function returns a tibble with the
#' simulation number column the x column which corresponds to the n randomly
#' generated points, the `d_`, `p_` and `q_` data points as well.
#'
#' The data is returned un-grouped.
#'
#' The columns that are output are:
#'
#' -  `sim_number` The current simulation number.
#' -  `x` The current value of `n` for the current simulation.
#' -  `y` The randomly generated data point.
#' -  `dx` The `x` value from the [stats::density()] function.
#' -  `dy` The `y` value from the [stats::density()] function.
#' -  `p` The values from the resulting p_ function of the distribution family.
#' -  `q` The values from the resulting q_ function of the distribution family.
#'
#' @param .n The number of randomly generated points you want.
#' @param .min A lower limit of the distribution.
#' @param .max An upper limit of the distribution
#' @param .num_sims The number of randomly generated simulations you want.
#'
#' @examples
#' tidy_uniform()
#' @return
#' A tibble of randomly generated data.
#'
#' @export
#'

tidy_uniform <- function(.n = 50, .min = 0, .max = 1, .num_sims = 1) {

  # Tidyeval ----
  n <- as.integer(.n)
  num_sims <- as.integer(.num_sims)
  max_val <- as.numeric(.max)
  min_val <- as.numeric(.min)

  # Checks ----
  if (!is.integer(n) | n < 0) {
    rlang::abort(
      "The parameters '.n' must be of class integer. Please pass a whole
            number like 50 or 100. It must be greater than 0."
    )
  }

  if (!is.integer(num_sims) | num_sims < 0) {
    rlang::abort(
      "The parameter `.num_sims' must be of class integer. Please pass a
            whole number like 50 or 100. It must be greater than 0."
    )
  }

  if (!is.numeric(min_val) | !is.numeric(max_val) | min_val > max_val) {
    rlang::abort(
      "The parameters of .min and .max should be numeric and max must be
            greater than or equal to min."
    )
  }

  x <- seq(1, num_sims, 1)

  # ps <- seq(-n, n - 1, 2)
  qs <- seq(0, 1, (1 / (n - 1)))
  ps <- qs

  df <- dplyr::tibble(sim_number = as.factor(x)) %>%
    dplyr::group_by(sim_number) %>%
    dplyr::mutate(x = list(1:n)) %>%
    dplyr::mutate(y = list(stats::runif(n = n, min = min_val, max = max_val))) %>%
    dplyr::mutate(d = list(density(unlist(y), n = n)[c("x", "y")] %>%
                             purrr::set_names("dx", "dy") %>%
                             dplyr::as_tibble())) %>%
    dplyr::mutate(p = list(stats::punif(ps, min = min_val, max = max_val))) %>%
    dplyr::mutate(q = list(stats::qunif(tidy_scale_zero_one_vec(unlist(y)), min = min_val, max = max_val))) %>%
    tidyr::unnest(cols = c(x, y, d, p, q)) %>%
    dplyr::ungroup()

  param_grid <- dplyr::tibble(.min, .max)

  # Attach descriptive attributes to tibble
  attr(df, "distribution_family_type") <- "continuous"
  attr(df, ".min") <- .min
  attr(df, ".max") <- .max
  attr(df, ".n") <- .n
  attr(df, ".num_sims") <- .num_sims
  attr(df, "tibble_type") <- "tidy_uniform"
  attr(df, "ps") <- ps
  attr(df, "qs") <- qs
  attr(df, "param_grid") <- param_grid
  attr(df, "param_grid_txt") <- paste0(
    "c(",
    paste(param_grid[, names(param_grid)], collapse = ", "),
    ")"
  )
  attr(df, "dist_with_params") <- paste0(
    "Uniform",
    " ",
    paste0(
      "c(",
      paste(param_grid[, names(param_grid)], collapse = ", "),
      ")"
    )
  )

  # Return final result as function output
  return(df)
}