Skip to content

Commit

Permalink
test for dbetabinom.R (#7)
Browse files Browse the repository at this point in the history
* cleaning up dbetabinom

* removed rounding, not needed

* rmarkdown language and assert for dbetabinom

* changed x and m to number instead of numeric

* test2

* testing dbetabinomMix and asserting inputs

* for dbetabinom and dbetabinomMix, created/edited : test functions and example files

* syntax

* update dbetabinom.R

* Update R/dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* Update R/dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* Update R/dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* Update R/dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* Update R/dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* Update tests/testthat/test-dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* Update tests/testthat/test-dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* Update tests/testthat/test-dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* Update R/dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* periods

* [skip actions] Roxygen Man Pages Auto Update

* added period for roxygen  document
added log option in dbetabinom
document build successful

* [skip actions] Roxygen Man Pages Auto Update

* Update R/dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* Update R/dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* [skip actions] Roxygen Man Pages Auto Update

* log in dbetabinomMix

* assert_flag for log input

* Update R/dbetabinom.R

Co-authored-by: Daniel Sabanes Bove <[email protected]>

* polish and fix tests

---------

Co-authored-by: Daniel Sabanes Bove <[email protected]>
Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 25, 2023
1 parent be3c928 commit e649e9c
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 102 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ vignettes/*.md
vignettes/*.R
coverage.*
.vscode/

tests/testthat/junit-result.xml

tests/unit_testing_results.rds
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Roxygen:
RoxygenNote: 7.2.3
Config/Needs/documentation:
roxytypes
Remotes:
openpharma/roxytypes
Config/roxytypes: list(format = "({type}):\\cr {description}")
Collate:
'Phase1b-package.R'
Expand Down
100 changes: 63 additions & 37 deletions R/dbetabinom.R
Original file line number Diff line number Diff line change
@@ -1,47 +1,73 @@
#' Beta-binomial density function
#'
#' Calculates the density function of the beta-binomial distribution
#' @description `r lifecycle::badge("experimental")`
#'
#' Note that \code{x} can be a vector.
#' Calculates the density function of the beta-binomial distribution.
#'
#' The beta-binomial density function has the following form:
#' \deqn{p(x) = (m! / (x!*(m-x)!)) * Beta(x+a,m-x+b) / Beta(a,b)}
#'
#' @param x number of successes
#' @param m number of trials
#' @param a first parameter of the beta distribution
#' @param b second parameter of the beta distribution
#' @return the density values of the beta-binomial distribution at \code{x}
#' `p(x) = (m! / (x!*(m-x)!)) * Beta(x+a,m-x+b) / Beta(a,b)`
#'
#' @typed x : numeric
#' number of successes.
#' @typed m : number
#' number of trials.
#' @typed a : numeric
#' first parameter of the beta distribution.
#' @typed b : numeric
#' second parameter of the beta distribution.
#' @typed log : flag
#' whether to return the log density value (not default).
#' @return The density values of the beta-binomial distribution at `x`.
#'
#' @note `x`, `a` and `b` can be vectors.
#'
#' @example examples/dbetabinom.R
#' @export
dbetabinom <- function(x, m, a, b) {
logRet <- lchoose(m, x) + lbeta(x + a, m - x + b) - lbeta(a, b)
exp(logRet)
dbetabinom <- function(x, m, a, b, log = FALSE) {
assert_numeric(x, lower = 0, upper = m, finite = TRUE)
assert_number(m, lower = 0, finite = TRUE)
assert_numeric(a, lower = 0, finite = TRUE)
assert_numeric(b, lower = 0, finite = TRUE)
assert_flag(log)
log_ret <- lchoose(m, x) + lbeta(x + a, m - x + b) - lbeta(a, b)
if (log) {
log_ret
} else {
exp(log_ret)
}
}


#' Beta-mixture-binomial density function
#'
#' Calculates the density function for a mixture of beta-binomial distributions.
#' @description `r lifecycle::badge("experimental")`
#'
#' Note that \code{x} can be a vector.
#'
#' @param x number of successes
#' @param m number of trials
#' @param par the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components
#' @param weights the mixture weights of the beta mixture prior
#' @param log return the log value? (not default)
#' @return The (log) density values of the mixture of beta-binomial distributions at \code{x}.
#' Calculates the density function for a mixture of beta-binomial distributions.
#'
#' @typed x : numeric
#' number of successes.
#' @typed m : number
#' number of trials.
#' @typed par : matrix
#' the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components.
#' @typed weights : numeric
#' the mixture weights of the beta mixture prior.
#' @typed log : flag
#' whether to return the log density value (not default).
#' @return The (log) density values of the mixture of beta-binomial distributions at `x`.
#' @note `x` can be a vector.
#'
#' @example examples/dbetabinomMix.R
#' @export
dbetabinomMix <- function(x, m, par, weights, log = FALSE) {
assert_matrix(par, min.rows = 1, min.cols = 2)
assert_flag(log)
ret <- sum(weights * dbetabinom(x, m, par[, 1], par[, 2]))
if (log) {
return(log(ret))
log(ret)
} else {
return(ret)
ret
}
}
dbetabinomMix <- Vectorize(dbetabinomMix, vectorize.args = "x")
Expand Down Expand Up @@ -98,7 +124,7 @@ getBetamixPost <- function(x, n, par, weights) {

#' Beta-mixture density function
#'
#' Note that \code{x} can be a vector.
#' Note that `x` can be a vector.
#'
#' @param x the abscissa
#' @param par the beta parameters matrix, with K rows and 2 columns,
Expand All @@ -121,15 +147,15 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x")

#' Beta-mixture cdf
#'
#' Note that \code{x} can be a vector.
#' Note that `x` can be a vector.
#'
#' @param x the abscissa
#' @param x the abscissa.
#' @param par the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components
#' @param weights the mixture weights of the beta mixture prior
#' corresponding to the beta parameters of the K components.
#' @param weights the mixture weights of the beta mixture prior.
#' @param lower.tail logical; if TRUE (default), probabilities are `P[X <= x]`,
#' and otherwise `P[X > x]`
#' @return the (one minus) cdf value
#' and otherwise `P[X > x]`.
#' @return the (one minus) cdf value.
#'
#' @export
pbetaMix <- function(x, par, weights, lower.tail = TRUE) {
Expand All @@ -141,13 +167,13 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x")

#' Beta-mixture quantile function
#'
#' Note that \code{q} can be a vector.
#' Note that `x` can be a vector.
#'
#' @param q the required quantile
#' @param par the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components
#' @param weights the mixture weights of the beta mixture prior
#' @return the abscissa
#' @param q the required quantile.
#' @param par the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components.
#' @param weights the mixture weights of the beta mixture prior.
#' @return the abscissa.
#'
#' @export
qbetaMix <- function(q, par, weights) {
Expand Down
13 changes: 4 additions & 9 deletions examples/dbetabinom.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
## Calculating the beta binomial density, x = 2; m = 29; a = 0.2; b = 0.4
##
## when p(x) = choose(29,2)*beta(2.2,27.4)/beta(0.2,0.4) = 0.04286893
##
dbetabinom(2, 29, 0.2, 0.4)
dbetabinom(x = 2, m = 29, a = 0.2, b = 0.4, log = FALSE)

## Can also specify x as a vector, x = 1:28
##
##
dbetabinom(1:28, 29, 0.2, 0.4)
# Can also specify x as a vector.

dbetabinom(x = 1:28, m = 29, a = 0.2, b = 0.4, log = FALSE)
18 changes: 1 addition & 17 deletions examples/dbetabinomMix.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
## Calculating the density of a mixture
## of beta binomial densities at x, x = 2; m = 29; a = 0.2; b = 0.4
##
##
## Only 1 mixture component, i.e., weights = 1, p(x) = choose(29,2)*beta(2.2,27.4)/beta(0.2,0.4) = 0.04286893
##
##
dbetabinomMix(x = 2, m = 29, par = rbind(c(0.2, 0.4)), weights = 1)

## With 2 mixture components
## Weight 0.6 for component 1; a = 0.2, b = 0.4
## Weight 0.4 for component 2; a = 1.0, b = 1.0
## when p(x) = 0.6*choose(29,2)*beta(2.2,27.4)/beta(0.2,0.4) + 0.4*choose(29,2)*beta(3,28)/beta(1,1) = 0.03905469
##
##
dbetabinomMix(
x = 2, m = 29, par = rbind(c(0.2, 0.4), c(1, 1)),
weights = c(0.6, 0.4)
)

## Can also specify x as a vector, x = 1:28
##
##
# x could be a vector
dbetabinomMix(
x = 1:28, m = 29, par = rbind(c(0.2, 0.4), c(1, 1)),
weights = c(0.6, 0.4)
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,7 @@ datepicker
datetime
db<U+034A>
dBe
dbetabinom
DCBCBC
dcc
DCC
Expand Down
41 changes: 20 additions & 21 deletions man/dbetabinom.Rd

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

31 changes: 23 additions & 8 deletions man/dbetabinomMix.Rd

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

10 changes: 5 additions & 5 deletions man/pbetaMix.Rd

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

10 changes: 5 additions & 5 deletions man/qbetaMix.Rd

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

Loading

0 comments on commit e649e9c

Please sign in to comment.