Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11_postprob #13

Merged
merged 83 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
50ef5b1
cleaning up dbetabinom
audreyyeoCH Aug 14, 2023
4342b5a
removed rounding, not needed
audreyyeoCH Aug 14, 2023
b095ae6
rmarkdown language and assert for dbetabinom
audreyyeoCH Aug 22, 2023
889e97d
changed x and m to number instead of numeric
audreyyeoCH Aug 22, 2023
2eafdc5
test
audreyyeoCH Aug 22, 2023
9dc428b
pbetaMix function and test
audreyyeoCH Aug 23, 2023
dd1d8c4
added new tests and changed documentation
audreyyeoCH Aug 24, 2023
6d7f478
upper case for test_that comments and more tests for qbeta
audreyyeoCH Aug 24, 2023
d520ff0
Merge branch 'main' into 9_pbeta
audreyyeoCH Aug 25, 2023
0b361e3
test_thats for pbetaMix and qbetaMix
audreyyeoCH Aug 27, 2023
8b20f72
tidy syntax and last checks
audreyyeoCH Aug 28, 2023
b0c2218
tidy syntax and last checks
audreyyeoCH Aug 28, 2023
9f53521
Merge remote-tracking branch 'origin/9_pbeta' into 9_pbeta
audreyyeoCH Aug 28, 2023
5384c3d
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
f5403fa
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Aug 28, 2023
0dd2bab
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
ca0236f
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
9cf54d0
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
2eb3bd2
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
c08435f
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
e36facd
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
f45d2f0
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
0505982
Update examples/pbetaMix.R
audreyyeoCH Aug 28, 2023
469ed6e
p and q inputs
audreyyeoCH Aug 29, 2023
1abea50
q,p changes
audreyyeoCH Aug 29, 2023
b7b523c
Update R/dbetabinom.R
audreyyeoCH Aug 29, 2023
34a663b
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Aug 29, 2023
f384100
documentation to reflect p and q changes
audreyyeoCH Aug 29, 2023
f0d5d00
Merge remote-tracking branch 'origin/9_pbeta' into 9_pbeta
audreyyeoCH Aug 29, 2023
b9e50b2
amending qt
audreyyeoCH Aug 29, 2023
0ab047d
intro x-> q
audreyyeoCH Aug 29, 2023
4164b46
amented postprob file
audreyyeoCH Aug 29, 2023
5d98627
amended postprob file
audreyyeoCH Aug 29, 2023
205e574
fixed mistake on x
audreyyeoCH Aug 29, 2023
01bd1bf
Merge remote-tracking branch 'origin/9_pbeta' into 9_pbeta
audreyyeoCH Aug 29, 2023
2d093ba
qbetaMix was in postprobDist
audreyyeoCH Aug 29, 2023
c7dbbf9
trying to find R CMD check error
audreyyeoCH Aug 30, 2023
640f9f3
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Aug 30, 2023
3f01c97
manual of ocPostprobDist
audreyyeoCH Aug 30, 2023
99d6eb5
Merge remote-tracking branch 'origin/9_pbeta' into 9_pbeta
audreyyeoCH Aug 30, 2023
cc069c3
fixing roxygen type to p
audreyyeoCH Aug 30, 2023
626345c
see if this fixes the CMD check
audreyyeoCH Aug 30, 2023
3882982
see if fixes CMD check error
audreyyeoCH Aug 30, 2023
f6c7df2
xi removed
audreyyeoCH Aug 30, 2023
80eee89
example post prob works
audreyyeoCH Aug 30, 2023
1d115b4
clean
audreyyeoCH Aug 30, 2023
2185dea
Merge branch 'tmp' into 9_pbeta
audreyyeoCH Aug 30, 2023
5bb074e
assert_number
audreyyeoCH Aug 30, 2023
9d4b62c
in postprobDist, I changed p-> q
audreyyeoCH Aug 30, 2023
db8fb65
change p to q
audreyyeoCH Aug 30, 2023
51ed0c9
Merge remote-tracking branch 'origin/9_pbeta' into 9_pbeta
audreyyeoCH Aug 30, 2023
8470616
postprobold
audreyyeoCH Aug 30, 2023
3c86a99
postprobOld
audreyyeoCH Aug 31, 2023
80606cf
Merge remote-tracking branch 'origin/main' into 11_postprob
audreyyeoCH Aug 31, 2023
2f9b831
clean post prob
audreyyeoCH Sep 7, 2023
5d553ee
nice titles
audreyyeoCH Sep 7, 2023
6b10167
Update R/dbetabinom.R
audreyyeoCH Sep 8, 2023
61fc96f
Update R/dbetabinom.R
audreyyeoCH Sep 8, 2023
5c7dab4
Update R/postprob.R
audreyyeoCH Sep 8, 2023
96a2b69
Update tests/testthat/test-postprob.R
audreyyeoCH Sep 8, 2023
9cdd9e6
Update tests/testthat/test-postprob.R
audreyyeoCH Sep 8, 2023
146dabd
Update tests/testthat/test-dbetabinom.R
audreyyeoCH Sep 8, 2023
af9419a
Update R/postprob.R
audreyyeoCH Sep 8, 2023
9bf2018
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 8, 2023
6497e6b
post PR changes
audreyyeoCH Sep 8, 2023
f1d3102
Update R/postprob.R
audreyyeoCH Sep 8, 2023
1cd8b39
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 8, 2023
dbcc53f
post PR
audreyyeoCH Sep 8, 2023
d34c114
Update R/postprob.R
audreyyeoCH Sep 8, 2023
ad4be5a
Update R/postprob.R
audreyyeoCH Sep 8, 2023
ef0a8e3
Update R/postprob.R
audreyyeoCH Sep 8, 2023
9ee2d75
Update R/postprob.R
audreyyeoCH Sep 8, 2023
29d40bf
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 8, 2023
e0ebdd5
Update R/postprob.R
audreyyeoCH Sep 8, 2023
3562e49
Auto stash before merge of "11_postprob" and "origin/11_postprob"
audreyyeoCH Sep 8, 2023
98388e4
manual edits not committed on pr review
audreyyeoCH Sep 8, 2023
517bf9f
dbetabinom changes too
audreyyeoCH Sep 8, 2023
c8d52d9
single # on test-dbetabinom
audreyyeoCH Sep 8, 2023
b14cba5
old-> Beta in test
audreyyeoCH Sep 8, 2023
6adc23c
Update R/postprob.R
audreyyeoCH Sep 20, 2023
21b29b5
Update R/postprob.R
audreyyeoCH Sep 20, 2023
9236db1
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 20, 2023
cc8728e
trigger checks
Sep 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export(plotBounds)
export(plotDecision)
export(plotOc)
export(postprob)
export(postprobBeta)
export(postprobDist)
export(postprobOld)
export(predprob)
export(predprobDist)
export(qbetaMix)
Expand Down Expand Up @@ -53,7 +53,6 @@ importFrom(stats,dbeta)
importFrom(stats,dbinom)
importFrom(stats,integrate)
importFrom(stats,optimize)
importFrom(stats,pbeta)
importFrom(stats,quantile)
importFrom(stats,rbeta)
importFrom(stats,rbinom)
Expand Down
13 changes: 8 additions & 5 deletions R/dbetabinom.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Beta-binomial density function
#' Beta-Binomial Density Function
#'
#' @description `r lifecycle::badge("experimental")`
#'
Expand Down Expand Up @@ -38,7 +38,7 @@ dbetabinom <- function(x, m, a, b, log = FALSE) {
}


