Skip to content

Commit

Permalink
Merge 309fa34 into 227e6c7
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmbaazam authored Nov 6, 2023
2 parents 227e6c7 + 309fa34 commit 612b5f9
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 78 deletions.
14 changes: 13 additions & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type: software
license: MIT
title: 'bpmodels: Simulating and Analysing Transmission Chain Statistics using Branching
Process Models'
version: 0.3.0
version: 0.3.1
abstract: Provides methods to simulate and analyse the size and length of branching
processes with an arbitrary offspring distribution. These can be used, for example,
to analyse the distribution of chain sizes or length of infectious disease outbreaks,
Expand Down Expand Up @@ -249,3 +249,15 @@ references:
given-names: Saralees
email: [email protected]
year: '2023'
- type: software
title: checkmate
abstract: 'checkmate: Fast and Versatile Argument Checks'
notes: Imports
url: https://mllg.github.io/checkmate/
repository: https://CRAN.R-project.org/package=checkmate
authors:
- family-names: Lang
given-names: Michel
email: [email protected]
orcid: https://orcid.org/0000-0001-9754-0393
year: '2023'
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: bpmodels
Title: Simulating and Analysing Transmission Chain Statistics using Branching Process
Models
Version: 0.3.0
Version: 0.3.1
Authors@R: c(
person("James M.", "Azam", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "https://orcid.org/0000-0001-5782-7330")),
Expand Down Expand Up @@ -48,3 +48,5 @@ LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Language: en-GB
Imports:
checkmate
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# bpmodels 0.3.1

## Unit tests and input validation

* The following internal functions now have input validation: `rborel()`, `dborel()`, `complementary_logprob()`, and `rnbinom_mean_disp()`.
* Code coverage has been improved with more tests on the following functions: `rborel()`, `dborel()`, `chain_sim()`, `rnbinom_mean_disp()`, `complementary_logprob()`, `rgen_length()`, and `rbinom_size()`.

# bpmodels 0.3.0

