Skip to content

Commit

Permalink
simplify <epiparameter> distribution methods
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Taylor <[email protected]>
  • Loading branch information
joshwlambert and TimTaylor committed Oct 22, 2024
1 parent 313aa71 commit 058acc2
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions R/epiparameter.R
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,15 @@ density.epiparameter <- function(x, at, ...) {
if (isFALSE(is_parameterised(x))) {
stop("<epiparameter> is unparameterised", call. = FALSE)
}
unlist <- length(x$prob_distribution) == 1
if (inherits(x$prob_distribution, "distcrete")) {
out <- x$prob_distribution$d(at)
} else {
out <- stats::density(x$prob_distribution, at = at)
}
out <- if (unlist) unlist(out, recursive = FALSE) else out
out
if (is.atomic(out)) {
return(out)
}
return(unlist(out, recursive = FALSE))
}

#' @importFrom distributional cdf
Expand All @@ -596,15 +597,16 @@ cdf.epiparameter <- function(x, q, ..., log = FALSE) {
if (isFALSE(is_parameterised(x))) {
stop("<epiparameter> is unparameterised", call. = FALSE)
}
unlist <- length(x$prob_distribution) == 1
if (inherits(x$prob_distribution, "distcrete")) {
out <- x$prob_distribution$p(q)
if (log) out <- log(out)
} else {
out <- distributional::cdf(x$prob_distribution, q = q, ..., log = log)
}
out <- if (unlist) unlist(out, recursive = FALSE) else out
out
if (is.atomic(out)) {
return(out)
}
return(unlist(out, recursive = FALSE))
}

#' @rdname epiparameter_distribution_functions
Expand All @@ -614,14 +616,15 @@ quantile.epiparameter <- function(x, p, ...) {
if (isFALSE(is_parameterised(x))) {
stop("<epiparameter> is unparameterised", call. = FALSE)
}
unlist <- length(x$prob_distribution) == 1
if (inherits(x$prob_distribution, "distcrete")) {
out <- x$prob_distribution$q(p)
} else {
out <- stats::quantile(x$prob_distribution, p = p)
}
out <- if (unlist) unlist(out, recursive = FALSE) else out
out
if (is.atomic(out)) {
return(out)
}
return(unlist(out, recursive = FALSE))
}

#' @importFrom distributional generate
Expand Down

0 comments on commit 058acc2

Please sign in to comment.