Skip to content

Commit

Permalink
local check and docs expand
Browse files Browse the repository at this point in the history
  • Loading branch information
seabbs committed Sep 2, 2024
1 parent f5f1de3 commit 78737b5
Show file tree
Hide file tree
Showing 19 changed files with 301 additions and 55 deletions.
2 changes: 0 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ Suggests:
usethis
Additional_repositories:
https://stan-dev.r-universe.dev
VignetteBuilder:
knitr
Config/Needs/hexsticker: hexSticker, sysfonts
Config/Needs/website: r-lib/pkgdown, epinowcast/enwtheme
Config/testthat/edition: 3
Expand Down
5 changes: 5 additions & 0 deletions R/check.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#' @return NULL. The function will stop execution with an error message if
#' pdist is not a valid CDF.
#' @export
#'
#' @family check
#'
#' @examples
#' check_pdist(pnorm, D = 10)
check_pdist <- function(pdist, D, ...) {
Expand Down Expand Up @@ -43,6 +46,8 @@ check_pdist <- function(pdist, D, ...) {
#' dprimary is not a valid PDF.
#' @export
#'
#' @family check
#'
#' @examples
#' check_dprimary(dunif, pwindow = 1)
check_dprimary <- function(dprimary, pwindow, dprimary_args = list(),
Expand Down
21 changes: 21 additions & 0 deletions R/dprimarycensoreddist.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#' Compute the primary event censored PMF for delays
#'
#'
#' This function computes the primary event censored probability mass function
#' (PMF) for a given set of quantiles. It adjusts the PMF of the primary event
#' distribution by accounting for the delay distribution and potential
#' truncation at a maximum delay (D). The function allows for custom primary
#' event distributions and delay distributions.
#'
#' @inheritParams pprimarycensoreddist
#'
#' @param x Vector of quantiles
Expand All @@ -19,6 +26,20 @@
#'
#' @export
#'
#' @details
#' The primary event censored PMF is computed by taking the difference of the
#' primary event censored cumulative distribution function (CDF) at two points,
#' \eqn{d + \text{swindow}} and \eqn{d}. The primary event censored PMF,
#' \eqn{f_{\text{cens}}(d)}, is given by:
#' \deqn{
#' f_{\text{cens}}(d) = F_{\text{cens}}(d + \text{swindow}) - F_{\text{cens}}(d)
#' }
#' where \eqn{F_{\text{cens}}} is the primary event censored CDF. For the
#' explanation and mathematical details of the CDF, refer to the documentation
#' of [pprimarycensoreddist()].
#'
#' @family primarycensoreddist
#'
#' @examples
#' # Example: Weibull distribution with uniform primary events
#' dprimarycensoreddist(c(0.1, 0.5, 1), pweibull, shape = 1.5, scale = 2.0)
Expand Down
48 changes: 30 additions & 18 deletions R/expgrowth.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,47 @@
#' maximum of the lengths of the numerical arguments for the other functions.
#'
#' @details
#' The exponential growth distribution is defined on the interval [min, max]
#' with rate parameter r. Its probability density function (PDF) is:
#' The exponential growth distribution is defined on the interval \[min, max\]
#' with rate parameter (r). Its probability density function (PDF) is:
#'
#' f(x) = r * exp(r * (x - min)) / (exp(r * max) - exp(r * min))
#' \deqn{f(x) = \frac{r \cdot \exp(r \cdot (x - min))}{\exp(r \cdot max) -
#' \exp(r \cdot min)}}
#'
#' The cumulative distribution function (CDF) is:
#'
#' F(x) = (exp(r * (x - min)) - exp(r * min)) / (exp(r * max) - exp(r * min))
#' \deqn{F(x) = \frac{\exp(r \cdot (x - min)) - \exp(r \cdot min)}{
#' \exp(r \cdot max) - \exp(r \cdot min)}}
#'
#' For random number generation, we use the inverse transform sampling method:
#' 1. Generate u ~ Uniform(0,1)
#' 2. Set F(x) = u and solve for x:
#' x = min + (1/r) * log(u * (exp(r * max) - exp(r * min)) + exp(r * min))
#' 1. Generate \eqn{u \sim \text{Uniform}(0,1)}
#' 2. Set \eqn{F(x) = u} and solve for \eqn{x}:
#' \deqn{
#' x = min + \frac{1}{r} \cdot \log(u \cdot (\exp(r \cdot max) -
#' \exp(r \cdot min)) + \exp(r \cdot min))
#' }
#'
#' This method works because of the probability integral transform theorem,
#' which states that if X is a continuous random variable with CDF F(x),
#' then Y = F(X) follows a Uniform(0,1) distribution. Conversely, if U is
#' a Uniform(0,1) random variable, then F^(-1)(U) has the same distribution
#' as X, where F^(-1) is the inverse of the CDF.
#' which states that if \eqn{X} is a continuous random variable with CDF
#' \eqn{F(x)}, then \eqn{Y = F(X)} follows a \eqn{\text{Uniform}(0,1)}
#' distribution. Conversely, if \eqn{U} is a \eqn{\text{Uniform}(0,1)} random
#' variable, then \eqn{F^{-1}(U)} has the same distribution as \eqn{X}, where
#' \eqn{F^{-1}} is the inverse of the CDF.
#'
#' In our case, we generate u from Uniform(0,1), then solve F(x) = u for x
#' to get a sample from our exponential growth distribution. The formula
#' for x is derived by algebraically solving the equation:
#' In our case, we generate \eqn{u} from \eqn{\text{Uniform}(0,1)}, then solve
#' \eqn{F(x) = u} for \eqn{x} to get a sample from our exponential growth
#' distribution. The formula for \eqn{x} is derived by algebraically solving
#' the equation:
#'
#' u = (exp(r * (x - min)) - exp(r * min)) / (exp(r * max) - exp(r * min))
#' \deqn{
#' u = \frac{\exp(r \cdot (x - min)) - \exp(r \cdot min)}{\exp(r \cdot max) -
#' \exp(r \cdot min)}
#' }
#'
#' When r is very close to 0 (|r| < 1e-10), the distribution approximates
#' a uniform distribution on [min, max], and we use a simpler method to
#' generate samples directly from this uniform distribution.
#' When \eqn{r} is very close to 0 (\eqn{|r| < 1e-10}), the distribution
#' approximates a uniform distribution on \[min, max\], and we use a simpler
#' method to generate samples directly from this uniform distribution.
#'
#' @family primaryeventdistributions
#'
#' @examples
#' x <- seq(0, 1, by = 0.1)
Expand Down
9 changes: 9 additions & 0 deletions R/pcd-stan-tools.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#' Get the path to the Stan code
#'
#' @return A character string with the path to the Stan code
#'
#' @family stantools
#'
#' @export
pcd_stan_path <- function() {
system.file("stan", package = "primarycensoreddist")
Expand Down Expand Up @@ -52,6 +55,9 @@ pcd_stan_path <- function() {
#' the Stan files.
#'
#' @export
#'
#' @family stantools
#'
#' @examples
#' \dontrun{
#' stan_functions <- pcd_stan_functions()
Expand Down Expand Up @@ -90,6 +96,9 @@ pcd_stan_functions <- function(
#' write_to_file is TRUE. Defaults to "pcd_stan_functions.stan".
#'
#' @return A character string containing the requested Stan functions
#'
#' @family stantools
#'
#' @export
pcd_load_stan_functions <- function(
functions = NULL, stan_path = primarycensoreddist::pcd_stan_path(),
Expand Down
29 changes: 29 additions & 0 deletions R/pprimarycensoreddist.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#' Compute the primary event censored CDF for delays
#'
#' This function computes the primary event censored cumulative distribution
#' function (CDF) for a given set of quantiles. It adjusts the CDF of the
#' primary event distribution by accounting for the delay distribution and
#' potential truncation at a maximum delay (D). The function allows for
#' custom primary event distributions and delay distributions.
#'
#' @param q Vector of quantiles
#'
#' @param pdist Distribution function (CDF)
Expand Down Expand Up @@ -33,6 +39,29 @@
#'
#' @export
#'
#' @details
#' The primary event censored CDF is computed by integrating the product of
#' the primary event distribution function (CDF) and the delay distribution
#' function (PDF) over the primary event window. The integration is adjusted
#' for truncation if a finite maximum delay (D) is specified.
#'
#' The primary event censored CDF, \eqn{F_{\text{cens}}(q)}, is given by:
#' \deqn{
#' F_{\text{cens}}(q) = \int_{0}^{pwindow} F(q - p) \cdot f_{\text{primary}}(p)
#' \, dp
#' }
#' where \eqn{F} is the CDF of the primary event distribution,
#' \eqn{f_{\text{primary}}} is the PDF of the primary event times, and
#' \eqn{pwindow} is the primary event window.
#'
#' If the maximum delay \eqn{D} is finite, the CDF is normalized by \eqn{F(D)}:
#' \deqn{
#' F_{\text{cens}}(q) = \int_{0}^{pwindow} \frac{F(q - p)}{F(D - p)} \cdot
#' f_{\text{primary}}(p) \, dp
#' }
#'
#' @family primarycensoreddist
#'
#' @examples
#' # Example: Lognormal distribution with uniform primary events
#' pprimarycensoreddist(c(0.1, 0.5, 1), plnorm, meanlog = 0, sdlog = 1)
Expand Down
24 changes: 22 additions & 2 deletions R/rprimarycensoreddist.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#' Generate random samples from a primary event censored distribution
#'
#' This function generates random samples from a primary event censored
#' distribution. It adjusts the distribution by accounting for the primary
#' event distribution and potential truncation at a maximum delay (D). The
#' function allows for custom primary event distributions and delay
#' distributions.
#'
#' @inheritParams dprimarycensoreddist
#'
#' @param rdist Function to generate random samples from the delay distribution
#' for example \code{rlnorm} for lognormal distribution.
#' for example [stats::rlnorm()] for lognormal distribution.
#'
#' @param n Number of random samples to generate.
#'
#' @param rprimary Function to generate random samples from the primary
#' distribution (default is \code{runif}).
#' distribution (default is [stats::runif()]).
#'
#' @param rprimary_args List of additional arguments to be passed to rprimary.
#'
Expand All @@ -26,6 +32,20 @@
#'
#' @export
#'
#' @details
#' The primary event censored random samples are generated by first
#' oversampling to account for potential truncation. Primary event times are
#' generated from the specified primary event distribution, and delays are
#' generated from the specified delay distribution. The total delays are
#' calculated by adding the primary event times and the delays. These total
#' delays are then rounded to the nearest secondary event window (swindow).
#' Truncation is applied to ensure that the delays are within the specified
#' range \[0, D\]. If the number of valid samples is less than the desired
#' number of samples (n), additional samples are generated until the required
#' number of valid samples is obtained.
#'
#' @family primarycensoreddist
#'
#' @examples
#' # Example: Lognormal distribution with uniform primary events
#' rprimarycensoreddist(10, rlnorm, meanlog = 0, sdlog = 1)
Expand Down
41 changes: 34 additions & 7 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
url: https://primarycensoreddist.epinowcast.org/
template:
package: enwtheme
twitter:
creator: "@seabbs"
card: summary_large_image
opengraph:
twitter:
creator: "@seabbs"
card: summary_large_image

development:
mode: auto

navbar:
structure:
left: [intro, reference, stanreference, articles]
right: [search, github, lightswitch]
components:
stanreference:
text: Stan reference
href: https://primarycensoreddist.epinowcast.org/stan/
articles:
text: Articles
menu:

authors:
Sam Abbott:
href: "https://www.samabbott.co.uk/"

reference:
- title: Primary event censored distribution functions
desc: Primary event censored distribution functions
contents:
- has_concept("primarycensoreddist")
- title: Primary event distributions
desc: Primary event distributions
contents:
- has_concept("primaryeventdistributions")
- title: Distribution checking functions
desc: Distribution checking functions
contents:
- has_concept("check")
- title: Tools for working with package Stan functions
desc: Tools for working with package Stan functions
contents:
- has_concept("stantools")
5 changes: 4 additions & 1 deletion inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ Lifecycle
PMF
PMFs
SamuelBrand
cens
dprimary
modeling
pcd
pdist
primaryeventdistributions
pwindow
rprimary
seabbs
stan
u
stantools
swindow
zsusswein
5 changes: 5 additions & 0 deletions man/check_dprimary.Rd

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

5 changes: 5 additions & 0 deletions man/check_pdist.Rd

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

24 changes: 23 additions & 1 deletion man/dprimarycensoreddist.Rd

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

Loading

0 comments on commit 78737b5

Please sign in to comment.