Skip to content

Commit

Permalink
Merge pull request #342 from poissonconsulting/prop
Browse files Browse the repository at this point in the history
- Soft-deprecated argument percent = 5 for proportion = 0.05
  • Loading branch information
joethorley committed Jan 14, 2024
2 parents b07fd04 + 9d47a9b commit 9b29e7e
Show file tree
Hide file tree
Showing 111 changed files with 1,501 additions and 1,419 deletions.
2 changes: 1 addition & 1 deletion R/bcanz.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ssd_fit_bcanz <- function(data, left = "Conc") {
#' ssd_hc_bcanz(fits, nboot = 100)
ssd_hc_bcanz <- function(x, nboot = 10000, delta = 10, min_pboot = 0.9) {
ssd_hc(x,
percent = c(1, 5, 10, 20),
proportion = c(0.01, 0.05, 0.1, 0.2),
ci = TRUE,
level = 0.95,
nboot = nboot,
Expand Down
2 changes: 1 addition & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' A data frame of the predictions based on 1,000 bootstrap iterations.
#'
#' \describe{
#' \item{percent}{The percent of species affected (int).}
#' \item{proportion}{The proportion of species affected (int).}
#' \item{est}{The estimated concentration (dbl).}
#' \item{se}{The standard error of the estimate (dbl).}
#' \item{lcl}{The lower confidence limit (dbl).}
Expand Down
2 changes: 1 addition & 1 deletion R/exposure.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

#' Percent Exposure
#' Proportion Exposure
#'
#' Calculates average proportion exposed based on log-normal distribution of concentrations.
#'
Expand Down
2 changes: 1 addition & 1 deletion R/ggplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ geom_hcintersect <- function(mapping = NULL,
#' @export
#' @examples
#' gp <- ggplot2::ggplot(boron_pred) +
#' geom_xribbon(ggplot2::aes(xmin = lcl, xmax = ucl, y = percent))
#' geom_xribbon(ggplot2::aes(xmin = lcl, xmax = ucl, y = proportion))
geom_xribbon <- function(mapping = NULL,
data = NULL,
stat = "identity",
Expand Down
16 changes: 14 additions & 2 deletions R/hc-burrlioz.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,25 @@
#' ssd_hc_burrlioz(fit)
#'
#' @export
ssd_hc_burrlioz <- function(x, percent = 5, ci = FALSE, level = 0.95, nboot = 1000,
ssd_hc_burrlioz <- function(x, percent, proportion = 0.05, ci = FALSE, level = 0.95, nboot = 1000,
min_pboot = 0.99, parametric = FALSE) {
lifecycle::deprecate_warn("0.3.5", "ssd_hc_burrlioz()", "ssd_hc()")
chk_s3_class(x, "fitburrlioz")

if(lifecycle::is_present(percent)) {
lifecycle::deprecate_soft("1.0.6.9009", "ssd_hc(percent)", "ssd_hc(proportion)", id = "hc")
chk_vector(percent)
chk_numeric(percent)
chk_range(percent, c(0, 100))
proportion <- percent / 100
}

chk_vector(proportion)
chk_numeric(proportion)
chk_range(proportion)

ssd_hc(x,
percent = percent, ci = ci, level = level,
proportion = proportion, ci = ci, level = level,
nboot = nboot, min_pboot = min_pboot, parametric = parametric
)
}
79 changes: 54 additions & 25 deletions R/hc.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' Hazard Concentrations for Species Sensitivity Distributions
#'
#' Calculates concentration(s) with bootstrap confidence intervals
#' that protect specified percentage(s) of species for
#' that protect specified proportion(s) of species for
#' individual or model-averaged distributions
#' using parametric or non-parametric bootstrapping.
#'
Expand All @@ -37,9 +37,8 @@
#' calculating the weighted arithmetic means of the lower
#' and upper confidence limits based on `nboot` samples for each distribution.
#'
#' Based on Burnham and Anderson (2002),
#' distributions with an absolute AIC difference greater
#' than a delta of by default 7 have considerably less support (weight < 0.03)
#' Distributions with an absolute AIC difference greater
#' than a delta of by default 7 have considerably less support (weight < 0.01)
#' and are excluded
#' prior to calculation of the hazard concentrations to reduce the run time.
#'
Expand All @@ -62,7 +61,7 @@ ssd_hc <- function(x, ...) {
est <- do.call(fun, args)
tibble(
dist = dist,
percent = proportion * 100, est = est,
proportion = proportion, est = est,
se = NA_real_, lcl = NA_real_, ucl = NA_real_,
wt = 1,
nboot = 0L, pboot = NA_real_
Expand All @@ -74,19 +73,35 @@ ssd_hc <- function(x, ...) {
#' @examples
#'
#' ssd_hc(ssd_match_moments())
ssd_hc.list <- function(x, percent = 5, ...) {
ssd_hc.list <- function(
x,
percent,
proportion = 0.05,
...) {
chk_list(x)
chk_named(x)
chk_unique(names(x))
chk_unused(...)

if(lifecycle::is_present(percent)) {
lifecycle::deprecate_soft("1.0.6.9009", "ssd_hc(percent)", "ssd_hc(proportion)", id = "hc")
chk_vector(percent)
chk_numeric(percent)
chk_range(percent, c(0, 100))
proportion <- percent / 100
}

chk_vector(proportion)
chk_numeric(proportion)
chk_range(proportion)

if (!length(x)) {
hc <- no_hcp()
hc <- dplyr::rename(hc, percent = "value")
hc <- dplyr::rename(hc, proportion = "value")
return(hc)
}
hc <- mapply(.ssd_hc_dist, x, names(x),
MoreArgs = list(proportion = percent / 100),
MoreArgs = list(proportion = proportion),
SIMPLIFY = FALSE
)
bind_rows(hc)
Expand All @@ -100,7 +115,8 @@ ssd_hc.list <- function(x, percent = 5, ...) {
#' ssd_hc(fits)
ssd_hc.fitdists <- function(
x,
percent = 5,
percent,
proportion = 0.05,
average = TRUE,
ci = FALSE,
level = 0.95,
Expand All @@ -110,20 +126,27 @@ ssd_hc.fitdists <- function(
multi_ci = TRUE,
weighted = TRUE,
parametric = TRUE,
delta = 7,
delta = 9.21,
samples = FALSE,
save_to = NULL,
control = NULL,
...
) {

chk_vector(percent)
chk_numeric(percent)
chk_range(percent, c(0, 100))
chk_unused(...)

proportion <- percent / 100

if(lifecycle::is_present(percent)) {
lifecycle::deprecate_soft("1.0.6.9009", "ssd_hc(percent)", "ssd_hc(proportion)", id = "hc")
chk_vector(percent)
chk_numeric(percent)
chk_range(percent, c(0, 100))
proportion <- percent / 100
}

chk_vector(proportion)
chk_numeric(proportion)
chk_range(proportion)

hcp <- ssd_hcp_fitdists(
x = x,
value = proportion,
Expand All @@ -142,8 +165,7 @@ ssd_hc.fitdists <- function(
save_to = save_to,
hc = TRUE)

hcp <- dplyr::rename(hcp, percent = "value")
hcp <- dplyr::mutate(hcp, percent = .data$percent * 100)
hcp <- dplyr::rename(hcp, proportion = "value")
hcp
}

Expand All @@ -155,7 +177,8 @@ ssd_hc.fitdists <- function(
#' ssd_hc(fit)
ssd_hc.fitburrlioz <- function(
x,
percent = 5,
percent,
proportion = 0.05,
ci = FALSE,
level = 0.95,
nboot = 1000,
Expand All @@ -167,15 +190,22 @@ ssd_hc.fitburrlioz <- function(
chk_length(x, upper = 1L)
chk_named(x)
chk_subset(names(x), c("burrIII3", "invpareto", "llogis", "lgumbel"))
chk_vector(percent)
chk_numeric(percent)
chk_range(percent, c(0, 100))
chk_unused(...)

if(lifecycle::is_present(percent)) {
lifecycle::deprecate_soft("1.0.6.9009", "ssd_hc(percent)", "ssd_hc(proportion)", id = "hc")
chk_vector(percent)
chk_numeric(percent)
chk_range(percent, c(0, 100))
proportion <- percent / 100
}

chk_vector(proportion)
chk_numeric(proportion)
chk_range(proportion)

fun <- if(names(x) == "burrIII3") fit_burrlioz else fit_tmb

proportion <- percent / 100

hcp <- ssd_hcp_fitdists (
x = x,
value = proportion,
Expand All @@ -195,7 +225,6 @@ ssd_hc.fitburrlioz <- function(
fix_weights = FALSE,
fun = fun)

hcp <- dplyr::rename(hcp, percent = "value")
hcp <- dplyr::mutate(hcp, percent = .data$percent * 100)
hcp <- dplyr::rename(hcp, proportion = "value")
hcp
}
12 changes: 6 additions & 6 deletions R/hp.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

#' Hazard Percent
#' Hazard Proportion
#'
#' Calculates percent of species affected at specified concentration(s)
#' Calculates proportion of species affected at specified concentration(s)
#' with quantile based bootstrap confidence intervals for
#' individual or model-averaged distributions
#' using parametric or non-parametric bootstrapping.
#' For more information see the inverse function [`ssd_hc()`].
#'
#' @inheritParams params
#' @return A tibble of corresponding hazard percents.
#' @return A tibble of corresponding hazard proportions.
#' @seealso [`ssd_hc()`]
#' @export
#' @examples
Expand All @@ -31,7 +31,7 @@ ssd_hp <- function(x, ...) {
UseMethod("ssd_hp")
}

#' @describeIn ssd_hp Hazard Percents for fitdists Object
#' @describeIn ssd_hp Hazard Proportions for fitdists Object
#' @export
ssd_hp.fitdists <- function(
x,
Expand All @@ -45,7 +45,7 @@ ssd_hp.fitdists <- function(
multi_ci = TRUE,
weighted = TRUE,
parametric = TRUE,
delta = 7,
delta = 9.21,
samples = FALSE,
save_to = NULL,
control = NULL,
Expand Down Expand Up @@ -79,7 +79,7 @@ ssd_hp.fitdists <- function(
}


#' @describeIn ssd_hp Hazard Percents for fitburrlioz Object
#' @describeIn ssd_hp Hazard Proportions for fitburrlioz Object
#' @export
#' @examples
#'
Expand Down
5 changes: 3 additions & 2 deletions R/params.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' Distributions with an absolute AIC difference greater than delta are excluded from the calculations.
#' @param digits A whole number specifying the number of significant figures.
#' @param dists A character vector of the distribution names.
#' @param hc A count between 1 and 99 indicating the percent hazard concentration (or NULL).
#' @param hc A value between 0 and 1 indicating the proportion hazard concentration (or NULL).
#' @param label A string of the column in data with the labels.
#' @param left A string of the column in data with the concentrations.
#' @param level A number between 0 and 1 of the confidence level of the interval.
Expand Down Expand Up @@ -70,8 +70,9 @@
#' @param object The object.
#' @param parametric A flag specifying whether to perform parametric bootstrapping as opposed to non-parametrically resampling the original data with replacement.
#' @param p vector of probabilities.
#' @param percent A numeric vector of percent values to estimate hazard concentrations for.
#' @param percent A numeric vector of percent values to estimate hazard concentrations for. Soft-deprecated for `proportion = 0.05`.
#' @param pmix Proportion mixture parameter.
#' @param proportion A numeric vector of proportion values to estimate hazard concentrations for.
#' @param pvalue A flag specifying whether to return p-values or the statistics (default) for the various tests.
#' @param pred A data frame of the predictions.
#' @param q vector of quantiles.
Expand Down
8 changes: 3 additions & 5 deletions R/plot-cdf.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ ssd_plot_cdf <- function(x, ...) {
#' @examples
#' fits <- ssd_fit_dists(ssddata::ccme_boron)
#' ssd_plot_cdf(fits)
ssd_plot_cdf.fitdists <- function(x, average = FALSE, delta = 7, ...) {
pred <- ssd_hc(x, percent = 1:99, average = average, delta = delta)
ssd_plot_cdf.fitdists <- function(x, average = FALSE, delta = 9.21, ...) {
pred <- ssd_hc(x, proportion = 1:99/100, average = average, delta = delta)
data <- ssd_data(x)
cols <- .cols_fitdists(x)

linetype <- if (length(unique(pred$dist)) > 1) "dist" else NULL
linecolor <- linetype
pred$percent <- round(pred$percent) # not sure why needed

ssd_plot(
data = data, pred = pred, left = cols$left, right = cols$right,
Expand All @@ -57,12 +56,11 @@ ssd_plot_cdf.fitdists <- function(x, average = FALSE, delta = 7, ...) {
#' lnorm = c(meanlog = 2, sdlog = 2)
#' ))
ssd_plot_cdf.list <- function(x, ...) {
pred <- ssd_hc(x, percent = 1:99)
pred <- ssd_hc(x, proportion = 1:99/100)
data <- data.frame(Conc = numeric(0))

linetype <- if (length(unique(pred$dist)) > 1) "dist" else NULL
linecolor <- linetype
pred$percent <- round(pred$percent) # not sure why needed

ssd_plot(
data = data, pred = pred,
Expand Down
Loading

0 comments on commit 9b29e7e

Please sign in to comment.