From 50ef5b122e82f0e67295af746781431bc501c026 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 14 Aug 2023 17:53:05 +0200 Subject: [PATCH 01/46] cleaning up dbetabinom --- R/dbetabinom.R | 32 +++++++++++++++++++++++--------- man/dbetabinom.Rd | 13 +++++++------ man/dbetabinomMix.Rd | 16 +++++++++------- tests/testthat/test-dbetabinom.R | 5 +++++ 4 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 tests/testthat/test-dbetabinom.R diff --git a/R/dbetabinom.R b/R/dbetabinom.R index be152158..80cb4ef3 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -1,5 +1,7 @@ #' Beta-binomial density function #' +#' @description `r lifecycle::badge("experimental")` +#' #' Calculates the density function of the beta-binomial distribution #' #' Note that \code{x} can be a vector. @@ -7,15 +9,23 @@ #' 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 +#' @typed x : numeric +#' number of successes +#' @typed m : numeric +#' number of trials +#' @typed a : numeric +#' first parameter of the beta distribution +#' @typed b : numeric +#' second parameter of the beta distribution #' @return the density values of the beta-binomial distribution at \code{x} #' #' @example examples/dbetabinom.R #' @export dbetabinom <- function(x, m, a, b) { + assert_numeric(x, lower = 0, upper = m, finite = TRUE) + assert_numeric(m, lower = 0, finite = TRUE) + assert_numeric(a, lower = 0, finite = TRUE) + assert_numeric(b, lower = 0, finite = TRUE) logRet <- lchoose(m, x) + lbeta(x + a, m - x + b) - lbeta(a, b) exp(logRet) } @@ -27,12 +37,16 @@ dbetabinom <- function(x, m, a, b) { #' #' 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, +#' @typed x : numeric +#' number of successes +#' @param m : numeric +#' number of trials +#' @param par : numeric +#' 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) +#' weights the mixture weights of the beta mixture prior +#' @param log : numeric +#' return the log value? (not default) #' @return The (log) density values of the mixture of beta-binomial distributions at \code{x}. #' #' @export diff --git a/man/dbetabinom.Rd b/man/dbetabinom.Rd index 1d6a61b2..bdc2b681 100644 --- a/man/dbetabinom.Rd +++ b/man/dbetabinom.Rd @@ -7,21 +7,22 @@ dbetabinom(x, m, a, b) } \arguments{ -\item{x}{number of successes} +\item{x}{(\code{numeric}):\cr number of successes} -\item{m}{number of trials} +\item{m}{(\code{numeric}):\cr number of trials} -\item{a}{first parameter of the beta distribution} +\item{a}{(\code{numeric}):\cr first parameter of the beta distribution} -\item{b}{second parameter of the beta distribution} +\item{b}{(\code{numeric}):\cr second parameter of the beta distribution} } \value{ the density values of the beta-binomial distribution at \code{x} } \description{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} + Calculates the density function of the beta-binomial distribution -} -\details{ + Note that \code{x} can be a vector. The beta-binomial density function has the following form: diff --git a/man/dbetabinomMix.Rd b/man/dbetabinomMix.Rd index 813b091c..dc39b9de 100644 --- a/man/dbetabinomMix.Rd +++ b/man/dbetabinomMix.Rd @@ -7,16 +7,18 @@ dbetabinomMix(x, m, par, weights, log = FALSE) } \arguments{ -\item{x}{number of successes} +\item{x}{(\code{numeric}):\cr number of successes} -\item{m}{number of trials} +\item{m}{: numeric +number of trials} -\item{par}{the beta parameters matrix, with K rows and 2 columns, -corresponding to the beta parameters of the K components} +\item{par}{: numeric +the beta parameters matrix, with K rows and 2 columns, +corresponding to the beta parameters of the K components +weights the mixture weights of the beta mixture prior} -\item{weights}{the mixture weights of the beta mixture prior} - -\item{log}{return the log value? (not default)} +\item{log}{: numeric +return the log value? (not default)} } \value{ The (log) density values of the mixture of beta-binomial distributions at \code{x}. diff --git a/tests/testthat/test-dbetabinom.R b/tests/testthat/test-dbetabinom.R new file mode 100644 index 00000000..29e7a4d6 --- /dev/null +++ b/tests/testthat/test-dbetabinom.R @@ -0,0 +1,5 @@ +test_that("the dbetabinom density for every x support is between 0 and 1", { + results <- round(dbetabinom(10, 20, 0.7, 2), digits = 2) + expected <- 0.04 + expect_number(results, lower = 0, upper = 1) +}) From 4342b5a66e19685957539bfe845b380137e77518 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 14 Aug 2023 17:56:34 +0200 Subject: [PATCH 02/46] removed rounding, not needed --- tests/testthat/test-dbetabinom.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/testthat/test-dbetabinom.R b/tests/testthat/test-dbetabinom.R index 29e7a4d6..0dc5113e 100644 --- a/tests/testthat/test-dbetabinom.R +++ b/tests/testthat/test-dbetabinom.R @@ -1,5 +1,4 @@ test_that("the dbetabinom density for every x support is between 0 and 1", { - results <- round(dbetabinom(10, 20, 0.7, 2), digits = 2) - expected <- 0.04 + results <- dbetabinom(10, 20, 0.7, 2) expect_number(results, lower = 0, upper = 1) }) From b095ae62e0f5770fbb083a34f4e27c842cc01905 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 22 Aug 2023 13:49:39 +0200 Subject: [PATCH 03/46] rmarkdown language and assert for dbetabinom --- R/dbetabinom.R | 34 +++++++++++++++++++------------- man/dbetabinom.Rd | 2 +- man/dbetabinomMix.Rd | 13 +++++------- man/qbetaMix.Rd | 2 +- tests/testthat/test-dbetabinom.R | 5 +++++ 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 80cb4ef3..23e4e776 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -4,10 +4,10 @@ #' #' Calculates the density function of the beta-binomial distribution #' -#' Note that \code{x} can be a vector. +#' Note that `x` can be a vector. #' #' 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)} +#' `p(x) = (m! / (x!*(m-x)!)) * Beta(x+a,m-x+b) / Beta(a,b)` #' #' @typed x : numeric #' number of successes @@ -17,7 +17,7 @@ #' first parameter of the beta distribution #' @typed b : numeric #' second parameter of the beta distribution -#' @return the density values of the beta-binomial distribution at \code{x} +#' @return the density values of the beta-binomial distribution at `x` #' #' @example examples/dbetabinom.R #' @export @@ -35,27 +35,29 @@ dbetabinom <- function(x, m, a, b) { #' #' Calculates the density function for a mixture of beta-binomial distributions. #' -#' Note that \code{x} can be a vector. +#' Note that can be a vector. ## TODO markdown syntax #' -#' @typed x : numeric +#' @typed x : number #' number of successes -#' @param m : numeric +#' @typed m : number #' number of trials -#' @param par : numeric +#' @typed par : numeric matrix #' the beta parameters matrix, with K rows and 2 columns, #' corresponding to the beta parameters of the K components #' weights the mixture weights of the beta mixture prior -#' @param log : numeric +#' @typed log : flag #' return the log value? (not default) -#' @return The (log) density values of the mixture of beta-binomial distributions at \code{x}. +#' @return The (log) density values of the mixture of beta-binomial distributions at `x`. #' #' @export dbetabinomMix <- function(x, m, par, weights, log = FALSE) { + # TODO par has to columns, assert this, weights have same dim as par1 and 2 + # TODO weight assert numeric 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") @@ -112,7 +114,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, @@ -135,7 +137,7 @@ 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 par the beta parameters matrix, with K rows and 2 columns, @@ -147,6 +149,10 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' #' @export pbetaMix <- function(x, par, weights, lower.tail = TRUE) { + assert_numeric(x, lower = 0, finite = TRUE) + assert_numeric(weights, lower = 0, upper = 1, finite = TRUE) + assert_numeric(pbeta, lower = 0, upper = 1, finite = TRUE) + assert_vector(par) ret <- sum(weights * pbeta(x, par[, 1], par[, 2], lower.tail = lower.tail)) return(ret) } @@ -155,7 +161,7 @@ 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, diff --git a/man/dbetabinom.Rd b/man/dbetabinom.Rd index bdc2b681..56cf9da4 100644 --- a/man/dbetabinom.Rd +++ b/man/dbetabinom.Rd @@ -26,7 +26,7 @@ Calculates the density function of the beta-binomial distribution Note that \code{x} can be a vector. 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)} +\verb{p(x) = (m! / (x!*(m-x)!)) * Beta(x+a,m-x+b) / Beta(a,b)} } \examples{ ## Calculating the beta binomial density, x = 2; m = 29; a = 0.2; b = 0.4 diff --git a/man/dbetabinomMix.Rd b/man/dbetabinomMix.Rd index dc39b9de..d9a24bac 100644 --- a/man/dbetabinomMix.Rd +++ b/man/dbetabinomMix.Rd @@ -7,18 +7,15 @@ dbetabinomMix(x, m, par, weights, log = FALSE) } \arguments{ -\item{x}{(\code{numeric}):\cr number of successes} +\item{x}{(\code{number}):\cr number of successes} -\item{m}{: numeric -number of trials} +\item{m}{(\code{number}):\cr number of trials} -\item{par}{: numeric -the beta parameters matrix, with K rows and 2 columns, +\item{par}{(\verb{numeric matrix}):\cr the beta parameters matrix, with K rows and 2 columns, corresponding to the beta parameters of the K components weights the mixture weights of the beta mixture prior} -\item{log}{: numeric -return the log value? (not default)} +\item{log}{(\code{flag}):\cr return the log value? (not default)} } \value{ The (log) density values of the mixture of beta-binomial distributions at \code{x}. @@ -27,5 +24,5 @@ The (log) density values of the mixture of beta-binomial distributions at \code{ Calculates the density function for a mixture of beta-binomial distributions. } \details{ -Note that \code{x} can be a vector. +Note that can be a vector. ## TODO markdown syntax } diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index 340e6232..ddb09db3 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -18,5 +18,5 @@ corresponding to the beta parameters of the K components} the abscissa } \description{ -Note that \code{q} can be a vector. +Note that \code{x} can be a vector. } diff --git a/tests/testthat/test-dbetabinom.R b/tests/testthat/test-dbetabinom.R index 0dc5113e..233c5446 100644 --- a/tests/testthat/test-dbetabinom.R +++ b/tests/testthat/test-dbetabinom.R @@ -2,3 +2,8 @@ test_that("the dbetabinom density for every x support is between 0 and 1", { results <- dbetabinom(10, 20, 0.7, 2) expect_number(results, lower = 0, upper = 1) }) + +test_that("the sum of the dbetabinom density for all x is 1", { + result <- sum(dbetabinom(0:10, 10, 1, 1)) + expect_equal(result, 1) +}) From 889e97dbdc822e09525fbd68fcd37b91eb374bd6 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 22 Aug 2023 13:52:59 +0200 Subject: [PATCH 04/46] changed x and m to number instead of numeric --- R/dbetabinom.R | 4 ++-- man/dbetabinom.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 23e4e776..4be07538 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -9,9 +9,9 @@ #' The beta-binomial density function has the following form: #' `p(x) = (m! / (x!*(m-x)!)) * Beta(x+a,m-x+b) / Beta(a,b)` #' -#' @typed x : numeric +#' @typed x : number #' number of successes -#' @typed m : numeric +#' @typed m : number #' number of trials #' @typed a : numeric #' first parameter of the beta distribution diff --git a/man/dbetabinom.Rd b/man/dbetabinom.Rd index 56cf9da4..8b5bfb03 100644 --- a/man/dbetabinom.Rd +++ b/man/dbetabinom.Rd @@ -7,9 +7,9 @@ dbetabinom(x, m, a, b) } \arguments{ -\item{x}{(\code{numeric}):\cr number of successes} +\item{x}{(\code{number}):\cr number of successes} -\item{m}{(\code{numeric}):\cr number of trials} +\item{m}{(\code{number}):\cr number of trials} \item{a}{(\code{numeric}):\cr first parameter of the beta distribution} From 2eafdc54d12bdbaef7478a5aa1a4bab4b010c184 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 22 Aug 2023 14:05:12 +0200 Subject: [PATCH 05/46] test --- R/dbetabinom.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 4be07538..82826b0b 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -154,7 +154,7 @@ pbetaMix <- function(x, par, weights, lower.tail = TRUE) { assert_numeric(pbeta, lower = 0, upper = 1, finite = TRUE) assert_vector(par) ret <- sum(weights * pbeta(x, par[, 1], par[, 2], lower.tail = lower.tail)) - return(ret) + return(ret) # hdhddhh } pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x") From 9dc428bf73974c13f5e623192757bfa5e5309332 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 23 Aug 2023 13:46:03 +0200 Subject: [PATCH 06/46] pbetaMix function and test --- R/dbetabinom.R | 24 ++++++++++++++---------- inst/WORDLIST | 1 + man/pbetaMix.Rd | 12 +++++++----- man/qbetaMix.Rd | 5 ++--- tests/testthat/test-dbetabinom.R | 6 ++++++ 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 82826b0b..faec6d9c 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -139,22 +139,25 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' #' Note that `x` can be a vector. #' -#' @param x the abscissa -#' @param par the beta parameters matrix, with K rows and 2 columns, +#' @typed x : number +#' the abscissa ## TODO what is this +#' @typed par : matrix or array +#' 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 lower.tail logical; if TRUE (default), probabilities are `P[X <= x]`, +#' @typed weights : matrix or array +#' the mixture weights of the beta mixture prior +#' @typed lower.tail : logical # TODO Why not flag +#' if TRUE (default), probabilities are `P[X <= x]`, #' and otherwise `P[X > x]` -#' @return the (one minus) cdf value -#' +#' @return the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-" +#' ## TODO could it be ret : numeric, `P[X <= x]` #' @export pbetaMix <- function(x, par, weights, lower.tail = TRUE) { assert_numeric(x, lower = 0, finite = TRUE) assert_numeric(weights, lower = 0, upper = 1, finite = TRUE) - assert_numeric(pbeta, lower = 0, upper = 1, finite = TRUE) assert_vector(par) ret <- sum(weights * pbeta(x, par[, 1], par[, 2], lower.tail = lower.tail)) - return(ret) # hdhddhh + ret } pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x") @@ -163,8 +166,9 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x") #' #' Note that `x` can be a vector. #' -#' @param q the required quantile -#' @param par the beta parameters matrix, with K rows and 2 columns, +#' @typed q : numeric +#' the required quantile +#' @typed 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 diff --git a/inst/WORDLIST b/inst/WORDLIST index ef88f286..23c1bd0c 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -3238,6 +3238,7 @@ resizable ResourceRef responder responders +ret rf rG RG diff --git a/man/pbetaMix.Rd b/man/pbetaMix.Rd index 2b875742..3838f0e4 100644 --- a/man/pbetaMix.Rd +++ b/man/pbetaMix.Rd @@ -7,18 +7,20 @@ pbetaMix(x, par, weights, lower.tail = TRUE) } \arguments{ -\item{x}{the abscissa} +\item{x}{(\code{number}):\cr the abscissa ## TODO what is this} -\item{par}{the beta parameters matrix, with K rows and 2 columns, +\item{par}{(\verb{matrix or array}):\cr the beta parameters matrix, with K rows and 2 columns, corresponding to the beta parameters of the K components} -\item{weights}{the mixture weights of the beta mixture prior} +\item{weights}{(\verb{matrix or array}):\cr the mixture weights of the beta mixture prior} -\item{lower.tail}{logical; if TRUE (default), probabilities are \code{P[X <= x]}, +\item{lower.tail}{(\code{logical # TODO Why not flag}):\cr if TRUE (default), probabilities are \code{P[X <= x]}, and otherwise \code{P[X > x]}} } \value{ -the (one minus) cdf value +the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-" +\subsection{TODO could it be ret : numeric, \code{P[X <= x]}}{ +} } \description{ Note that \code{x} can be a vector. diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index ddb09db3..cc1d58c5 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -7,10 +7,9 @@ qbetaMix(q, par, weights) } \arguments{ -\item{q}{the required quantile} +\item{q}{(\code{numeric}):\cr the required quantile} -\item{par}{the beta parameters matrix, with K rows and 2 columns, -corresponding to the beta parameters of the K components} +\item{par}{(\verb{the beta parameters matrix, with K rows and 2 columns,}):\cr corresponding to the beta parameters of the K components} \item{weights}{the mixture weights of the beta mixture prior} } diff --git a/tests/testthat/test-dbetabinom.R b/tests/testthat/test-dbetabinom.R index 233c5446..956fc30f 100644 --- a/tests/testthat/test-dbetabinom.R +++ b/tests/testthat/test-dbetabinom.R @@ -7,3 +7,9 @@ test_that("the sum of the dbetabinom density for all x is 1", { result <- sum(dbetabinom(0:10, 10, 1, 1)) expect_equal(result, 1) }) + +test_that("the pbetaMix has incrementally higher cdf with increase x support", { + is_lower <- pbetaMix(x = 2, ...) # TODO fill in the blanks + is_higher <- pbetaMix(x = 3, ...) # TODO fill in the blanks + expect_true(is_lower > is_higher) +}) From dd1d8c45bb6673876f5947908b2e9ac930f3e0ee Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Thu, 24 Aug 2023 15:02:40 +0200 Subject: [PATCH 07/46] added new tests and changed documentation --- R/dbetabinom.R | 42 +++++++++++++++++++------------- examples/pbetaMix.R | 16 +----------- man/pbetaMix.Rd | 14 +++++------ man/qbetaMix.Rd | 13 ++++++---- tests/testthat/test-dbetabinom.R | 20 ++++++++++++--- 5 files changed, 58 insertions(+), 47 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index faec6d9c..8cbdc180 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -137,20 +137,22 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' Beta-mixture cdf #' +#' @description `r lifecycle::badge("experimental")` +#' #' Note that `x` can be a vector. #' #' @typed x : number -#' the abscissa ## TODO what is this +#' the abscissa. #' @typed par : matrix or array -#' the beta parameters matrix, with K rows and 2 columns, -#' corresponding to the beta parameters of the K components -#' @typed weights : matrix or array -#' the mixture weights of the beta mixture prior +#' the beta parameters matrix, with K rows and 2 columns, +#' corresponding to the beta parameters of the K components. +#' @typed weights : matrix +#' the mixture weights of the beta mixture prior. #' @typed lower.tail : logical # TODO Why not flag -#' if TRUE (default), probabilities are `P[X <= x]`, -#' and otherwise `P[X > x]` -#' @return the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-" -#' ## TODO could it be ret : numeric, `P[X <= x]` +#' if TRUE (default), probabilities are `P[X <= x]`, +#' and otherwise `P[X > x]`. +#' @return the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". +#' #' @export pbetaMix <- function(x, par, weights, lower.tail = TRUE) { assert_numeric(x, lower = 0, finite = TRUE) @@ -164,25 +166,31 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x") #' Beta-mixture quantile function #' +#' @description `r lifecycle::badge("experimental")` +#' #' Note that `x` can be a vector. #' #' @typed q : numeric -#' the required quantile -#' @typed 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 +#' the required quantile. +#' @typed par : number +#' the beta parameters matrix, with K rows and 2 columns, +#' corresponding to the beta parameters of the K components. +#' @typed weights : matrix +#' the mixture weights of the beta mixture prior. +#' @return the abscissa. #' #' @export -qbetaMix <- function(q, par, weights) { +qbetaMix <- function(q, par, weights, lower.tail) { f <- function(pi) { pbetaMix(x = pi, par = par, weights = weights) - q } + assert_number(f, lower = 0, upper = 1, finite = TRUE) unirootResult <- uniroot(f, lower = 0, upper = 1) + assert_number(unirootResult, lower = 0, upper = 1, finite = TRUE) if (unirootResult$iter < 0) { - return(NA) + NA } else { - return(unirootResult$root) + unirootResult$root } } qbetaMix <- Vectorize(qbetaMix, vectorize.args = "q") diff --git a/examples/pbetaMix.R b/examples/pbetaMix.R index 32b34342..422b1cf6 100644 --- a/examples/pbetaMix.R +++ b/examples/pbetaMix.R @@ -1,31 +1,17 @@ -## Calculating the CDF of a mixture -## of beta densities at x, x = 0.3; a = 0.2; b = 0.4 -## -## -## Only 1 mixture component, i.e., weights = 1 -## Compare to pbeta(0.3,0.2,0.4) = 0.5947341 -## pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) ## Can get the one minus CDF values -## Need to specify lower.tail = FALSE, 1 - 0.5947341 = 0.4052659 -## -## pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE) ## 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 -## Compare to 0.6*pbeta(0.3,0.2,0.4) + 0.4*pbeta(0.3,1,1) = 0.4768404 -## pbetaMix( x = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) -## Can also specify x as a vector, x = seq(0,1,.01) -## -## +## Can also specify x as a vector. pbetaMix( x = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) diff --git a/man/pbetaMix.Rd b/man/pbetaMix.Rd index 3838f0e4..ffd8ec96 100644 --- a/man/pbetaMix.Rd +++ b/man/pbetaMix.Rd @@ -7,21 +7,21 @@ pbetaMix(x, par, weights, lower.tail = TRUE) } \arguments{ -\item{x}{(\code{number}):\cr the abscissa ## TODO what is this} +\item{x}{(\code{number}):\cr the abscissa.} \item{par}{(\verb{matrix or array}):\cr the beta parameters matrix, with K rows and 2 columns, -corresponding to the beta parameters of the K components} +corresponding to the beta parameters of the K components.} -\item{weights}{(\verb{matrix or array}):\cr the mixture weights of the beta mixture prior} +\item{weights}{(\code{matrix}):\cr the mixture weights of the beta mixture prior.} \item{lower.tail}{(\code{logical # TODO Why not flag}):\cr if TRUE (default), probabilities are \code{P[X <= x]}, -and otherwise \code{P[X > x]}} +and otherwise \code{P[X > x]}.} } \value{ -the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-" -\subsection{TODO could it be ret : numeric, \code{P[X <= x]}}{ -} +the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". } \description{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} + Note that \code{x} can be a vector. } diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index cc1d58c5..c27ee6d9 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -4,18 +4,21 @@ \alias{qbetaMix} \title{Beta-mixture quantile function} \usage{ -qbetaMix(q, par, weights) +qbetaMix(q, par, weights, lower.tail) } \arguments{ -\item{q}{(\code{numeric}):\cr the required quantile} +\item{q}{(\code{numeric}):\cr the required quantile.} -\item{par}{(\verb{the beta parameters matrix, with K rows and 2 columns,}):\cr corresponding to the beta parameters of the K components} +\item{par}{(\code{number}):\cr the beta parameters matrix, with K rows and 2 columns, +corresponding to the beta parameters of the K components.} -\item{weights}{the mixture weights of the beta mixture prior} +\item{weights}{(\code{matrix}):\cr the mixture weights of the beta mixture prior.} } \value{ -the abscissa +the abscissa. } \description{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} + Note that \code{x} can be a vector. } diff --git a/tests/testthat/test-dbetabinom.R b/tests/testthat/test-dbetabinom.R index 956fc30f..22c0e888 100644 --- a/tests/testthat/test-dbetabinom.R +++ b/tests/testthat/test-dbetabinom.R @@ -9,7 +9,21 @@ test_that("the sum of the dbetabinom density for all x is 1", { }) test_that("the pbetaMix has incrementally higher cdf with increase x support", { - is_lower <- pbetaMix(x = 2, ...) # TODO fill in the blanks - is_higher <- pbetaMix(x = 3, ...) # TODO fill in the blanks - expect_true(is_lower > is_higher) + is_lower <- pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) + is_higher <- pbetaMix(x = 0.5, par = rbind(c(0.2, 0.4)), weights = 1) + expect_true(is_lower < is_higher) +}) + +test_that("the pbetaMix has the correct numeric result", { + result <- pbetaMix( + x = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)), + weights = c(0.6, 0.4) + ) + expect_equal(result, 0.4768404, tolerance = 1e9) +}) + +test_that("the complement of pbetaMix can be derived with a different lower.tail flag", { + result <- pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE) + result_inversed <- pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = TRUE) + expect_equal(result, 1 - result_inversed, tolerance = 1e9) }) From 6d7f47880177f4f51a4cd4b81aabc94bb24e8a55 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Thu, 24 Aug 2023 15:15:59 +0200 Subject: [PATCH 08/46] upper case for test_that comments and more tests for qbeta --- examples/qbetaMix.R | 13 +------------ tests/testthat/test-dbetabinom.R | 28 +++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/examples/qbetaMix.R b/examples/qbetaMix.R index 92584a8a..81e1a2e7 100644 --- a/examples/qbetaMix.R +++ b/examples/qbetaMix.R @@ -1,24 +1,13 @@ -## Calculating the quantile (inverse CDF) of a mixture -## of beta densities at x where q = 0.6; a = 0.2; b = 0.4 -## -## ## Only 1 mixture component, i.e., weights = 1 -## Compare to qbeta(0.6,0.2,0.4) = 0.3112065 -## qbetaMix(q = 0.60, 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 -## qbetaMix( q = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) -## Can also specify q as a vector : q = seq(0,1,.01) -## -## +## Can also specify q as a vector qbetaMix( q = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) diff --git a/tests/testthat/test-dbetabinom.R b/tests/testthat/test-dbetabinom.R index 22c0e888..074f0ca7 100644 --- a/tests/testthat/test-dbetabinom.R +++ b/tests/testthat/test-dbetabinom.R @@ -8,13 +8,15 @@ test_that("the sum of the dbetabinom density for all x is 1", { expect_equal(result, 1) }) -test_that("the pbetaMix has incrementally higher cdf with increase x support", { +## pbetaMix ---- + +test_that("The pbetaMix has incrementally higher cdf with increase x support", { is_lower <- pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) is_higher <- pbetaMix(x = 0.5, par = rbind(c(0.2, 0.4)), weights = 1) expect_true(is_lower < is_higher) }) -test_that("the pbetaMix has the correct numeric result", { +test_that("The pbetaMix has the correct numeric result", { result <- pbetaMix( x = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) @@ -22,8 +24,28 @@ test_that("the pbetaMix has the correct numeric result", { expect_equal(result, 0.4768404, tolerance = 1e9) }) -test_that("the complement of pbetaMix can be derived with a different lower.tail flag", { +test_that("The complement of pbetaMix can be derived with a different lower.tail flag", { result <- pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE) result_inversed <- pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = TRUE) expect_equal(result, 1 - result_inversed, tolerance = 1e9) }) + +## qbetaMix ---- + +test_that("The qbetaMix has the correct numeric result", { # TODO ask if "number" more accurate + result <- qbetaMix(q = 0.60, par = rbind(c(0.2, 0.4)), weights = 1) + expect_equal(result, 0.3112065, tolerance = 1e9) +}) + +test_that("The qbetaMix has the correct numeric result", { # TODO ask if "number" more accurate + result <- qbetaMix( + q = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), + weights = c(0.6, 0.4) + ) + expect_equal(result, 0.488759, tolerance = 1e9) +}) + +test_that("The qbetaMix has the correct numeric result", { + result <- qbetaMix(q = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4)) + expect_numeric(result) +}) From 0b361e34384c77befa70a1cc2cfe0320585b1c70 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Sun, 27 Aug 2023 15:49:02 +0200 Subject: [PATCH 09/46] test_thats for pbetaMix and qbetaMix --- R/dbetabinom.R | 19 ++++++---- examples/pbetaMix.R | 2 -- examples/qbetaMix.R | 9 +++-- man/pbetaMix.Rd | 25 +++++++++++-- man/qbetaMix.Rd | 28 +++++++++++++-- tests/testthat/test-dbetabinom.R | 60 +++++++++++++++++++++++--------- 6 files changed, 113 insertions(+), 30 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 5cb72228..d89e9461 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -149,11 +149,11 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' #' @description `r lifecycle::badge("experimental")` #' -#' Note that `x` can be a vector. +#' Calculates the cdf of the beta-mixture #' #' @typed x : number #' the abscissa. -#' @typed par : matrix or array +#' @typed par : matrix #' the beta parameters matrix, with K rows and 2 columns, #' corresponding to the beta parameters of the K components. #' @typed weights : matrix @@ -163,11 +163,15 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' and otherwise `P[X > x]`. #' @return the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". #' +#' @note `x` can be a vector. +#' +#' @example examples/pbetaMix.R #' @export pbetaMix <- function(x, par, weights, lower.tail = TRUE) { assert_numeric(x, lower = 0, finite = TRUE) assert_numeric(weights, lower = 0, upper = 1, finite = TRUE) assert_vector(par) + assert_flag(lower.tail) ret <- sum(weights * pbeta(x, par[, 1], par[, 2], lower.tail = lower.tail)) ret } @@ -176,9 +180,9 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x") #' Beta-mixture quantile function #' -#' @description `r lifecycle::badge("experimental")` +#' @description `r lifecycle::badge("experimental")` #' -#' Note that `x` can be a vector. +#' Calculates the quantile where x support is at the intersection of cdf and quantile function at chosen quantile/s #' #' @typed q : numeric #' the required quantile. @@ -187,16 +191,19 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x") #' corresponding to the beta parameters of the K components. #' @typed weights : matrix #' the mixture weights of the beta mixture prior. +#' @typed lower.tail : flag +#' whether cdf at x taken at lower or upper tail #' @return the abscissa. #' +#' @example examples/qbetaMix.R #' @export qbetaMix <- function(q, par, weights, lower.tail) { f <- function(pi) { + assert_numeric(pi, lower = 0, finite = TRUE) pbetaMix(x = pi, par = par, weights = weights) - q } - assert_number(f, lower = 0, upper = 1, finite = TRUE) unirootResult <- uniroot(f, lower = 0, upper = 1) - assert_number(unirootResult, lower = 0, upper = 1, finite = TRUE) + assert_number(unirootResult$f.root) if (unirootResult$iter < 0) { NA } else { diff --git a/examples/pbetaMix.R b/examples/pbetaMix.R index 422b1cf6..c02ea51b 100644 --- a/examples/pbetaMix.R +++ b/examples/pbetaMix.R @@ -4,8 +4,6 @@ pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE) ## 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 pbetaMix( x = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) diff --git a/examples/qbetaMix.R b/examples/qbetaMix.R index 81e1a2e7..c1c21cca 100644 --- a/examples/qbetaMix.R +++ b/examples/qbetaMix.R @@ -1,5 +1,9 @@ ## Only 1 mixture component, i.e., weights = 1 -qbetaMix(q = 0.60, par = rbind(c(0.2, 0.4)), weights = 1) +qbetaMix( + q = 0.60, + par = rbind(c(0.2, 0.4)), + weights = 1 +) ## With 2 mixture components qbetaMix( @@ -9,6 +13,7 @@ qbetaMix( ## Can also specify q as a vector qbetaMix( - q = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), + q = seq(0, 1, .01), + par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) diff --git a/man/pbetaMix.Rd b/man/pbetaMix.Rd index ffd8ec96..a37a69c0 100644 --- a/man/pbetaMix.Rd +++ b/man/pbetaMix.Rd @@ -9,7 +9,7 @@ pbetaMix(x, par, weights, lower.tail = TRUE) \arguments{ \item{x}{(\code{number}):\cr the abscissa.} -\item{par}{(\verb{matrix or array}):\cr the beta parameters matrix, with K rows and 2 columns, +\item{par}{(\code{matrix}):\cr the beta parameters matrix, with K rows and 2 columns, corresponding to the beta parameters of the K components.} \item{weights}{(\code{matrix}):\cr the mixture weights of the beta mixture prior.} @@ -23,5 +23,26 @@ the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". \description{ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} -Note that \code{x} can be a vector. +Calculates the cdf of the beta-mixture +} +\note{ +\code{x} can be a vector. +} +\examples{ +pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) + +## Can get the one minus CDF values +pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE) + +## With 2 mixture components +pbetaMix( + x = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)), + weights = c(0.6, 0.4) +) + +## Can also specify x as a vector. +pbetaMix( + x = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), + weights = c(0.6, 0.4) +) } diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index c27ee6d9..0bd15540 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -13,12 +13,36 @@ qbetaMix(q, par, weights, lower.tail) corresponding to the beta parameters of the K components.} \item{weights}{(\code{matrix}):\cr the mixture weights of the beta mixture prior.} + +\item{lower.tail}{(\code{flag}):\cr whether cdf at x taken at lower or upper tail} } \value{ the abscissa. } \description{ -\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} +@description \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} +} +\details{ +Calculates the quantile where x support is at the intersection of cdf and quantile function at chosen quantile/s +} +\examples{ +## Only 1 mixture component, i.e., weights = 1 +qbetaMix( + q = 0.60, + par = rbind(c(0.2, 0.4)), + weights = 1 +) + +## With 2 mixture components +qbetaMix( + q = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), + weights = c(0.6, 0.4) +) -Note that \code{x} can be a vector. +## Can also specify q as a vector +qbetaMix( + q = seq(0, 1, .01), + par = rbind(c(0.2, 0.4), c(1, 1)), + weights = c(0.6, 0.4) +) } diff --git a/tests/testthat/test-dbetabinom.R b/tests/testthat/test-dbetabinom.R index 82f1c3f7..a02b7086 100644 --- a/tests/testthat/test-dbetabinom.R +++ b/tests/testthat/test-dbetabinom.R @@ -62,41 +62,69 @@ test_that("Beta mixture density has the correct numeric result", { ## pbetaMix ---- test_that("The pbetaMix has incrementally higher cdf with increase x support", { - is_lower <- pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) - is_higher <- pbetaMix(x = 0.5, par = rbind(c(0.2, 0.4)), weights = 1) + is_lower <- pbetaMix( + x = 0.3, + par = rbind(c(0.2, 0.4)), + weights = 1 + ) + is_higher <- pbetaMix( + x = 0.5, + par = rbind(c(0.2, 0.4)), + weights = 1 + ) expect_true(is_lower < is_higher) }) -test_that("The pbetaMix has the correct numeric result", { +test_that("The pbetaMix has the correct number result", { result <- pbetaMix( - x = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)), + x = 0.3, + par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) - expect_equal(result, 0.4768404, tolerance = 1e9) + expect_equal(result, 0.4768404, tolerance = 1e-5) }) test_that("The complement of pbetaMix can be derived with a different lower.tail flag", { - result <- pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE) - result_inversed <- pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = TRUE) - expect_equal(result, 1 - result_inversed, tolerance = 1e9) + result <- pbetaMix( + x = 0.3, + par = rbind(c(0.2, 0.4)), + weights = 1, + lower.tail = FALSE + ) + result_inversed <- pbetaMix( + x = 0.3, + par = rbind(c(0.2, 0.4)), + weights = 1, + lower.tail = TRUE + ) + expect_equal(result, 1 - result_inversed, tolerance = 1e-5) }) ## qbetaMix ---- -test_that("The qbetaMix has the correct numeric result", { # TODO ask if "number" more accurate - result <- qbetaMix(q = 0.60, par = rbind(c(0.2, 0.4)), weights = 1) - expect_equal(result, 0.3112065, tolerance = 1e9) +test_that("The qbetaMix has the correct number result", { + result <- qbetaMix( + q = 0.6, + par = rbind(c(0.2, 0.4)), + weights = 1 + ) + expect_equal(result, 0.3112068, tolerance = 1e-6) }) -test_that("The qbetaMix has the correct numeric result", { # TODO ask if "number" more accurate +test_that("The qbetaMix has the correct number result", { result <- qbetaMix( - q = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), + q = 0.6, + par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) - expect_equal(result, 0.488759, tolerance = 1e9) + expect_equal(result, 0.488759, tolerance = 1e-6) }) -test_that("The qbetaMix has the correct numeric result", { - result <- qbetaMix(q = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4)) +test_that("The qbetaMix has a number result", { + result <- qbetaMix( + q = seq(0, 1, .01), + par = rbind(c(0.2, 0.4), c(1, 1)), + weights = c(0.6, 0.4) + ) expect_numeric(result) }) From 8b20f728559ed15c70feff72b928512b15fbde10 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 28 Aug 2023 09:07:34 +0200 Subject: [PATCH 10/46] tidy syntax and last checks --- R/dbetabinom.R | 4 ++-- man/qbetaMix.Rd | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index d89e9461..34e30f03 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -170,7 +170,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") pbetaMix <- function(x, par, weights, lower.tail = TRUE) { assert_numeric(x, lower = 0, finite = TRUE) assert_numeric(weights, lower = 0, upper = 1, finite = TRUE) - assert_vector(par) + assert_matrix(par) assert_flag(lower.tail) ret <- sum(weights * pbeta(x, par[, 1], par[, 2], lower.tail = lower.tail)) ret @@ -182,7 +182,7 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x") #' #' @description `r lifecycle::badge("experimental")` #' -#' Calculates the quantile where x support is at the intersection of cdf and quantile function at chosen quantile/s +#' Calculates the quantile where x support is at the intersection of cdf and quantile function #' #' @typed q : numeric #' the required quantile. diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index 0bd15540..33f1c819 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -23,7 +23,7 @@ the abscissa. @description \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} } \details{ -Calculates the quantile where x support is at the intersection of cdf and quantile function at chosen quantile/s +Calculates the quantile where x support is at the intersection of cdf and quantile function } \examples{ ## Only 1 mixture component, i.e., weights = 1 From b0c22187e74b6b15c288664398c74532688ba7dd Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 28 Aug 2023 09:07:34 +0200 Subject: [PATCH 11/46] tidy syntax and last checks --- R/dbetabinom.R | 4 ++-- man/qbetaMix.Rd | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index d89e9461..34e30f03 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -170,7 +170,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") pbetaMix <- function(x, par, weights, lower.tail = TRUE) { assert_numeric(x, lower = 0, finite = TRUE) assert_numeric(weights, lower = 0, upper = 1, finite = TRUE) - assert_vector(par) + assert_matrix(par) assert_flag(lower.tail) ret <- sum(weights * pbeta(x, par[, 1], par[, 2], lower.tail = lower.tail)) ret @@ -182,7 +182,7 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x") #' #' @description `r lifecycle::badge("experimental")` #' -#' Calculates the quantile where x support is at the intersection of cdf and quantile function at chosen quantile/s +#' Calculates the quantile where x support is at the intersection of cdf and quantile function #' #' @typed q : numeric #' the required quantile. diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index 0bd15540..33f1c819 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -23,7 +23,7 @@ the abscissa. @description \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} } \details{ -Calculates the quantile where x support is at the intersection of cdf and quantile function at chosen quantile/s +Calculates the quantile where x support is at the intersection of cdf and quantile function } \examples{ ## Only 1 mixture component, i.e., weights = 1 From 5384c3d3d9245251a79410b5a2c66258c1b43a7f Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 28 Aug 2023 14:26:04 +0200 Subject: [PATCH 12/46] Update R/dbetabinom.R Co-authored-by: Daniel Sabanes Bove --- R/dbetabinom.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 34e30f03..7aa49165 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -145,7 +145,7 @@ dbetaMix <- function(x, par, weights, log = FALSE) { dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") -#' Beta-mixture cdf +#' Beta-Mixture CDF #' #' @description `r lifecycle::badge("experimental")` #' From f5403fa28c44602bccaccc8f0565bfb31094eaf0 Mon Sep 17 00:00:00 2001 From: "27856297+dependabot-preview[bot]@users.noreply.github.com" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 28 Aug 2023 12:29:02 +0000 Subject: [PATCH 13/46] [skip actions] Roxygen Man Pages Auto Update --- man/pbetaMix.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/pbetaMix.Rd b/man/pbetaMix.Rd index a37a69c0..2d6d0d68 100644 --- a/man/pbetaMix.Rd +++ b/man/pbetaMix.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/dbetabinom.R \name{pbetaMix} \alias{pbetaMix} -\title{Beta-mixture cdf} +\title{Beta-Mixture CDF} \usage{ pbetaMix(x, par, weights, lower.tail = TRUE) } From 0dd2bab848bed856b3f1cc73e83eaf4138a91606 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 28 Aug 2023 14:29:25 +0200 Subject: [PATCH 14/46] Update R/dbetabinom.R Co-authored-by: Daniel Sabanes Bove --- R/dbetabinom.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 7aa49165..e4bd16e7 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -149,7 +149,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' #' @description `r lifecycle::badge("experimental")` #' -#' Calculates the cdf of the beta-mixture +#' Calculates the cdf of the beta-mixture distribution. #' #' @typed x : number #' the abscissa. From ca0236f6ffbf391dc604792e534a3093dbf0d8c5 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 28 Aug 2023 14:29:32 +0200 Subject: [PATCH 15/46] Update R/dbetabinom.R Co-authored-by: Daniel Sabanes Bove --- R/dbetabinom.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index e4bd16e7..06e7c895 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -157,7 +157,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' the beta parameters matrix, with K rows and 2 columns, #' corresponding to the beta parameters of the K components. #' @typed weights : matrix -#' the mixture weights of the beta mixture prior. +#' the mixture weights of the beta mixture prior which add up to 1. #' @typed lower.tail : logical # TODO Why not flag #' if TRUE (default), probabilities are `P[X <= x]`, #' and otherwise `P[X > x]`. From 9cf54d0a3e952492e3e7cf8fc9003c493b55256c Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 28 Aug 2023 14:29:37 +0200 Subject: [PATCH 16/46] Update R/dbetabinom.R Co-authored-by: Daniel Sabanes Bove --- R/dbetabinom.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 06e7c895..1a180fb1 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -156,7 +156,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' @typed par : matrix #' the beta parameters matrix, with K rows and 2 columns, #' corresponding to the beta parameters of the K components. -#' @typed weights : matrix +#' @typed weights : numeric #' the mixture weights of the beta mixture prior which add up to 1. #' @typed lower.tail : logical # TODO Why not flag #' if TRUE (default), probabilities are `P[X <= x]`, From 2eb3bd215a02faa4c2ccb13e96b1e5209013f04d Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 28 Aug 2023 14:29:46 +0200 Subject: [PATCH 17/46] Update R/dbetabinom.R Co-authored-by: Daniel Sabanes Bove --- R/dbetabinom.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 1a180fb1..0b52ff1e 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -158,7 +158,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' corresponding to the beta parameters of the K components. #' @typed weights : numeric #' the mixture weights of the beta mixture prior which add up to 1. -#' @typed lower.tail : logical # TODO Why not flag +#' @typed lower.tail : flag #' if TRUE (default), probabilities are `P[X <= x]`, #' and otherwise `P[X > x]`. #' @return the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". From c08435fdda15fc50684d8afe573a226d97c7944c Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 28 Aug 2023 14:30:32 +0200 Subject: [PATCH 18/46] Update R/dbetabinom.R Co-authored-by: Daniel Sabanes Bove --- R/dbetabinom.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 0b52ff1e..d0ec16ed 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -203,7 +203,6 @@ qbetaMix <- function(q, par, weights, lower.tail) { pbetaMix(x = pi, par = par, weights = weights) - q } unirootResult <- uniroot(f, lower = 0, upper = 1) - assert_number(unirootResult$f.root) if (unirootResult$iter < 0) { NA } else { From e36facd305865a5f8dcca505db864c880fe2675f Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 28 Aug 2023 14:30:41 +0200 Subject: [PATCH 19/46] Update R/dbetabinom.R Co-authored-by: Daniel Sabanes Bove --- R/dbetabinom.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index d0ec16ed..e03f6cd3 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -206,6 +206,7 @@ qbetaMix <- function(q, par, weights, lower.tail) { if (unirootResult$iter < 0) { NA } else { + assert_number(unirootResult$root) unirootResult$root } } From f45d2f04dae9cdd8425019cf450f180949ede8c2 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 28 Aug 2023 14:30:46 +0200 Subject: [PATCH 20/46] Update R/dbetabinom.R Co-authored-by: Daniel Sabanes Bove --- R/dbetabinom.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index e03f6cd3..92bb60c9 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -210,4 +210,4 @@ qbetaMix <- function(q, par, weights, lower.tail) { unirootResult$root } } -qbetaMix <- Vectorize(qbetaMix, vectorize.args = "q") +qbetaMix <- Vectorize(qbetaMix, vectorize.args = "p") From 0505982a6aed333a0a7ac6ec23c34fde6f211dc4 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Mon, 28 Aug 2023 14:30:54 +0200 Subject: [PATCH 21/46] Update examples/pbetaMix.R Co-authored-by: Daniel Sabanes Bove --- examples/pbetaMix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pbetaMix.R b/examples/pbetaMix.R index c02ea51b..5d75722d 100644 --- a/examples/pbetaMix.R +++ b/examples/pbetaMix.R @@ -1,6 +1,6 @@ pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) -## Can get the one minus CDF values +# Can get the one minus CDF values. pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE) ## With 2 mixture components From 469ed6ef81f6c9e06c22ae8f9fe3035b60e38268 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 29 Aug 2023 11:29:08 +0200 Subject: [PATCH 22/46] p and q inputs --- R/dbetabinom.R | 15 +++++++-------- examples/pbetaMix.R | 8 ++++---- examples/qbetaMix.R | 6 +++--- man/pbetaMix.Rd | 22 +++++++++++----------- man/qbetaMix.Rd | 12 ++++++------ tests/testthat/test-dbetabinom.R | 16 ++++++++-------- 6 files changed, 39 insertions(+), 40 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 92bb60c9..aec0758b 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -167,15 +167,15 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' #' @example examples/pbetaMix.R #' @export -pbetaMix <- function(x, par, weights, lower.tail = TRUE) { - assert_numeric(x, lower = 0, finite = TRUE) +pbetaMix <- function(q, par, weights, lower.tail = TRUE) { + assert_numeric(q, lower = 0, finite = TRUE) assert_numeric(weights, lower = 0, upper = 1, finite = TRUE) assert_matrix(par) assert_flag(lower.tail) - ret <- sum(weights * pbeta(x, par[, 1], par[, 2], lower.tail = lower.tail)) + ret <- sum(weights * pbeta(q, par[, 1], par[, 2], lower.tail = lower.tail)) ret } -pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x") +pbetaMix <- Vectorize(pbetaMix, vectorize.args = "q") #' Beta-mixture quantile function @@ -197,10 +197,9 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x") #' #' @example examples/qbetaMix.R #' @export -qbetaMix <- function(q, par, weights, lower.tail) { +qbetaMix <- function(qt, par, weights, lower.tail = TRUE) { f <- function(pi) { - assert_numeric(pi, lower = 0, finite = TRUE) - pbetaMix(x = pi, par = par, weights = weights) - q + pbetaMix(q = pi, par = par, weights = weights, lower.tail = lower.tail) - qt } unirootResult <- uniroot(f, lower = 0, upper = 1) if (unirootResult$iter < 0) { @@ -210,4 +209,4 @@ qbetaMix <- function(q, par, weights, lower.tail) { unirootResult$root } } -qbetaMix <- Vectorize(qbetaMix, vectorize.args = "p") +qbetaMix <- Vectorize(qbetaMix, vectorize.args = "qt") diff --git a/examples/pbetaMix.R b/examples/pbetaMix.R index 5d75722d..15e7f13c 100644 --- a/examples/pbetaMix.R +++ b/examples/pbetaMix.R @@ -1,16 +1,16 @@ -pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) +pbetaMix(q = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) # Can get the one minus CDF values. -pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE) +pbetaMix(q = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE) ## With 2 mixture components pbetaMix( - x = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)), + q = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) ## Can also specify x as a vector. pbetaMix( - x = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), + q = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) diff --git a/examples/qbetaMix.R b/examples/qbetaMix.R index c1c21cca..aea0eb83 100644 --- a/examples/qbetaMix.R +++ b/examples/qbetaMix.R @@ -1,19 +1,19 @@ ## Only 1 mixture component, i.e., weights = 1 qbetaMix( - q = 0.60, + qt = 0.60, par = rbind(c(0.2, 0.4)), weights = 1 ) ## With 2 mixture components qbetaMix( - q = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), + qt = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) ## Can also specify q as a vector qbetaMix( - q = seq(0, 1, .01), + qt = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) diff --git a/man/pbetaMix.Rd b/man/pbetaMix.Rd index 2d6d0d68..ab2d4661 100644 --- a/man/pbetaMix.Rd +++ b/man/pbetaMix.Rd @@ -4,18 +4,18 @@ \alias{pbetaMix} \title{Beta-Mixture CDF} \usage{ -pbetaMix(x, par, weights, lower.tail = TRUE) +pbetaMix(q, par, weights, lower.tail = TRUE) } \arguments{ -\item{x}{(\code{number}):\cr the abscissa.} - \item{par}{(\code{matrix}):\cr the beta parameters matrix, with K rows and 2 columns, corresponding to the beta parameters of the K components.} -\item{weights}{(\code{matrix}):\cr the mixture weights of the beta mixture prior.} +\item{weights}{(\code{numeric}):\cr the mixture weights of the beta mixture prior which add up to 1.} -\item{lower.tail}{(\code{logical # TODO Why not flag}):\cr if TRUE (default), probabilities are \code{P[X <= x]}, +\item{lower.tail}{(\code{flag}):\cr if TRUE (default), probabilities are \code{P[X <= x]}, and otherwise \code{P[X > x]}.} + +\item{x}{(\code{number}):\cr the abscissa.} } \value{ the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". @@ -23,26 +23,26 @@ the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". \description{ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} -Calculates the cdf of the beta-mixture +Calculates the cdf of the beta-mixture distribution. } \note{ \code{x} can be a vector. } \examples{ -pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) +pbetaMix(q = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) -## Can get the one minus CDF values -pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE) +# Can get the one minus CDF values. +pbetaMix(q = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE) ## With 2 mixture components pbetaMix( - x = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)), + q = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) ## Can also specify x as a vector. pbetaMix( - x = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), + q = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) } diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index 33f1c819..9393cbaf 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -4,17 +4,17 @@ \alias{qbetaMix} \title{Beta-mixture quantile function} \usage{ -qbetaMix(q, par, weights, lower.tail) +qbetaMix(qt, par, weights, lower.tail = TRUE) } \arguments{ -\item{q}{(\code{numeric}):\cr the required quantile.} - \item{par}{(\code{number}):\cr the beta parameters matrix, with K rows and 2 columns, corresponding to the beta parameters of the K components.} \item{weights}{(\code{matrix}):\cr the mixture weights of the beta mixture prior.} \item{lower.tail}{(\code{flag}):\cr whether cdf at x taken at lower or upper tail} + +\item{q}{(\code{numeric}):\cr the required quantile.} } \value{ the abscissa. @@ -28,20 +28,20 @@ Calculates the quantile where x support is at the intersection of cdf and quanti \examples{ ## Only 1 mixture component, i.e., weights = 1 qbetaMix( - q = 0.60, + qt = 0.60, par = rbind(c(0.2, 0.4)), weights = 1 ) ## With 2 mixture components qbetaMix( - q = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), + qt = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) ## Can also specify q as a vector qbetaMix( - q = seq(0, 1, .01), + qt = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) diff --git a/tests/testthat/test-dbetabinom.R b/tests/testthat/test-dbetabinom.R index a02b7086..eb4720ff 100644 --- a/tests/testthat/test-dbetabinom.R +++ b/tests/testthat/test-dbetabinom.R @@ -63,12 +63,12 @@ test_that("Beta mixture density has the correct numeric result", { test_that("The pbetaMix has incrementally higher cdf with increase x support", { is_lower <- pbetaMix( - x = 0.3, + q = 0.3, par = rbind(c(0.2, 0.4)), weights = 1 ) is_higher <- pbetaMix( - x = 0.5, + q = 0.5, par = rbind(c(0.2, 0.4)), weights = 1 ) @@ -77,7 +77,7 @@ test_that("The pbetaMix has incrementally higher cdf with increase x support", { test_that("The pbetaMix has the correct number result", { result <- pbetaMix( - x = 0.3, + q = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) @@ -86,13 +86,13 @@ test_that("The pbetaMix has the correct number result", { test_that("The complement of pbetaMix can be derived with a different lower.tail flag", { result <- pbetaMix( - x = 0.3, + q = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE ) result_inversed <- pbetaMix( - x = 0.3, + q = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = TRUE @@ -104,7 +104,7 @@ test_that("The complement of pbetaMix can be derived with a different lower.tail test_that("The qbetaMix has the correct number result", { result <- qbetaMix( - q = 0.6, + qt = 0.6, par = rbind(c(0.2, 0.4)), weights = 1 ) @@ -113,7 +113,7 @@ test_that("The qbetaMix has the correct number result", { test_that("The qbetaMix has the correct number result", { result <- qbetaMix( - q = 0.6, + qt = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) @@ -122,7 +122,7 @@ test_that("The qbetaMix has the correct number result", { test_that("The qbetaMix has a number result", { result <- qbetaMix( - q = seq(0, 1, .01), + qt = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) From 1abea5060e1ac37736212fa32b41fec71e048268 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 29 Aug 2023 11:36:45 +0200 Subject: [PATCH 23/46] q,p changes --- R/dbetabinom.R | 17 ++++++++--------- man/pbetaMix.Rd | 4 ++-- man/qbetaMix.Rd | 8 ++++---- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index aec0758b..c2b7ea1c 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -159,33 +159,32 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' @typed weights : numeric #' the mixture weights of the beta mixture prior which add up to 1. #' @typed lower.tail : flag -#' if TRUE (default), probabilities are `P[X <= x]`, +#' if `TRUE` (default), probabilities are `P[X <= x]`, #' and otherwise `P[X > x]`. -#' @return the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". +#' @return The (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". #' #' @note `x` can be a vector. #' #' @example examples/pbetaMix.R #' @export pbetaMix <- function(q, par, weights, lower.tail = TRUE) { - assert_numeric(q, lower = 0, finite = TRUE) + assert_number(q, lower = 0, finite = TRUE) assert_numeric(weights, lower = 0, upper = 1, finite = TRUE) assert_matrix(par) assert_flag(lower.tail) - ret <- sum(weights * pbeta(q, par[, 1], par[, 2], lower.tail = lower.tail)) - ret + sum(weights * pbeta(q, par[, 1], par[, 2], lower.tail = lower.tail)) } pbetaMix <- Vectorize(pbetaMix, vectorize.args = "q") -#' Beta-mixture quantile function +#' Beta-Mixture Quantile function #' #' @description `r lifecycle::badge("experimental")` #' -#' Calculates the quantile where x support is at the intersection of cdf and quantile function +#' Calculates the quantile of the Beta-Mixture distribution for a given probability. #' -#' @typed q : numeric -#' the required quantile. +#' @typed qt : numeric +#' the required probability #' @typed par : number #' the beta parameters matrix, with K rows and 2 columns, #' corresponding to the beta parameters of the K components. diff --git a/man/pbetaMix.Rd b/man/pbetaMix.Rd index ab2d4661..1938692d 100644 --- a/man/pbetaMix.Rd +++ b/man/pbetaMix.Rd @@ -12,13 +12,13 @@ corresponding to the beta parameters of the K components.} \item{weights}{(\code{numeric}):\cr the mixture weights of the beta mixture prior which add up to 1.} -\item{lower.tail}{(\code{flag}):\cr if TRUE (default), probabilities are \code{P[X <= x]}, +\item{lower.tail}{(\code{flag}):\cr if \code{TRUE} (default), probabilities are \code{P[X <= x]}, and otherwise \code{P[X > x]}.} \item{x}{(\code{number}):\cr the abscissa.} } \value{ -the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". +The (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". } \description{ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index 9393cbaf..5bda8ba7 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -2,19 +2,19 @@ % Please edit documentation in R/dbetabinom.R \name{qbetaMix} \alias{qbetaMix} -\title{Beta-mixture quantile function} +\title{Beta-Mixture Quantile function} \usage{ qbetaMix(qt, par, weights, lower.tail = TRUE) } \arguments{ +\item{qt}{(\code{numeric}):\cr the required probability} + \item{par}{(\code{number}):\cr the beta parameters matrix, with K rows and 2 columns, corresponding to the beta parameters of the K components.} \item{weights}{(\code{matrix}):\cr the mixture weights of the beta mixture prior.} \item{lower.tail}{(\code{flag}):\cr whether cdf at x taken at lower or upper tail} - -\item{q}{(\code{numeric}):\cr the required quantile.} } \value{ the abscissa. @@ -23,7 +23,7 @@ the abscissa. @description \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} } \details{ -Calculates the quantile where x support is at the intersection of cdf and quantile function +Calculates the quantile of the Beta-Mixture distribution for a given probability. } \examples{ ## Only 1 mixture component, i.e., weights = 1 From b7b523c10be18f2d3ebf57fc9bebf83e512bdc41 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 29 Aug 2023 11:39:00 +0200 Subject: [PATCH 24/46] Update R/dbetabinom.R Co-authored-by: Daniel Sabanes Bove --- R/dbetabinom.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index c2b7ea1c..efd2334a 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -179,7 +179,7 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "q") #' Beta-Mixture Quantile function #' -#' @description `r lifecycle::badge("experimental")` +#' @description `r lifecycle::badge("experimental")` #' #' Calculates the quantile of the Beta-Mixture distribution for a given probability. #' From 34a663bdabd7bce594b7e01551c2a99327f57c36 Mon Sep 17 00:00:00 2001 From: "27856297+dependabot-preview[bot]@users.noreply.github.com" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Tue, 29 Aug 2023 09:41:50 +0000 Subject: [PATCH 25/46] [skip actions] Roxygen Man Pages Auto Update --- man/qbetaMix.Rd | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index 5bda8ba7..7f2cf18d 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -20,9 +20,8 @@ corresponding to the beta parameters of the K components.} the abscissa. } \description{ -@description \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} -} -\details{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} + Calculates the quantile of the Beta-Mixture distribution for a given probability. } \examples{ From f384100c0b2c1b274684c4d02aaebfa2e9e4f100 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 29 Aug 2023 11:49:28 +0200 Subject: [PATCH 26/46] documentation to reflect p and q changes --- R/dbetabinom.R | 12 ++++++------ man/pbetaMix.Rd | 8 ++++---- man/qbetaMix.Rd | 9 ++++----- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index efd2334a..68a9ec41 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -149,9 +149,9 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' #' @description `r lifecycle::badge("experimental")` #' -#' Calculates the cdf of the beta-mixture distribution. +#' Calculates the CDF of the Beta-Mixture distribution. #' -#' @typed x : number +#' @typed q : number #' the abscissa. #' @typed par : matrix #' the beta parameters matrix, with K rows and 2 columns, @@ -163,7 +163,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' and otherwise `P[X > x]`. #' @return The (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". #' -#' @note `x` can be a vector. +#' @note `q` can be a vector. #' #' @example examples/pbetaMix.R #' @export @@ -181,7 +181,7 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "q") #' #' @description `r lifecycle::badge("experimental")` #' -#' Calculates the quantile of the Beta-Mixture distribution for a given probability. +#' Calculates the quantile of the Beta-Mixture distribution for a given probability. #' #' @typed qt : numeric #' the required probability @@ -191,8 +191,8 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "q") #' @typed weights : matrix #' the mixture weights of the beta mixture prior. #' @typed lower.tail : flag -#' whether cdf at x taken at lower or upper tail -#' @return the abscissa. +#' whether CDF at x taken at lower or upper tail +#' @return The abscissa. #' #' @example examples/qbetaMix.R #' @export diff --git a/man/pbetaMix.Rd b/man/pbetaMix.Rd index 1938692d..40bbe85f 100644 --- a/man/pbetaMix.Rd +++ b/man/pbetaMix.Rd @@ -7,6 +7,8 @@ pbetaMix(q, par, weights, lower.tail = TRUE) } \arguments{ +\item{q}{(\code{number}):\cr the abscissa.} + \item{par}{(\code{matrix}):\cr the beta parameters matrix, with K rows and 2 columns, corresponding to the beta parameters of the K components.} @@ -14,8 +16,6 @@ corresponding to the beta parameters of the K components.} \item{lower.tail}{(\code{flag}):\cr if \code{TRUE} (default), probabilities are \code{P[X <= x]}, and otherwise \code{P[X > x]}.} - -\item{x}{(\code{number}):\cr the abscissa.} } \value{ The (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". @@ -23,10 +23,10 @@ The (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". \description{ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} -Calculates the cdf of the beta-mixture distribution. +Calculates the CDF of the Beta-Mixture distribution. } \note{ -\code{x} can be a vector. +\code{q} can be a vector. } \examples{ pbetaMix(q = 0.3, par = rbind(c(0.2, 0.4)), weights = 1) diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index 5bda8ba7..11b56c4c 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -14,15 +14,14 @@ corresponding to the beta parameters of the K components.} \item{weights}{(\code{matrix}):\cr the mixture weights of the beta mixture prior.} -\item{lower.tail}{(\code{flag}):\cr whether cdf at x taken at lower or upper tail} +\item{lower.tail}{(\code{flag}):\cr whether CDF at x taken at lower or upper tail} } \value{ -the abscissa. +The abscissa. } \description{ -@description \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} -} -\details{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} + Calculates the quantile of the Beta-Mixture distribution for a given probability. } \examples{ From b9e50b258d5a89bb849e3786584b85e369b07adc Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 29 Aug 2023 14:47:49 +0200 Subject: [PATCH 27/46] amending qt --- R/dbetabinom.R | 8 ++++---- examples/qbetaMix.R | 6 +++--- man/pbetaMix.Rd | 2 +- man/qbetaMix.Rd | 12 ++++++------ tests/testthat/test-dbetabinom.R | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 68a9ec41..21dc983a 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -161,7 +161,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' @typed lower.tail : flag #' if `TRUE` (default), probabilities are `P[X <= x]`, #' and otherwise `P[X > x]`. -#' @return The (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". +#' @return The (one minus) cdf value #' #' @note `q` can be a vector. #' @@ -196,9 +196,9 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "q") #' #' @example examples/qbetaMix.R #' @export -qbetaMix <- function(qt, par, weights, lower.tail = TRUE) { +qbetaMix <- function(p, par, weights, lower.tail = TRUE) { f <- function(pi) { - pbetaMix(q = pi, par = par, weights = weights, lower.tail = lower.tail) - qt + pbetaMix(q = pi, par = par, weights = weights, lower.tail = lower.tail) - p } unirootResult <- uniroot(f, lower = 0, upper = 1) if (unirootResult$iter < 0) { @@ -208,4 +208,4 @@ qbetaMix <- function(qt, par, weights, lower.tail = TRUE) { unirootResult$root } } -qbetaMix <- Vectorize(qbetaMix, vectorize.args = "qt") +qbetaMix <- Vectorize(qbetaMix, vectorize.args = "p") diff --git a/examples/qbetaMix.R b/examples/qbetaMix.R index aea0eb83..359c7759 100644 --- a/examples/qbetaMix.R +++ b/examples/qbetaMix.R @@ -1,19 +1,19 @@ ## Only 1 mixture component, i.e., weights = 1 qbetaMix( - qt = 0.60, + p = 0.60, par = rbind(c(0.2, 0.4)), weights = 1 ) ## With 2 mixture components qbetaMix( - qt = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), + p = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) ## Can also specify q as a vector qbetaMix( - qt = seq(0, 1, .01), + p = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) diff --git a/man/pbetaMix.Rd b/man/pbetaMix.Rd index 40bbe85f..07708b0c 100644 --- a/man/pbetaMix.Rd +++ b/man/pbetaMix.Rd @@ -18,7 +18,7 @@ corresponding to the beta parameters of the K components.} and otherwise \code{P[X > x]}.} } \value{ -The (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-". +The (one minus) cdf value } \description{ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index 11b56c4c..7dbbdaf1 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -4,17 +4,17 @@ \alias{qbetaMix} \title{Beta-Mixture Quantile function} \usage{ -qbetaMix(qt, par, weights, lower.tail = TRUE) +qbetaMix(p, par, weights, lower.tail = TRUE) } \arguments{ -\item{qt}{(\code{numeric}):\cr the required probability} - \item{par}{(\code{number}):\cr the beta parameters matrix, with K rows and 2 columns, corresponding to the beta parameters of the K components.} \item{weights}{(\code{matrix}):\cr the mixture weights of the beta mixture prior.} \item{lower.tail}{(\code{flag}):\cr whether CDF at x taken at lower or upper tail} + +\item{qt}{(\code{numeric}):\cr the required probability} } \value{ The abscissa. @@ -27,20 +27,20 @@ Calculates the quantile of the Beta-Mixture distribution for a given probability \examples{ ## Only 1 mixture component, i.e., weights = 1 qbetaMix( - qt = 0.60, + p = 0.60, par = rbind(c(0.2, 0.4)), weights = 1 ) ## With 2 mixture components qbetaMix( - qt = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), + p = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) ## Can also specify q as a vector qbetaMix( - qt = seq(0, 1, .01), + p = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) diff --git a/tests/testthat/test-dbetabinom.R b/tests/testthat/test-dbetabinom.R index eb4720ff..c57e2fd4 100644 --- a/tests/testthat/test-dbetabinom.R +++ b/tests/testthat/test-dbetabinom.R @@ -104,7 +104,7 @@ test_that("The complement of pbetaMix can be derived with a different lower.tail test_that("The qbetaMix has the correct number result", { result <- qbetaMix( - qt = 0.6, + p = 0.6, par = rbind(c(0.2, 0.4)), weights = 1 ) @@ -113,7 +113,7 @@ test_that("The qbetaMix has the correct number result", { test_that("The qbetaMix has the correct number result", { result <- qbetaMix( - qt = 0.6, + p = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) @@ -122,7 +122,7 @@ test_that("The qbetaMix has the correct number result", { test_that("The qbetaMix has a number result", { result <- qbetaMix( - qt = seq(0, 1, .01), + p = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)), weights = c(0.6, 0.4) ) From 0ab047dba04757c8d54264ec9307b93440b35798 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 29 Aug 2023 15:54:58 +0200 Subject: [PATCH 28/46] intro x-> q --- vignettes/introduction.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/introduction.Rmd b/vignettes/introduction.Rmd index 103d09f5..fbacf809 100644 --- a/vignettes/introduction.Rmd +++ b/vignettes/introduction.Rmd @@ -649,7 +649,7 @@ user could calculate the probability that the PET-CR rate is greater than 60%, i $\Pr(P_E > 0.6 \given x)$, by issuing the following command: ```{r Example_1_Postprob, echo=TRUE} -postprob(x = 55, n = 80, p = 0.6, parE = c(5.75, 4.25)) +postprob(q = 55, n = 80, p = 0.6, parE = c(5.75, 4.25)) ``` Here the result indicates that there is a roughly 93% chance that the PET-CR From 4164b4649db14297f429aebb875af37fe76c3e00 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 29 Aug 2023 16:07:44 +0200 Subject: [PATCH 29/46] amented postprob file --- R/postprob.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/postprob.R b/R/postprob.R index 57b4426f..5824b99d 100644 --- a/R/postprob.R +++ b/R/postprob.R @@ -73,7 +73,8 @@ postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALS ## now compute updated parameters betamixPost <- getBetamixPost( - x = x, n = n, + x = x, + n = n, par = parE, weights = weights ) @@ -82,7 +83,7 @@ postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALS ## now compute the survival function at p, i.e. 1 - cdf at p: ret <- with( betamixPost, - pbetaMix(x = p, par = par, weights = weights, lower.tail = FALSE) + pbetaMix(q = p, par = par, weights = weights, lower.tail = FALSE) ) if (log.p) { From 5d98627f4445e747e8901ae938b14fe1a9d50c5d Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 29 Aug 2023 16:07:44 +0200 Subject: [PATCH 30/46] amended postprob file --- R/postprob.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/postprob.R b/R/postprob.R index 57b4426f..5824b99d 100644 --- a/R/postprob.R +++ b/R/postprob.R @@ -73,7 +73,8 @@ postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALS ## now compute updated parameters betamixPost <- getBetamixPost( - x = x, n = n, + x = x, + n = n, par = parE, weights = weights ) @@ -82,7 +83,7 @@ postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALS ## now compute the survival function at p, i.e. 1 - cdf at p: ret <- with( betamixPost, - pbetaMix(x = p, par = par, weights = weights, lower.tail = FALSE) + pbetaMix(q = p, par = par, weights = weights, lower.tail = FALSE) ) if (log.p) { From 205e5740adf84f77eb5b212f57a42276e3854a42 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 29 Aug 2023 16:13:33 +0200 Subject: [PATCH 31/46] fixed mistake on x --- vignettes/introduction.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/introduction.Rmd b/vignettes/introduction.Rmd index fbacf809..103d09f5 100644 --- a/vignettes/introduction.Rmd +++ b/vignettes/introduction.Rmd @@ -649,7 +649,7 @@ user could calculate the probability that the PET-CR rate is greater than 60%, i $\Pr(P_E > 0.6 \given x)$, by issuing the following command: ```{r Example_1_Postprob, echo=TRUE} -postprob(q = 55, n = 80, p = 0.6, parE = c(5.75, 4.25)) +postprob(x = 55, n = 80, p = 0.6, parE = c(5.75, 4.25)) ``` Here the result indicates that there is a roughly 93% chance that the PET-CR From 2d093bad776fcec68ce4efc7546bfe8db117bac9 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Tue, 29 Aug 2023 16:20:45 +0200 Subject: [PATCH 32/46] qbetaMix was in postprobDist --- R/postprobDist.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/postprobDist.R b/R/postprobDist.R index 1edddc90..ce339667 100644 --- a/R/postprobDist.R +++ b/R/postprobDist.R @@ -126,7 +126,7 @@ postprobDist <- function(x, n, bounds <- with( controlBetamixPost, qbetaMix( - q = c(epsilon, 1 - epsilon), + p = c(epsilon, 1 - epsilon), par = par, weights = weights ) From c7dbbf9b70e84804b7088250ff355c7aa70a9f48 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 08:42:14 +0200 Subject: [PATCH 33/46] trying to find R CMD check error --- examples/ocPostprobDist.R | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/examples/ocPostprobDist.R b/examples/ocPostprobDist.R index c9662deb..93b9c75f 100644 --- a/examples/ocPostprobDist.R +++ b/examples/ocPostprobDist.R @@ -11,8 +11,15 @@ res1 <- ocPostprobDist( - nn = c(10, 20, 30), p = 0.4, deltaE = 0.1, deltaF = -0.1, tL = 0.6, tU = 0.6, - parE = c(1, 1), parS = c(5, 25), ns = 100 + nn = c(10, 20, 30), + p = 0.4, + deltaE = 0.1, + deltaF = -0.1, + tL = 0.6, + tU = 0.6, + parE = c(1, 1), + parS = c(5, 25), + ns = 100 ) res1$oc @@ -22,16 +29,32 @@ res1$oc # this call will generate d (distance for random looks around the look locations) # based on "floor(min(nn - c(0,nn[-length(nn)]))/2)" as d is missing: res2 <- ocPostprobDist( - nn = c(10, 20, 30), p = 0.4, deltaE = 0.1, deltaF = -0.1, tL = 0.6, tU = 0.6, - parE = c(1, 1), parS = c(5, 25), ns = 100, nr = TRUE + nn = c(10, 20, 30), + p = 0.4, + deltaE = 0.1, + deltaF = -0.1, + tL = 0.6, + tU = 0.6, + parE = c(1, 1), + parS = c(5, 25), + ns = 100, nr = TRUE ) res2$oc # specify the distance for random looks around the look locations in nn (d=5 for illustration) res3 <- ocPostprobDist( - nn = c(10, 20, 30), p = 0.4, deltaE = 0.1, deltaF = -0.1, tL = 0.6, tU = 0.6, - parE = c(1, 1), parS = c(5, 25), ns = 100, nr = TRUE, d = 5 + nn = c(10, 20, 30), + p = 0.4, + deltaE = 0.1, + deltaF = -0.1, + tL = 0.6, + tU = 0.6, + parE = c(1, 1), + parS = c(5, 25), + ns = 100, + nr = TRUE, + d = 5 ) res3$oc From 640f9f31cf9c282fadac95c3e8889e41d512c214 Mon Sep 17 00:00:00 2001 From: "27856297+dependabot-preview[bot]@users.noreply.github.com" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Wed, 30 Aug 2023 06:46:02 +0000 Subject: [PATCH 34/46] [skip actions] Roxygen Man Pages Auto Update --- man/ocPostprobDist.Rd | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/man/ocPostprobDist.Rd b/man/ocPostprobDist.Rd index 045a7bc5..11019a83 100644 --- a/man/ocPostprobDist.Rd +++ b/man/ocPostprobDist.Rd @@ -97,8 +97,15 @@ PrGrayZone: probability of no decision at the end ("gray zone") res1 <- ocPostprobDist( - nn = c(10, 20, 30), p = 0.4, deltaE = 0.1, deltaF = -0.1, tL = 0.6, tU = 0.6, - parE = c(1, 1), parS = c(5, 25), ns = 100 + nn = c(10, 20, 30), + p = 0.4, + deltaE = 0.1, + deltaF = -0.1, + tL = 0.6, + tU = 0.6, + parE = c(1, 1), + parS = c(5, 25), + ns = 100 ) res1$oc @@ -108,16 +115,32 @@ res1$oc # this call will generate d (distance for random looks around the look locations) # based on "floor(min(nn - c(0,nn[-length(nn)]))/2)" as d is missing: res2 <- ocPostprobDist( - nn = c(10, 20, 30), p = 0.4, deltaE = 0.1, deltaF = -0.1, tL = 0.6, tU = 0.6, - parE = c(1, 1), parS = c(5, 25), ns = 100, nr = TRUE + nn = c(10, 20, 30), + p = 0.4, + deltaE = 0.1, + deltaF = -0.1, + tL = 0.6, + tU = 0.6, + parE = c(1, 1), + parS = c(5, 25), + ns = 100, nr = TRUE ) res2$oc # specify the distance for random looks around the look locations in nn (d=5 for illustration) res3 <- ocPostprobDist( - nn = c(10, 20, 30), p = 0.4, deltaE = 0.1, deltaF = -0.1, tL = 0.6, tU = 0.6, - parE = c(1, 1), parS = c(5, 25), ns = 100, nr = TRUE, d = 5 + nn = c(10, 20, 30), + p = 0.4, + deltaE = 0.1, + deltaF = -0.1, + tL = 0.6, + tU = 0.6, + parE = c(1, 1), + parS = c(5, 25), + ns = 100, + nr = TRUE, + d = 5 ) res3$oc From 3f01c97a77bdfef9a8e3e5f50a7575bf95ae62e3 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 08:50:23 +0200 Subject: [PATCH 35/46] manual of ocPostprobDist --- man/ocPostprobDist.Rd | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/man/ocPostprobDist.Rd b/man/ocPostprobDist.Rd index 045a7bc5..11019a83 100644 --- a/man/ocPostprobDist.Rd +++ b/man/ocPostprobDist.Rd @@ -97,8 +97,15 @@ PrGrayZone: probability of no decision at the end ("gray zone") res1 <- ocPostprobDist( - nn = c(10, 20, 30), p = 0.4, deltaE = 0.1, deltaF = -0.1, tL = 0.6, tU = 0.6, - parE = c(1, 1), parS = c(5, 25), ns = 100 + nn = c(10, 20, 30), + p = 0.4, + deltaE = 0.1, + deltaF = -0.1, + tL = 0.6, + tU = 0.6, + parE = c(1, 1), + parS = c(5, 25), + ns = 100 ) res1$oc @@ -108,16 +115,32 @@ res1$oc # this call will generate d (distance for random looks around the look locations) # based on "floor(min(nn - c(0,nn[-length(nn)]))/2)" as d is missing: res2 <- ocPostprobDist( - nn = c(10, 20, 30), p = 0.4, deltaE = 0.1, deltaF = -0.1, tL = 0.6, tU = 0.6, - parE = c(1, 1), parS = c(5, 25), ns = 100, nr = TRUE + nn = c(10, 20, 30), + p = 0.4, + deltaE = 0.1, + deltaF = -0.1, + tL = 0.6, + tU = 0.6, + parE = c(1, 1), + parS = c(5, 25), + ns = 100, nr = TRUE ) res2$oc # specify the distance for random looks around the look locations in nn (d=5 for illustration) res3 <- ocPostprobDist( - nn = c(10, 20, 30), p = 0.4, deltaE = 0.1, deltaF = -0.1, tL = 0.6, tU = 0.6, - parE = c(1, 1), parS = c(5, 25), ns = 100, nr = TRUE, d = 5 + nn = c(10, 20, 30), + p = 0.4, + deltaE = 0.1, + deltaF = -0.1, + tL = 0.6, + tU = 0.6, + parE = c(1, 1), + parS = c(5, 25), + ns = 100, + nr = TRUE, + d = 5 ) res3$oc From cc069c3201083e1413d4b602bcd932a9885b675d Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 09:04:13 +0200 Subject: [PATCH 36/46] fixing roxygen type to p --- R/dbetabinom.R | 2 +- man/qbetaMix.Rd | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 21dc983a..0b64e5fe 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -183,7 +183,7 @@ pbetaMix <- Vectorize(pbetaMix, vectorize.args = "q") #' #' Calculates the quantile of the Beta-Mixture distribution for a given probability. #' -#' @typed qt : numeric +#' @typed p : numeric #' the required probability #' @typed par : number #' the beta parameters matrix, with K rows and 2 columns, diff --git a/man/qbetaMix.Rd b/man/qbetaMix.Rd index 7dbbdaf1..be89fc47 100644 --- a/man/qbetaMix.Rd +++ b/man/qbetaMix.Rd @@ -7,14 +7,14 @@ qbetaMix(p, par, weights, lower.tail = TRUE) } \arguments{ +\item{p}{(\code{numeric}):\cr the required probability} + \item{par}{(\code{number}):\cr the beta parameters matrix, with K rows and 2 columns, corresponding to the beta parameters of the K components.} \item{weights}{(\code{matrix}):\cr the mixture weights of the beta mixture prior.} \item{lower.tail}{(\code{flag}):\cr whether CDF at x taken at lower or upper tail} - -\item{qt}{(\code{numeric}):\cr the required probability} } \value{ The abscissa. From 626345c09fc79e3269746a4fc9cf843928cef69f Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 09:17:18 +0200 Subject: [PATCH 37/46] see if this fixes the CMD check --- R/dbetabinom.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 0b64e5fe..6a7c95f0 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -168,7 +168,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' @example examples/pbetaMix.R #' @export pbetaMix <- function(q, par, weights, lower.tail = TRUE) { - assert_number(q, lower = 0, finite = TRUE) + assert_numeric(q, lower = 0, finite = TRUE) assert_numeric(weights, lower = 0, upper = 1, finite = TRUE) assert_matrix(par) assert_flag(lower.tail) From 3882982216b6ea32471c3241862747d60f208c1d Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 09:46:09 +0200 Subject: [PATCH 38/46] see if fixes CMD check error --- R/postprob.R | 4 ++-- examples/postprob.R | 2 +- man/postprob.Rd | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/postprob.R b/R/postprob.R index 5824b99d..02b895c9 100644 --- a/R/postprob.R +++ b/R/postprob.R @@ -73,7 +73,7 @@ postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALS ## now compute updated parameters betamixPost <- getBetamixPost( - x = x, + x = xi, n = n, par = parE, weights = weights @@ -83,7 +83,7 @@ postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALS ## now compute the survival function at p, i.e. 1 - cdf at p: ret <- with( betamixPost, - pbetaMix(q = p, par = par, weights = weights, lower.tail = FALSE) + pbetaMix(q = xi, par = par, weights = weights, lower.tail = FALSE) ) if (log.p) { diff --git a/examples/postprob.R b/examples/postprob.R index bad6317b..b31ab387 100644 --- a/examples/postprob.R +++ b/examples/postprob.R @@ -22,5 +22,5 @@ postprob( c(0.6, 0.4), c(1, 1) ), - weights = c(0.6, 0.4) + weights = c(0.5, 0.4) ) diff --git a/man/postprob.Rd b/man/postprob.Rd index 84418334..c3b5a8e7 100644 --- a/man/postprob.Rd +++ b/man/postprob.Rd @@ -66,6 +66,6 @@ postprob( c(0.6, 0.4), c(1, 1) ), - weights = c(0.6, 0.4) + weights = c(0.5, 0.4) ) } From f6c7df2a00a2119e6da8ba476fddb1c19e963a7b Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 13:04:38 +0200 Subject: [PATCH 39/46] xi removed --- R/postprob.R | 4 ++-- examples/postprob.R | 2 +- man/postprob.Rd | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/postprob.R b/R/postprob.R index 02b895c9..484fad9b 100644 --- a/R/postprob.R +++ b/R/postprob.R @@ -73,7 +73,7 @@ postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALS ## now compute updated parameters betamixPost <- getBetamixPost( - x = xi, + x = x, n = n, par = parE, weights = weights @@ -83,7 +83,7 @@ postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALS ## now compute the survival function at p, i.e. 1 - cdf at p: ret <- with( betamixPost, - pbetaMix(q = xi, par = par, weights = weights, lower.tail = FALSE) + pbetaMix(q = x, par = par, weights = weights, lower.tail = FALSE) ) if (log.p) { diff --git a/examples/postprob.R b/examples/postprob.R index b31ab387..bad6317b 100644 --- a/examples/postprob.R +++ b/examples/postprob.R @@ -22,5 +22,5 @@ postprob( c(0.6, 0.4), c(1, 1) ), - weights = c(0.5, 0.4) + weights = c(0.6, 0.4) ) diff --git a/man/postprob.Rd b/man/postprob.Rd index c3b5a8e7..84418334 100644 --- a/man/postprob.Rd +++ b/man/postprob.Rd @@ -66,6 +66,6 @@ postprob( c(0.6, 0.4), c(1, 1) ), - weights = c(0.5, 0.4) + weights = c(0.6, 0.4) ) } From 80eee89e6442f3dc747bbff527d4f76e8ecd99b6 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 13:04:38 +0200 Subject: [PATCH 40/46] example post prob works --- R/postprob.R | 4 ++-- examples/postprob.R | 2 +- man/postprob.Rd | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/postprob.R b/R/postprob.R index 02b895c9..484fad9b 100644 --- a/R/postprob.R +++ b/R/postprob.R @@ -73,7 +73,7 @@ postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALS ## now compute updated parameters betamixPost <- getBetamixPost( - x = xi, + x = x, n = n, par = parE, weights = weights @@ -83,7 +83,7 @@ postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALS ## now compute the survival function at p, i.e. 1 - cdf at p: ret <- with( betamixPost, - pbetaMix(q = xi, par = par, weights = weights, lower.tail = FALSE) + pbetaMix(q = x, par = par, weights = weights, lower.tail = FALSE) ) if (log.p) { diff --git a/examples/postprob.R b/examples/postprob.R index b31ab387..bad6317b 100644 --- a/examples/postprob.R +++ b/examples/postprob.R @@ -22,5 +22,5 @@ postprob( c(0.6, 0.4), c(1, 1) ), - weights = c(0.5, 0.4) + weights = c(0.6, 0.4) ) diff --git a/man/postprob.Rd b/man/postprob.Rd index c3b5a8e7..84418334 100644 --- a/man/postprob.Rd +++ b/man/postprob.Rd @@ -66,6 +66,6 @@ postprob( c(0.6, 0.4), c(1, 1) ), - weights = c(0.5, 0.4) + weights = c(0.6, 0.4) ) } From 1d115b429c6928cf25cd73817cbd346b3463fe53 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 13:30:47 +0200 Subject: [PATCH 41/46] clean --- R/postprob.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/postprob.R b/R/postprob.R index 484fad9b..5824b99d 100644 --- a/R/postprob.R +++ b/R/postprob.R @@ -83,7 +83,7 @@ postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALS ## now compute the survival function at p, i.e. 1 - cdf at p: ret <- with( betamixPost, - pbetaMix(q = x, par = par, weights = weights, lower.tail = FALSE) + pbetaMix(q = p, par = par, weights = weights, lower.tail = FALSE) ) if (log.p) { From 5bb074e11e9c6a67cb7e0e746298ec5769c5bc33 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 14:24:15 +0200 Subject: [PATCH 42/46] assert_number --- R/dbetabinom.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 6a7c95f0..0b64e5fe 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -168,7 +168,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' @example examples/pbetaMix.R #' @export pbetaMix <- function(q, par, weights, lower.tail = TRUE) { - assert_numeric(q, lower = 0, finite = TRUE) + assert_number(q, lower = 0, finite = TRUE) assert_numeric(weights, lower = 0, upper = 1, finite = TRUE) assert_matrix(par) assert_flag(lower.tail) From 9d4b62c893dd9316427f9ff4cd575ac22ec8e5e9 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 14:24:15 +0200 Subject: [PATCH 43/46] in postprobDist, I changed p-> q --- R/dbetabinom.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 6a7c95f0..0b64e5fe 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -168,7 +168,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' @example examples/pbetaMix.R #' @export pbetaMix <- function(q, par, weights, lower.tail = TRUE) { - assert_numeric(q, lower = 0, finite = TRUE) + assert_number(q, lower = 0, finite = TRUE) assert_numeric(weights, lower = 0, upper = 1, finite = TRUE) assert_matrix(par) assert_flag(lower.tail) From db8fb65df6f5399aeeed754d6abdd0a64f281814 Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 15:04:54 +0200 Subject: [PATCH 44/46] change p to q --- R/postprobDist.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/postprobDist.R b/R/postprobDist.R index ce339667..1edddc90 100644 --- a/R/postprobDist.R +++ b/R/postprobDist.R @@ -126,7 +126,7 @@ postprobDist <- function(x, n, bounds <- with( controlBetamixPost, qbetaMix( - p = c(epsilon, 1 - epsilon), + q = c(epsilon, 1 - epsilon), par = par, weights = weights ) From ac40906d96ce7d39f67d3f34848518bc6a87cc7c Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 30 Aug 2023 15:45:18 +0200 Subject: [PATCH 45/46] change q to p --- R/postprobDist.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/postprobDist.R b/R/postprobDist.R index 1edddc90..ce339667 100644 --- a/R/postprobDist.R +++ b/R/postprobDist.R @@ -126,7 +126,7 @@ postprobDist <- function(x, n, bounds <- with( controlBetamixPost, qbetaMix( - q = c(epsilon, 1 - epsilon), + p = c(epsilon, 1 - epsilon), par = par, weights = weights ) From 1a03d33ddda5dbb7353fbe3f23d51f86d53cd4cd Mon Sep 17 00:00:00 2001 From: Daniel Sabanes Bove Date: Wed, 30 Aug 2023 16:37:16 +0200 Subject: [PATCH 46/46] fix postprobDist (for now) --- R/dbetabinom.R | 2 +- R/postprobDist.R | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 0b64e5fe..ea3a2bdf 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -168,7 +168,7 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x") #' @example examples/pbetaMix.R #' @export pbetaMix <- function(q, par, weights, lower.tail = TRUE) { - assert_number(q, lower = 0, finite = TRUE) + assert_number(q, lower = 0, upper = 1, finite = TRUE) assert_numeric(weights, lower = 0, upper = 1, finite = TRUE) assert_matrix(par) assert_flag(lower.tail) diff --git a/R/postprobDist.R b/R/postprobDist.R index ce339667..728752e5 100644 --- a/R/postprobDist.R +++ b/R/postprobDist.R @@ -133,7 +133,11 @@ postprobDist <- function(x, n, ) intRes <- integrate( f = integrand, - lower = bounds[1], + lower = + max( + bounds[1], + ifelse(relativeDelta, 0, 0 - delta) + ), upper = min( ifelse(relativeDelta, 1, 1 - delta),