Skip to content

Commit

Permalink
Fix #38
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed Feb 8, 2022
1 parent d5dd66d commit 1cd6e45
Show file tree
Hide file tree
Showing 63 changed files with 667 additions and 295 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export(tidy_gamma)
export(tidy_generalized_pareto)
export(tidy_geometric)
export(tidy_hypergeometric)
export(tidy_inverse_exponential)
export(tidy_inverse_pareto)
export(tidy_logistic)
export(tidy_lognormal)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ None
12. Fix #60 - Add function `tidy_random_walk_autoplot()`
13. Fix #47 - Add function `tidy_generalized_pareto()`
14. Fix #44 - Add function `tidy_paralogistic()`
15. Fix #38 - Add function `tidy_inverse_exponential()`

## Fixes and Minor Improvements
1. Fix #30 - Move `crayon`, `rstudioapi`, and `cli` from Suggests to Imports due to `pillar`
Expand Down
4 changes: 3 additions & 1 deletion R/autoplot-density.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ tidy_autoplot <- function(.data, .plot_type = "density", .line_size = .5,
"tidy_zero_truncated_poisson", "tidy_zero_truncated_geometric",
"tidy_zero_truncated_binomial", "tidy_zero_truncated_negative_binomial",
"tidy_pareto_single_parameter", "tidy_pareto", "tidy_inverse_pareto",
"tidy_generalized_pareto","tidy_paralogistic"
"tidy_generalized_pareto","tidy_paralogistic", "tidy_inverse_exponential"
)) {
rlang::abort("The data passed must come from a `tidy_` distribution function.")
}
Expand Down Expand Up @@ -164,6 +164,8 @@ tidy_autoplot <- function(.data, .plot_type = "density", .line_size = .5,
paste0("Shape: ", atb$.shape, " - ",
"Rate: ", atb$.rate, " - ",
"Scale: ", atb$.scale)
} else if (atb$tibble_type == "tidy_inverse_exponential"){
paste0("Rate: ", atb$.rate, " - Scale: ", atb$.scale)
}
)

Expand Down
109 changes: 109 additions & 0 deletions R/random-tidy-exponential-inverse.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#' Tidy Randomly Generated Inverse Exponential Distribution Tibble
#'
#' @family Continuous Distribution
#' @family Exponential
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @seealso \url{https://openacttexts.github.io/Loss-Data-Analytics/C-SummaryDistributions.html}
#'
#' @details This function uses the underlying `actuar::rinvexp()`, and its underlying
#' `p`, `d`, and `q` functions. For more information please see [actuar::rinvexp()]
#'
#' @description This function will generate `n` random points from an inverse exponential
#' distribution with a user provided, `.rate` or `.scale` 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 .scale Must be strictly positive.
#' @param .rate An alternative way to specify the `.scale`
#' @param .num_sims The number of randomly generated simulations you want.
#'
#' @examples
#' tidy_inverse_exponential()
#'
#' @return
#' A tibble of randomly generated data.
#'
#' @export
#'

tidy_inverse_exponential <- function(.n = 50, .rate = 1, .scale = 1/.rate, .num_sims = 1) {

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

# 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(rate) | !is.numeric(scale)){
rlang::abort(
"The parameters of rate and scale must be of calss numeric."
)
}

if (rate <= 0 | scale <= 0){
rlang::abort(
"The parameters of rate and scale must be strictly positive."
)
}

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(actuar::rinvexp(n = n, rate = rate, scale = scale))) %>%
dplyr::mutate(d = list(density(unlist(y), n = n)[c("x", "y")] %>%
purrr::set_names("dx", "dy") %>%
dplyr::as_tibble())) %>%
dplyr::mutate(p = list(actuar::pinvexp(ps, rate = rate, scale = scale))) %>%
dplyr::mutate(q = list(actuar::qinvexp(qs, rate = rate, scale = scale))) %>%
tidyr::unnest(cols = c(x, y, d, p, q)) %>%
dplyr::ungroup()


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

# Return final result as function output
return(df)
}
3 changes: 2 additions & 1 deletion R/random-tidy-exponential.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Tidy Randomly Generated Exponential Distribution Tibble
#'
#' @family Data Generator
#' @family Continuous Distribution
#' @family Exponential
#'
#' @author Steven P. Sanderson II, MPH
#'
Expand Down
24 changes: 12 additions & 12 deletions docs/articles/getting-started.html

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

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/news/index.html

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

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pkgdown: 2.0.2
pkgdown_sha: ~
articles:
getting-started: getting-started.html
last_built: 2022-02-07T20:06Z
last_built: 2022-02-08T18:02Z

Binary file modified docs/reference/Rplot002.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/reference/tidy_autoplot-1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/reference/tidy_autoplot-2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1cd6e45

Please sign in to comment.