Skip to content

Commit

Permalink
Merge pull request #93 from spsanderson/development
Browse files Browse the repository at this point in the history
Fix #91
  • Loading branch information
spsanderson committed Mar 16, 2022
2 parents 6075f1c + dde95d5 commit 5d44819
Show file tree
Hide file tree
Showing 82 changed files with 509 additions and 497 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# TidyDensity (development version)

## Breaking Changes
1. Fix #91 - Bug fix, change `tidy_gamma()` parameter of `.rate` to `.scale
Fix `tidy_autoplot_` functions to incorprate this change. Fix `util_gamma_param_estimate()`
to say `scale` instead of `rate` in the returned estimated parameters.

# TidyDensity 1.0.0

## Breaking Changes
Expand Down
2 changes: 1 addition & 1 deletion R/autoplot-density.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ tidy_autoplot <- function(.data, .plot_type = "density", .line_size = .5,
"Parameters: ", if (atb$tibble_type == "tidy_gaussian") {
paste0("Mean: ", atb$.mean, " - SD: ", atb$.sd)
} else if (atb$tibble_type == "tidy_gamma") {
paste0("Shape: ", atb$.shape, " - Rate: ", atb$.rate)
paste0("Shape: ", atb$.shape, " - Scale: ", atb$.scale)
} else if (atb$tibble_type == "tidy_beta") {
paste0("Shape1: ", atb$.shape1, " - Shape2: ", atb$.shape2, " - NCP: ", atb$.ncp)
} else if (atb$tibble_type %in% c("tidy_poisson", "tidy_zero_truncated_poisson")) {
Expand Down
2 changes: 1 addition & 1 deletion R/autoplot-multi-dist.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ tidy_multi_dist_autoplot <- function(.data, .plot_type = "density", .line_size =
"Parameters: ", if (atb$all$tibble_type == "tidy_gaussian") {
paste0("Mean: ", toString(atb$.param_list$.mean), " - SD: ", toString(atb$.param_list$.sd))
} else if (atb$all$tibble_type == "tidy_gamma") {
paste0("Shape: ", toString(atb$.param_list$.shape), " - Rate: ", toString(atb$.param_list$.rate))
paste0("Shape: ", toString(atb$.param_list$.shape), " - Scale: ", toString(atb$.param_list$.scale))
} else if (atb$all$tibble_type == "tidy_beta") {
paste0("Shape1: ", toString(atb$.param_list$.shape1), " - Shape2: ", toString(atb$.param_list$.shape2), " - NCP: ", toString(atb$.param_list$.ncp))
} else if (atb$all$tibble_type %in% c("tidy_poisson", "tidy_zero_truncated_poisson")) {
Expand Down
18 changes: 9 additions & 9 deletions R/est-param-gamma.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#'
#' @details This function will see if the given vector `.x` is a numeric vector.
#'
#' @description This function will attempt to estimate the gamma shape and rate
#' @description This function will attempt to estimate the gamma shape and scale
#' parameters given some vector of values. The function will return a list output by default, and if the parameter
#' `.auto_gen_empirical` is set to `TRUE` then the empirical data given to the
#' parameter `.x` will be run through the `tidy_empirical()` function and combined
Expand All @@ -23,7 +23,7 @@
#' library(dplyr)
#' library(ggplot2)
#'
#' tg <- tidy_gamma(.shape = 1, .rate = .5) %>% pull(y)
#' tg <- tidy_gamma(.shape = 1, .scale = .3) %>% pull(y)
#' output <- util_gamma_param_estimate(tg)
#'
#' output$parameter_tbl
Expand Down Expand Up @@ -76,19 +76,19 @@ util_gamma_param_estimate <- function(.x, .auto_gen_empirical = TRUE){
# Parameters ----
# NIST
nist_shape <- (m/s)^2
nist_rate <- (s^2)/m
nist_scale <- (s^2)/m

# EnvStats
es_mmu_shape <- (m/(sqrt(n/(n - 1)) * s))^2
es_mmu_rate <- m/nist_shape
es_mmu_scale <- m/nist_shape

es_bcmle_shape <- ((n - 3)/n) * nist_shape + (2/(3 * n))
es_bcmle_rate <- m/nist_shape
es_bcmle_scale <- m/nist_shape

# Return Tibble ----
if (.auto_gen_empirical){
te <- tidy_empirical(.x = x_term)
td <- tidy_gamma(.n = n, .shape = round(nist_shape, 3), .rate = round(nist_rate, 3))
td <- tidy_gamma(.n = n, .shape = round(nist_shape, 3), .scale = round(nist_scale, 3))
combined_tbl <- tidy_combine_distributions(te, td)
}

Expand All @@ -101,9 +101,9 @@ util_gamma_param_estimate <- function(.x, .auto_gen_empirical = TRUE){
variance = rep(s, 3),
method = c("NIST_MME", "EnvStats_MMUE", "EnvStats_BCMLE"),
shape = c(nist_shape, es_mmu_shape, es_bcmle_shape),
rate = c(nist_rate, es_mmu_rate, es_bcmle_rate),
shape_ratio = c(nist_shape/nist_rate, es_mmu_shape/es_mmu_rate,
es_bcmle_shape/es_bcmle_rate)
scale = c(nist_scale, es_mmu_scale, es_bcmle_scale),
shape_ratio = c(nist_shape/nist_scale, es_mmu_shape/es_mmu_scale,
es_bcmle_shape/es_bcmle_scale)
)

# Return ----
Expand Down
133 changes: 66 additions & 67 deletions R/random-tidy-gamma.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,87 +43,86 @@
#' @export
#'

tidy_gamma <- function(.n = 50, .shape = 1, .rate = 1, .num_sims = 1) {
tidy_gamma <- function(.n = 50, .shape = 1, .scale = 0.3, .num_sims = 1) {

# Tidyeval ----
n <- as.integer(.n)
num_sims <- as.integer(.num_sims)
shp <- .shape
rte <- .rate
# Tidyeval ----
n <- as.integer(.n)
num_sims <- as.integer(.num_sims)
shp <- .shape
scle <- .scale

# Checks ----
if (!is.integer(n) | n < 0) {
rlang::abort(
"The parameters '.n' must be of class integer. Please pass a whole
# 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
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(shp) | shp < 0) {
rlang::abort(
"The parameters of '.shape' and '.rate' must be of class numeric.
if (!is.numeric(shp) | shp < 0) {
rlang::abort(
"The parameters of '.shape' and '.scale' must be of class numeric.
Please pass a numer like 1 or 1.1 etc. and must be greater than 0."
)
}
)
}

if (!is.numeric(rte) | rte < 0) {
rlang::abort(
"The parameters of '.shape' and '.rate' must be of class numeric.
if (!is.numeric(scle) | scle < 0) {
rlang::abort(
"The parameters of '.shape' and '.scale' must be of class numeric.
Please pass a numer like 1 or 1.1 etc."
)
}
)
}

x <- seq(1, num_sims, 1)
x <- seq(1, num_sims, 1)

# ps <- seq(-n, n - 1, 2)
qs <- seq(0, 1, (1 / (n - 1)))
ps <- qs
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::rgamma(n = n, shape = shp, rate = rte))) %>%
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::pgamma(ps, shape = shp, rate = rte))) %>%
dplyr::mutate(q = list(stats::qgamma(qs, shape = shp, rate = rte))) %>%
tidyr::unnest(cols = c(x, y, d, p, q)) %>%
dplyr::ungroup()
df <- dplyr::tibble(sim_number = as.factor(x)) %>%
dplyr::group_by(sim_number) %>%
dplyr::mutate(x = list(1:n)) %>%
dplyr::mutate(y = list(stats::rgamma(n = n, shape = shp, scale = scle))) %>%
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::pgamma(ps, shape = shp, scale = scle))) %>%
dplyr::mutate(q = list(stats::qgamma(qs, shape = shp, scale = scle))) %>%
tidyr::unnest(cols = c(x, y, d, p, q)) %>%
dplyr::ungroup()

param_grid <- dplyr::tibble(.shape, .rate)
param_grid <- dplyr::tibble(.shape, .scale)

# Attach descriptive attributes to tibble
attr(df, ".shape") <- .shape
attr(df, ".rate") <- .rate
attr(df, ".n") <- .n
attr(df, ".num_sims") <- .num_sims
attr(df, "tibble_type") <- "tidy_gamma"
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(
"Gamma",
" ",
paste0(
"c(",
paste(param_grid[, names(param_grid)], collapse = ", "),
")"
# Attach descriptive attributes to tibble
attr(df, ".shape") <- .shape
attr(df, ".scale") <- .scale
attr(df, ".n") <- .n
attr(df, ".num_sims") <- .num_sims
attr(df, "tibble_type") <- "tidy_gamma"
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(
"Gamma",
" ",
paste0(
"c(",
paste(param_grid[, names(param_grid)], collapse = ", "),
")"
)
)
)

# Return final result as function output
return(df)
# Return final result as function output
return(df)
}
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.
Binary file modified docs/articles/getting-started_files/figure-html/plot_density-1.png
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.
7 changes: 7 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,7 +3,7 @@ pkgdown: 2.0.2
pkgdown_sha: ~
articles:
getting-started: getting-started.html
last_built: 2022-03-08T21:44Z
last_built: 2022-03-16T18:55Z
urls:
reference: https://github.com/spsanderson/TidyDensity/reference
article: https://github.com/spsanderson/TidyDensity/articles
Expand Down
Binary file modified docs/reference/Rplot001.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/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 5d44819

Please sign in to comment.