#' Beta-mixture-binomial density function
#' Beta-Mixture-Binomial Density Function
#'
#' @description `r lifecycle::badge("experimental")`
#'
Expand All @@ -56,6 +56,7 @@ dbetabinom <- function(x, m, a, b, log = FALSE) {
#' @typed log : flag
#' whether to return the log density value (not default).
#' @return The (log) density values of the mixture of beta-binomial distributions at `x`.
#'
#' @note `x` can be a vector.
#'
#' @example examples/dbetabinomMix.R
Expand All @@ -73,7 +74,9 @@ dbetabinomMix <- function(x, m, par, weights, log = FALSE) {
dbetabinomMix <- Vectorize(dbetabinomMix, vectorize.args = "x")


#' Computes the posterior parameters of a beta mixture
#' Compute Beta-Mixture-Binomial Posterior Distribution
#'
#' Computes the posterior parameters of a beta-mixture-binomial distribution.
#'
#' @param x number of successes
#' @param n number of patients
Expand Down Expand Up @@ -177,14 +180,14 @@ pbetaMix <- function(q, par, weights, lower.tail = TRUE) {
pbetaMix <- Vectorize(pbetaMix, vectorize.args = "q")


#' Beta-Mixture Quantile function
#' Beta-Mixture Quantile Function
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' Calculates the quantile of the Beta-Mixture distribution for a given probability.
#'
#' @typed p : numeric
#' the required probability
#' 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.
Expand Down
104 changes: 59 additions & 45 deletions R/postprob.R
Original file line number Diff line number Diff line change
@@ -1,95 +1,109 @@
#' @include dbetabinom.R
NULL

#' Compute the posterior probability to be above threshold assuming a beta prior
#' on the response rate
#' Posterior Probability of Efficacy Given Beta Prior
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' Computes the posterior probability Pr(P_E > p | data). Prior is P_E ~ beta(a, b),
#' Compute the posterior probability to be above threshold assuming a beta prior
#' on the response rate such that `Pr(P_E > p | data)`. Prior is `P_E ~ beta(a, b)`,
#' with default set to be a uniform or beta(1,1).
#'
#' We observed x successes in n trials and so the posterior is
#' P_E | data ~ beta(a + x, b + n - x).
#' We observed `x` successes in n trials and so the posterior is
#' `P_E | data ~ beta(a + x, b + n - x)`.
#'
#' @param x number of successes
#' @param n number of patients
#' @param p threshold
#' @param a first parameter of the beta prior (successes)
#' @param b second parameter of the beta prior (failures)
#' @typed x : numeric
#' number of successes.
#' @typed n : number
#' number of patients.
#' @typed p : number
#' threshold set to compute posterior probability.
#' @typed a : matrix
#' first parameter `alpha` of the beta prior (successes).
#' @typed b : matrix
#' second parameter `beta` of the beta prior (failures).
#' @return The posterior probability that the response rate P_E is above a threshold p.
#'
#' @importFrom stats pbeta
#'
#' @example examples/postprobOld.R
#' @example examples/postprobBeta.R
#' @export
postprobOld <- function(x, n, p, a = 1, b = 1) {
postprobBeta <- function(x, n, p, a = 1, b = 1) {
assert_number(n, lower = 0, finite = TRUE)
assert_numeric(x, lower = 0, upper = n, finite = TRUE)
assert_number(a, finite = TRUE)
assert_number(b, finite = TRUE)
assert_number(p, lower = 0, upper = 1, finite = TRUE)
stats::pbeta(p, a + x, b + n - x, lower.tail = FALSE)
}


#' Compute the posterior probability to be above threshold,
#' with a beta mixture prior on the response rate.
#' Posterior Probability of Efficacy Given Beta-Mixture Prior
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' Computes the posterior probability `Pr(P_E > p | data)`. Prior is
#' Compute the posterior probability that the response probability `P_E`is above a threshold.
#' such that this posterior probability can be expressed as `Pr(P_E > p | data)`. Prior is
#' `P_E ~ sum(weights * beta(parE[, 1], parE[, 2]))`, i.e., a mixture of beta priors.
#' Default is one component only with uniform or `beta(1,1)`.
#'
#' We observed x successes in n trials. Note that \code{x} can be a vector.
#' We observed `x` successes in n trials.
#'
#' Posterior is again a mixture of beta priors, with updated mixture weights
#' and beta parameters.
#'
#' @param x number of successes
#' @param n number of patients
#' @param p threshold
#' @param parE the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components. Default is a
#' uniform prior.
#' @param weights the mixture weights of the beta mixture prior. Default are
#' uniform weights across mixture components.
#' @param betamixPost optional result of \code{\link{getBetamixPost}} in order
#' to speed up the computations. If supplied, this is directly used, bypassing
#' the other arguments (except \code{p} and \code{log.p} of course)
#' @param log.p Return the log of the probability? (default: FALSE)
#' @return The posterior probability that the response rate P_E is above p.
#' @typed x : numeric
#' number of successes.
#' @typed n : number
#' number of patients.
#' @typed p : number
#' threshold that `P_E` is measured.
#' @typed parE : matrix
#' the beta parameters matrix, with `K` rows and 2 columns,
#' corresponding to the beta parameters of the `K` components.
#' Default is a uniform prior.
#' @typed weights : vector
#' The mixture weights of the beta mixture prior. Default are
#' uniform weights across mixture components.
#' @typed betamixPost : matrix
#' optional result of `[getBetamixPost()]` in order
#' to speed up the computations. If supplied, this is directly used, bypassing
#' the other arguments (except `p` and `log.p` of course).
#' @typed log.p : number
#' whether to return the log of the probability
#' @return The posterior probability that the response rate `P_E` is above `p`.
#'
#' @example examples/postprob.R
#' @export
postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALSE) {
if (missing(betamixPost)) {
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
## if parE is a vector => situation where there is only one component
assert_flag(log.p)
if (is.vector(parE)) {
## check that it has exactly two entries
stopifnot(identical(length(parE), 2L))

## and transpose to matrix with one row
# Here there is only one component.
assert_true(identical(length(parE), 2L))
# To get matrix with one row.
parE <- t(parE)
}

## if prior weights of the beta mixture are not supplied
assert_matrix(parE)
if (missing(weights)) {
weights <- rep(1, nrow(parE))
## (don't need to be normalized, this is done in getBetamixPost)
}

## now compute updated parameters
betamixPost <- getBetamixPost(
x = x,
n = n,
par = parE,
weights = weights
)
}

## now compute the survival function at p, i.e. 1 - cdf at p:
assert_list(betamixPost)
assert_names(names(betamixPost), identical.to = c("par", "weights"))
ret <- with(
betamixPost,
pbetaMix(q = p, par = par, weights = weights, lower.tail = FALSE)
)

if (log.p) {
return(log(ret))
log(ret)
} else {
return(ret)
ret
}
}
postprob <- Vectorize(postprob, vectorize.args = "x")
21 changes: 8 additions & 13 deletions examples/postprob.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
## Example taken from Lee and Liu
##
## We observed 16 successes out of 23 patients
## We set a threshold of 0.60
## Assume a beta(0.6,0.4) prior for P_E
## Posterior will be a beta(16.6, 7.4), Pr(P_E > p | data) = 0.836
##
##
# Example taken from Lee and Liu (2006)
#
# We observed 16 successes out of 23 patients
# We set a threshold of 0.60
# Assume a beta(0.6,0.4) prior for P_E
# Posterior will be a beta(16.6, 7.4), Pr(P_E > p | data) = 0.836

postprob(x = 16, n = 23, p = 0.60, par = c(0.6, 0.4))

## We could instead specify a mixture prior
## 2 component beta mixture prior, i.e., P_E ~ 0.6*beta(0.6,0.4) + 0.4*beta(1,1) and Pr(P_E > p | data) = 0.823
##
##
##
# We could instead specify a mixture prior
# 2 component beta mixture prior, i.e., P_E ~ 0.6*beta(0.6,0.4) + 0.4*beta(1,1) and Pr(P_E > p | data) = 0.823

postprob(
x = 16, n = 23, p = 0.60,
Expand Down
18 changes: 9 additions & 9 deletions examples/postprobOld.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## Example taken from Lee and Liu (????)
##
## We observed 16 successes out of 23 patients
## We set a threshold of 0.60
## Assume a beta(0.6,0.4) prior for P_E
## Posterior will be a beta(16.6,22.8), Pr(P_E > p | data) = 0.8358808
##
##
# Example taken from Lee & Liu (2006)
# We observed 16 successes out of 23 patients # should we write this in the documentation
# We set a threshold of 0.60
# Assume a beta(0.6,0.4) prior for P_E
# Posterior will be a beta(16.6,22.8), Pr(P_E > p | data) = 0.8358808

postprobOld(x = 16, n = 23, p = 0.60, a = 0.6, b = 0.4)

# Example taken from Lee and Liu (2006)
postprobBeta(x = 16, n = 23, p = 0.60, a = 0.6, b = 0.4)
# Interpretation : The probability 16 of 23 successes is greater than 60 % threshold is approximately 84 %
2 changes: 1 addition & 1 deletion man/dbetabinom.Rd

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

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

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

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

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

56 changes: 26 additions & 30 deletions man/postprob.Rd

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

Loading