## Website
Expand Down
19 changes: 15 additions & 4 deletions R/borel.r
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
##' Density of the Borel distribution
##'
##' @param x vector of integers.
##' @param mu mu parameter.
##' @param x vector of quantiles; integer.
##' @param mu mu parameter (the poisson mean); non-negative.
##' @param log logical; if TRUE, probabilities p are given as log(p).
##' @return probability mass.
##' @author Sebastian Funk
dborel <- function(x, mu, log = FALSE) {
if (x < 1) stop("'x' must be greater than 0")
checkmate::assert_numeric(
x, lower = 1, upper = Inf
)
checkmate::assert_number(
mu, lower = 0, finite = TRUE, na.ok = FALSE
)
ld <- -mu * x + (x - 1) * log(mu * x) - lgamma(x + 1)
if (!log) ld <- exp(ld)
return(ld)
Expand All @@ -16,11 +21,17 @@ dborel <- function(x, mu, log = FALSE) {
##'
##' Random numbers are generated by simulating from a Poisson branching process
##' @param n number of random variates to generate.
##' @param mu mu parameter.
##' @param mu mu parameter (the Poisson mean).
##' @param infinite any number to treat as infinite; simulations will be stopped
##' if this number is reached
##' @return vector of random numbers
##' @author Sebastian Funk
rborel <- function(n, mu, infinite = Inf) {
checkmate::assert_number(
n, lower = 1, finite = TRUE, na.ok = FALSE
)
checkmate::assert_number(
mu, lower = 0, finite = TRUE, na.ok = FALSE
)
chain_sim(n, "pois", "size", infinite = infinite, lambda = mu)
}
16 changes: 14 additions & 2 deletions R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#' @author Sebastian Funk
#' @keywords internal
complementary_logprob <- function(x) {
checkmate::assert_numeric(
x, lower = -Inf, upper = 0
)
tryCatch(log1p(-sum(exp(x))), error = function(e) -Inf)
}

Expand Down Expand Up @@ -43,14 +46,23 @@ rgen_length <- function(n, x, prob) {
#' Negative binomial random numbers parametrized
#' in terms of mean and dispersion coefficient
#' @param n number of samples to draw
#' @param mn mean of distribution
#' @param disp dispersion coefficient (var/mean)
#' @param mn mean of distribution; Must be > 0.
#' @param disp dispersion coefficient (var/mean); Must be > 1.
#' @return vector containing the random numbers
#' @author Flavio Finger
#' @export
#' @examples
#' rnbinom_mean_disp(n = 5, mn = 4, disp = 2)
rnbinom_mean_disp <- function(n, mn, disp) {
checkmate::assert_number(
n, lower = 1, finite = TRUE, na.ok = FALSE
)
checkmate::assert_number(
disp, lower = 1, finite = TRUE, na.ok = FALSE
)
checkmate::assert_number(
mn, lower = 1E-100, finite = TRUE, na.ok = FALSE
)
size <- mn / (disp - 1)
stats::rnbinom(n, size = size, mu = mn)
}
4 changes: 2 additions & 2 deletions man/dborel.Rd

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

2 changes: 1 addition & 1 deletion man/rborel.Rd

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

4 changes: 2 additions & 2 deletions man/rnbinom_mean_disp.Rd

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

28 changes: 28 additions & 0 deletions tests/testthat/test-borel.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
test_that("We can calculate probabilities and sample", {
expect_gt(dborel(1, 0.5), 0)
expect_identical(dborel(1, 0.5, log = TRUE), -0.5)
expect_length(rborel(2, 0.9), 2)
})

test_that("Errors are thrown", {
expect_error(
dborel(x = 0, mu = 0.5),
"is not >= 1"
)
expect_error(
dborel(x = 1, mu = -0.5),
"is not >= 0"
)
expect_error(
dborel(x = 1, mu = Inf),
"Must be finite"
)
expect_error(
rborel(n = 0, mu = -0.5),
"is not >= 1"
)
expect_error(
rborel(n = 0, mu = Inf),
"is not >= 1"
)
})
60 changes: 60 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
test_that("Util functions work", {
expect_length(rnbinom_mean_disp(n = 5, mn = 4, disp = 2), 5)
expect_length(rgen_length(n = 1, x = c(1, 2, 3), prob = 0.3), 3)
expect_length(rbinom_size(n = 1, x = c(1, 2, 3), prob = 0.3), 3)
expect_identical(complementary_logprob(x = 0), -Inf)
expect_identical(complementary_logprob(x = -Inf), 0)
expect_lt(complementary_logprob(x = -0.1), 0)
})

test_that("Errors are thrown", {
# Checks on 'disp' argument
expect_error(
rnbinom_mean_disp(n = 5, mn = 4, disp = 0.9),
"is not >= 1"
)
expect_error(
rnbinom_mean_disp(n = 5, mn = 4, disp = NA),
"May not be NA"
)
expect_error(
rnbinom_mean_disp(n = 5, mn = 4, disp = Inf),
"Must be finite"
)
# Checks on 'n' argument
expect_error(
rnbinom_mean_disp(n = 0, mn = 4, disp = 2),
"is not >= 1"
)
expect_error(
rnbinom_mean_disp(n = NA, mn = 4, disp = 2),
"May not be NA"
)
expect_error(
rnbinom_mean_disp(n = Inf, mn = 4, disp = 2),
"Must be finite"
)
# Checks on 'mn' argument
expect_error(
rnbinom_mean_disp(n = 2, mn = 0, disp = 2)
)
expect_error(
rnbinom_mean_disp(n = 2, mn = NA, disp = 2),
"May not be NA"
)
expect_error(
rnbinom_mean_disp(n = 2, mn = Inf, disp = 2),
"Must be finite"
)
})

test_that("Errors are thrown", {
expect_error(
complementary_logprob(0.1),
"is not <= 0"
)
expect_error(
complementary_logprob(Inf),
"is not <= 0"
)
})
9 changes: 0 additions & 9 deletions tests/testthat/tests-borel.r

This file was deleted.

Loading

0 comments on commit 612b5f9

Please sign in to comment.