Skip to content

Commit

Permalink
Clean up pbetaMix and qbetaMix #9 (#10)
Browse files Browse the repository at this point in the history
* cleaning up dbetabinom

* removed rounding, not needed

* rmarkdown language and assert for dbetabinom

* changed x and m to number instead of numeric

* test

* pbetaMix function and test

* added new tests and changed documentation

* upper case for test_that comments and more tests for qbeta

* test_thats for pbetaMix and qbetaMix

* tidy syntax and last checks

* tidy syntax and last checks

* Update R/dbetabinom.R

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

* [skip actions] Roxygen Man Pages Auto Update

* Update R/dbetabinom.R

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

* Update R/dbetabinom.R

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

* Update R/dbetabinom.R

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

* Update R/dbetabinom.R

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

* Update R/dbetabinom.R

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

* Update R/dbetabinom.R

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

* Update R/dbetabinom.R

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

* Update examples/pbetaMix.R

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

* p and q inputs

* q,p changes

* Update R/dbetabinom.R

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

* [skip actions] Roxygen Man Pages Auto Update

* documentation to reflect p and q changes

* amending qt

* intro x-> q

* amented postprob file

* amended postprob file

* fixed mistake on x

* qbetaMix was in postprobDist

* trying to find R CMD check error

* [skip actions] Roxygen Man Pages Auto Update

* manual of ocPostprobDist

* fixing roxygen type to p

* see if this fixes the CMD check

* see if fixes CMD check error

* xi removed

* example post prob works

* clean

* assert_number

* in postprobDist, I changed p-> q

* change p to q

* change q to p

* fix postprobDist (for now)

---------

Co-authored-by: Daniel Sabanes Bove <[email protected]>
Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Daniel Sabanes Bove <[email protected]>
  • Loading branch information
4 people authored Aug 31, 2023
1 parent e649e9c commit 168f3e4
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 93 deletions.
71 changes: 46 additions & 25 deletions R/dbetabinom.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,46 +145,67 @@ dbetaMix <- function(x, par, weights, log = FALSE) {
dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x")


#' Beta-mixture cdf
#' Beta-Mixture CDF
#'
#' Note that `x` can be a vector.
#' @description `r lifecycle::badge("experimental")`
#'
#' @param x the abscissa.
#' @param par the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components.
#' @param weights the mixture weights of the beta mixture prior.
#' @param lower.tail logical; if TRUE (default), probabilities are `P[X <= x]`,
#' and otherwise `P[X > x]`.
#' @return the (one minus) cdf value.
#' Calculates the CDF of the Beta-Mixture distribution.
#'
#' @typed q : number
#' the abscissa.
#' @typed par : matrix
#' the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components.
#' @typed weights : numeric
#' the mixture weights of the beta mixture prior which add up to 1.
#' @typed lower.tail : flag
#' if `TRUE` (default), probabilities are `P[X <= x]`,
#' and otherwise `P[X > x]`.
#' @return The (one minus) cdf value
#'
#' @note `q` can be a vector.
#'
#' @example examples/pbetaMix.R
#' @export
pbetaMix <- function(x, par, weights, lower.tail = TRUE) {
ret <- sum(weights * pbeta(x, par[, 1], par[, 2], lower.tail = lower.tail))
return(ret)
pbetaMix <- function(q, par, weights, lower.tail = 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)
sum(weights * pbeta(q, par[, 1], par[, 2], lower.tail = lower.tail))
}
pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x")
pbetaMix <- Vectorize(pbetaMix, vectorize.args = "q")


#' Beta-mixture quantile function
#' Beta-Mixture Quantile function
#'
#' Note that `x` can be a vector.
#' @description `r lifecycle::badge("experimental")`
#'
#' Calculates the quantile of the Beta-Mixture distribution for a given probability.
#'
#' @param q the required quantile.
#' @param par the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components.
#' @param weights the mixture weights of the beta mixture prior.
#' @return the abscissa.
#' @typed p : 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.
#' @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) {
qbetaMix <- function(p, par, weights, lower.tail = TRUE) {
f <- function(pi) {
pbetaMix(x = pi, par = par, weights = weights) - q
pbetaMix(q = pi, par = par, weights = weights, lower.tail = lower.tail) - p
}
unirootResult <- uniroot(f, lower = 0, upper = 1)
if (unirootResult$iter < 0) {
return(NA)
NA
} else {
return(unirootResult$root)
assert_number(unirootResult$root)
unirootResult$root
}
}
qbetaMix <- Vectorize(qbetaMix, vectorize.args = "q")
qbetaMix <- Vectorize(qbetaMix, vectorize.args = "p")
5 changes: 3 additions & 2 deletions R/postprob.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions R/postprobDist.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,18 @@ postprobDist <- function(x, n,
bounds <- with(
controlBetamixPost,
qbetaMix(
q = c(epsilon, 1 - epsilon),
p = c(epsilon, 1 - epsilon),
par = par,
weights = weights
)
)
intRes <- integrate(
f = integrand,
lower = bounds[1],
lower =
max(
bounds[1],
ifelse(relativeDelta, 0, 0 - delta)
),
upper =
min(
ifelse(relativeDelta, 1, 1 - delta),
Expand Down
35 changes: 29 additions & 6 deletions examples/ocPostprobDist.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
28 changes: 6 additions & 22 deletions examples/pbetaMix.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
## 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)
pbetaMix(q = 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)
# 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
## 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)),
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, 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)),
q = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)),
weights = c(0.6, 0.4)
)
24 changes: 9 additions & 15 deletions examples/qbetaMix.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
## 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)
qbetaMix(
p = 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)),
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 : 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)),
p = seq(0, 1, .01),
par = rbind(c(0.2, 0.4), c(1, 1)),
weights = c(0.6, 0.4)
)
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -3239,6 +3239,7 @@ resizable
ResourceRef
responder
responders
ret
rf
rG
RG
Expand Down
35 changes: 29 additions & 6 deletions man/ocPostprobDist.Rd

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

39 changes: 31 additions & 8 deletions man/pbetaMix.Rd

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

Loading

0 comments on commit 168f3e4

Please sign in to comment.