Skip to content

Commit

Permalink
Fix #32
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed Jan 25, 2022
1 parent f590243 commit d5fc53e
Show file tree
Hide file tree
Showing 33 changed files with 218 additions and 29 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(tidy_empirical)
export(tidy_exponential)
export(tidy_f)
export(tidy_gamma)
export(tidy_geometric)
export(tidy_hypergeometric)
export(tidy_logistic)
export(tidy_lognormal)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

## New Features
1. Fix #27 - Add function `tidy_binomial()`
2. Fix #32 - Add function `tidy_geometric()`

## Fixes and Minor Improvements
1. Fix #30 - Move `crayong()` and `cli` from Suggests to Imports due to `pillar`
1. Fix #30 - Move `crayon`, `rstudioapi`, and `cli` from Suggests to Imports due to `pillar`
no longer importing.

# TidyDensity 0.0.1
Expand Down
4 changes: 3 additions & 1 deletion R/autoplot-density.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ tidy_autoplot <- function(.data, .plot_type = "density", .line_size = .5, .point
"tidy_gaussian", "tidy_poisson","tidy_gamma","tidy_beta","tidy_f",
"tidy_hypergeometric","tidy_lognormal","tidy_cauchy","tidy_chisquare",
"tidy_weibull","tidy_uniform","tidy_logistic","tidy_exponential",
"tidy_empirical","tidy_binomial"
"tidy_empirical","tidy_binomial","tidy_geometric"
)){
rlang::abort("The data passed must come from a `tidy_` distribution function.")
}
Expand Down Expand Up @@ -126,6 +126,8 @@ tidy_autoplot <- function(.data, .plot_type = "density", .line_size = .5, .point
paste0("Empirical - No params")
} else if(atb$tibble_type == "tidy_binomial"){
paste0("Size: ", atb$.size, " - Prob: ", atb$.prob)
} else if(atb$tibble_type == "tidy_geometric"){
paste0("Prob: ", atb$.prob)
}
)

Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-beta.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Beta Tibble
#' Tidy Randomly Generated Beta Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-binomial.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Binomial Tibble
#' Tidy Randomly Generated Binomial Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-cauchy.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Cauchy Tibble
#' Tidy Randomly Generated Cauchy Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-chisquare.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Chisquare (Non-Central) Tibble
#' Tidy Randomly Generated Chisquare (Non-Central) Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-exponential.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Exponential Tibble
#' Tidy Randomly Generated Exponential Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-gamma.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Gamma Tibble
#' Tidy Randomly Generated Gamma Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
101 changes: 101 additions & 0 deletions R/random-tidy-geom.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#' Tidy Randomly Generated Geometric Distribution Tibble
#'
#' @family Data Generator
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @seealso \url{https://www.itl.nist.gov/div898/handbook/eda/section3/eda3667.htm}
#'
#' @details This function uses the underlying `stats::rexp()`, and its underlying
#' `p`, `d`, and `q` functions. For more information please see [stats::rexp()]
#'
#' @description This function will generate `n` random points from a exponential
#' distribution with a user provided, `.prob`, 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 .prob A probability of success in each trial 0 < prob <= 1.
#' @param .num_sims The number of randomly generated simulations you want.
#'
#' @examples
#' tidy_geometric()
#'
#' @return
#' A tibble of randomly generated data.
#'
#' @export
#'

tidy_geometric <- function(.n = 50, .prob = 1, .num_sims = 1){

# Tidyeval ----
n <- as.integer(.n)
num_sims <- as.integer(.num_sims)
prob <- as.numeric(.prob)

# 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(prob) | prob < 0 | prob > 1){
rlang::abort(
"The parameter of rate must be of class numeric and 0 < .prob <= 1 must be
satisfied."
)
}

x <- seq(1, num_sims, 1)

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

df <- dplyr::tibble(sim_number = as.factor(x)) %>%
dplyr::group_by(sim_number) %>%
dplyr::mutate(x = list(1:n)) %>%
dplyr::mutate(y = list(stats::rgeom(n = n, prob = prob))) %>%
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::pgeom(ps, prob = prob))) %>%
dplyr::mutate(q = list(stats::qgeom(qs, prob = prob))) %>%
tidyr::unnest(cols = c(x, y, d, p, q)) %>%
dplyr::ungroup()


# Attach descriptive attributes to tibble
attr(df, ".prob") <- .prob
attr(df, ".n") <- .n
attr(df, ".num_sims") <- .num_sims
attr(df, "tibble_type") <- "tidy_geometric"
attr(df, "ps") <- ps
attr(df, "qs") <- qs

# Return final result as function output
return(df)

}
2 changes: 1 addition & 1 deletion R/random-tidy-hypergeometric.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Hypergeometric Tibble
#' Tidy Randomly Generated Hypergeometric Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-logistic.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Logistic Tibble
#' Tidy Randomly Generated Logistic Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-lognormal.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Lognormal Tibble
#' Tidy Randomly Generated Lognormal Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-normal.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Gaussian Tibble
#' Tidy Randomly Generated Gaussian Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-poisson.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Poisson Tibble
#' Tidy Randomly Generated Poisson Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-uniform.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Uniform Tibble
#' Tidy Randomly Generated Uniform Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
2 changes: 1 addition & 1 deletion R/random-tidy-weibull.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Tidy Randomly Generated Weibull Tibble
#' Tidy Randomly Generated Weibull Distribution Tibble
#'
#' @family Data Generator
#'
Expand Down
3 changes: 2 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"\nIf you encounter a bug or want to request an enhancement please file an issue at:",
"\n https://github.com/spsanderson/TidyDensity/issues",
"\n",
"\nThank you for using TidyDensity"
"\nThank you for using TidyDensity!",
"\n"
)

packageStartupMessage(msg)
Expand Down
3 changes: 2 additions & 1 deletion man/tidy_beta.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/tidy_binomial.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/tidy_cauchy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/tidy_chisquare.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/tidy_exponential.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/tidy_f.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/tidy_gamma.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d5fc53e

Please sign in to comment.