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

14 oc post prob #16

Merged
merged 103 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
103 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
c475186
documentations
audreyyeoCH Sep 8, 2023
0dd9c8a
clean
audreyyeoCH Sep 8, 2023
9ddd5df
clean
audreyyeoCH Sep 21, 2023
476ad27
clean
audreyyeoCH Sep 22, 2023
5397930
clean
audreyyeoCH Sep 22, 2023
6fd620f
Merge remote-tracking branch 'origin/main' into 14_ocPostProb
audreyyeoCH Sep 22, 2023
2dd0a44
clean
audreyyeoCH Sep 26, 2023
0af15cd
clean
audreyyeoCH Sep 27, 2023
63de68a
some examples and tests
audreyyeoCH Sep 28, 2023
7eba969
clean
audreyyeoCH Sep 29, 2023
8135abd
fix R CMD check errors
audreyyeoCH Oct 2, 2023
c79982c
fix R CMD check errors for PR
audreyyeoCH Oct 2, 2023
cf0ace7
Update R/ocPostprob.R
audreyyeoCH Oct 3, 2023
3b1ed09
Update R/ocPostprob.R
audreyyeoCH Oct 3, 2023
271edd4
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Oct 3, 2023
7f2c01b
Update R/ocPostprob.R
audreyyeoCH Oct 3, 2023
027e432
Update R/ocPostprob.R
audreyyeoCH Oct 3, 2023
459dac9
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Oct 3, 2023
7964767
Update R/ocPostprob.R
audreyyeoCH Oct 3, 2023
c3ab3ea
PR feedback
audreyyeoCH Oct 3, 2023
71f797e
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Oct 3, 2023
645024c
Merge remote-tracking branch 'origin/14_ocPostProb' into 14_ocPostProb
audreyyeoCH Oct 3, 2023
7724a7a
clean
audreyyeoCH Oct 5, 2023
f436dbb
Merge remote-tracking branch 'origin/14_ocPostProb' into 14_ocPostProb
audreyyeoCH Oct 5, 2023
35126ab
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Oct 5, 2023
55939e9
clean
audreyyeoCH Oct 11, 2023
a9c991f
Revert "[skip actions] Roxygen Man Pages Auto Update"
audreyyeoCH Oct 11, 2023
147146c
Revert "clean"
audreyyeoCH Oct 11, 2023
ac6cb69
clean
audreyyeoCH Oct 11, 2023
bd78bbe
clean
audreyyeoCH Oct 12, 2023
971264f
Update R/ocPostprob.R
audreyyeoCH Oct 12, 2023
ff8aa37
Update R/ocPostprob.R
audreyyeoCH Oct 12, 2023
6f3a999
Update R/ocPostprob.R
audreyyeoCH Oct 12, 2023
8990c36
Update R/ocPostprob.R
audreyyeoCH Oct 12, 2023
8923010
Update R/postprob.R
audreyyeoCH Oct 12, 2023
3820c91
Update R/postprob.R
audreyyeoCH Oct 12, 2023
ec14b6d
Update tests/testthat/test-ocPostprob.R
audreyyeoCH Oct 12, 2023
0c5202e
clean
audreyyeoCH Oct 12, 2023
f94c6e4
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Oct 12, 2023
cb5d150
clean
audreyyeoCH Oct 12, 2023
7b29dc0
Merge remote-tracking branch 'origin/14_ocPostProb' into 14_ocPostProb
audreyyeoCH Oct 12, 2023
5c332e4
clean
audreyyeoCH Oct 12, 2023
6d0b952
clean
audreyyeoCH Oct 12, 2023
096b8bb
Update tests/testthat/test-ocPostprob.R
audreyyeoCH Oct 13, 2023
993dad4
clean
audreyyeoCH Oct 13, 2023
ee111a6
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Oct 13, 2023
9c1ada1
clean
audreyyeoCH Oct 13, 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
4 changes: 0 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ export(dbetabinom)
export(dbetabinomMix)
export(dbetadiff)
export(getBetamixPost)
export(get_decision)
export(get_distance)
export(get_looks)
export(get_oc)
export(logit)
export(myPlot)
export(myPlotDiff)
Expand Down
126 changes: 58 additions & 68 deletions R/ocPostprob.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#' @include postprob.R
NULL

#' Generating random distance in given looks for sample sizes for efficacy and futility.
#'
#' @description `r lifecycle::badge("experimental")`
#' Generating random distance in given looks for sample sizes for Efficacy and Futility.
#'
#' A helper function for `ocPostprob` to generate random distance's wiggle room around looks `nn`.
#' Numeric looks `nn` must be of minimum two elements and will generate `length(nn)-1` distances.
#'
#' @param nn : number or numeric
#' the union of `nnE` and `nnF` (if futility analysis or looks exists) supplied
#' @typed nn : number or numeric
#' the union of `nnE` and `nnF` (if futility analysis or looks exists) supplied.
#'
#' @return A numeric with `length(nn)-1` elements.
#'
#' @export
#' @keywords internal
#'
#' @examples examples / ocPostprob.R
get_distance <- function(nn) {
h_get_distance <- function(nn) {
assert_numeric(nn, unique = TRUE, sorted = TRUE, min.len = 1)
dist0 <- floor(min(nn - c(0, nn[-length(nn)])) / 2)
assert_numeric(dist0, sorted = TRUE)
Expand All @@ -27,31 +24,27 @@ get_distance <- function(nn) {
)
}


#' Generating looks
#'
#' @description `r lifecycle::badge("experimental")`
#' Generating looks from random distance
#'
#' A helper function for `ocPostprob` that applies the numeric element of `dist` to looks `nn`.
#'
#' @typed dist : numeric or logical
#' Distance for random looks around the look locations in `nn`,
#' where `dist`is generated from `get_distance` in a numeric of at least one element.
#' If `NULL`, only one location look will be set at `nnE` or `nnF`.
#' distance for random looks around the look locations in `nn`,
#' where `dist`is generated from `h_get_distance` in a numeric of at least one element.
#' If `NULL`, only one location look will be set at `nnE` or `nnF`.
#' @typed nnE : numeric
#' Sample size or sizes where study can be stopped for efficacy decision. If different for futility decision,
#' specify in `nnF`.
#' sample size or sizes where study can be stopped for Efficacy decision. If different for Futility decision,
#' specify in `nnF`.
#' @typed nnF : numeric
#' Sample size or sizes where study can be stopped for futility decision if different from efficacy decision.
#' sample size or sizes where study can be stopped for Futility decision if different from Efficacy decision.
#'
#' @return Uses distance from `get_distance` to add to looks, creating wiggled looks:
#' `nnrE`is the result for efficacy looks with random distance added.
#' `nnrF`is the result for futility looks with random distance added.
#' @return Uses distance from `h_get_distance` to add to looks, creating wiggled looks:
#' `nnrE`is the result for Efficacy looks with random distance added.
#' `nnrF`is the result for Futility looks with random distance added.
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @export
#' @keywords internal
#'
#' @examples examples / ocPostProb.R
get_looks <- function(dist, nnE, nnF) {
h_get_looks <- function(dist, nnE, nnF) {
assert_numeric(nnE)
assert_numeric(nnF)
nn <- unique(c(nnE, nnF))
Expand All @@ -66,38 +59,35 @@ get_looks <- function(dist, nnE, nnF) {

#' Generating random decision and sample size looks.
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' A helper function for `ocPostprob` to generate numeric of decisions `decisions` and random looks `all_sizes`.
#'
#' @inheritParams get_looks
#' @inheritParams h_get_looks
#' @typed nnr : numeric
#' union of `nnE`and `nnF`.
#' @typed response : numeric
#' A numeric of Bernoulli successes based on `size_look`
#' A numeric of Bernoulli successes based on `size_look`.
#' @typed truep : number
#' assumed true response rate or true rate (scenario).
#' @typed p0 : number
#' lower efficacy threshold of response rate.
#' lower Futility threshold of response rate.
#' @typed p1 : number
#' upper efficacy threshold of response rate.
#' upper Efficacy threshold of response rate.
#' @typed tL : number
#' posterior probability threshold for being below `p0`.
#' posterior probability threshold for being below `p0`..
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#' @typed tU : number
#' posterior probability threshold for being above `p1`.
#' @typed parE : numeric
#' Alpha and beta parameters for the prior on the treatment proportion.
#' alpha and beta parameters for the prior on the treatment proportion.
#' Default set at alpha = 1, beta = 1, or uniform prior.
#'
#' @return A list of the following objects :
#' - `decision` : resulting numeric of decision, one of `TRUE` for GO, `FALSE`for STOP, `NA` for Gray zone
#' - `decision` : resulting numeric of decision, one of `TRUE` for Go, `FALSE`for Stop, `NA` for Gray zone.
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#' - `all_sizes` : resulting numeric of look size, anything below maximum
#' look size is an indicated interim, futility or efficacy or both
#' look size is an indicated interim, Futility or Efficacy or both.
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @export
#' @keywords internal
#'
#' @examples
get_decision <- function(nnr, response, truep, p0, p1, parE = c(1, 1), nnE, nnF, tL, tU) {
h_get_decision <- function(nnr, response, truep, p0, p1, parE = c(1, 1), nnE, nnF, tL, tU) {
index_look <- 1
assert_numeric(nnr)
size_look <- nnr[index_look]
Expand Down Expand Up @@ -127,35 +117,33 @@ get_decision <- function(nnr, response, truep, p0, p1, parE = c(1, 1), nnE, nnF,

#' Creating list for operating characteristics.
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' Generates operating characteristics.
#'
#' @inheritParams get_looks
#' @inheritParams get_decision
#' @inheritParams h_get_looks
#' @inheritParams h_get_decision
#' @typed nnrE : numeric
#' Looks with random distance, if applied on `nnE`.
#' looks with random distance, if applied on `nnE`.
#' @typed nnrF : numeric
#' Looks with random distance, if applied on `nnF`.
#' looks with random distance, if applied on `nnF`.
#' @typed all_sizes : numeric
#' Sample sizes of all looks simulated `length(sim)` times if `dist` applied.
#' sample sizes of all looks simulated `length(sim)` times if `dist` applied.
#' @typed decision : numeric
#' Go, Stop or Gray Zone decisions of all looks simulated `length(sim)` times.
#' Go, Stop or Gray Zone decisions of all looks simulated `length(sim)` times.
#'
#' @return A list of results containing :
#'
#' - `ExpectedN`: expected number of patients in the trials
#' - `PrStopEarly`: probability to stop the trial early (before reaching the
#' maximum sample size)
#' - `PrEarlyEff`: probability of Early Go decision
#' - `PrEarlyFut`: probability to decide for Futility early
#' - `PrEarlyFut`: probability of for Early Stop decision
#' - `PrEfficacy`: probability of Go decision
#' - `PrFutility`: Probability of Stop decision
#' - `PrFutility`: probability of Stop decision
#' - `PrGrayZone`: probability between Go and Stop ,"Evaluate" or Gray decision zone
#' @export
#'
#' @examples examples / ocPostprob.R
get_oc <- function(all_sizes, nnr, decision, nnrE, nnrF) {
#' @keywords internal
#'
h_get_oc <- function(all_sizes, nnr, decision, nnrE, nnrF) {
sim <- length(all_sizes)
assert_logical(decision, len = sim)
assert_numeric(all_sizes)
Expand All @@ -179,8 +167,8 @@ get_oc <- function(all_sizes, nnr, decision, nnrE, nnrF) {
#' Calculate operating characteristics for posterior probability method.
#'
#' It is assumed that the true response rate is `truep`.
#' The trial is stopped for efficacy if the posterior probability to be
#' above `p1` is larger than `tU`, and stopped for futility if the posterior
#' The trial is stopped for Efficacy if the posterior probability to be
#' above `p1` is larger than `tU`, and stopped for Futility if the posterior
#' probability to be below `p0` is larger than `tL`:
#'
#' Stop criteria for Efficacy :
Expand All @@ -197,18 +185,18 @@ get_oc <- function(all_sizes, nnr, decision, nnrE, nnrF) {
#' - `PrStopEarly`: probability to stop the trial early (before reaching the
#' maximum sample size)
#' - `PrEarlyEff`: probability of Early Go decision
#' - `PrEarlyFut`: probability of for Early Futility decision
#' - `PrEarlyFut`: probability of for Early Stop decision
#' - `PrEfficacy`: probability of Go decision
#' - `PrFutility`: Probability of Stop decision
#' - `PrFutility`: probability of Stop decision
#' - `PrGrayZone`: probability between Go and Stop ,"Evaluate" or Gray decision zone
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @inheritParams get_looks
#' @inheritParams get_decision
#' @inheritParams h_get_looks
#' @inheritParams h_get_decision
#' @typed sim : number
#' number of simulations
#' number of simulations.
#' @typed wiggle : logical
#' generate random look locations (not default)
#' if `TRUE`, optional to specify `dist` (see @details)
#' generate random look locations (not default).
#' if `TRUE`, optional to specify `dist` (see @details).
#' @typed randomdist : logical
#' Random distance added to looks. if `NULL`, and `wiggle = TRUE`, function will
#' generate and add a random distance within range of the closest looks.
Expand All @@ -217,9 +205,9 @@ get_oc <- function(all_sizes, nnr, decision, nnrE, nnrF) {
#'
#' - `oc`: matrix with operating characteristics (see @details section)
#' - `nn`: vector of look locations that was supplied
#' - `nnE`: vector of efficacy look locations
#' - `nnF`: vector of futility look locations # TODO
#' - `params`: multiple parameters# TODOs
#' - `nnE`: vector of Efficacy look locations
#' - `nnF`: vector of Futility look locations
#' - `params`: multiple parameters
#'
#' @details
#' `ExpectedN` is an average of the simulated sample sizes.
Expand All @@ -233,22 +221,24 @@ get_oc <- function(all_sizes, nnr, decision, nnrE, nnrF) {
ocPostprob <- function(nnE, truep, p0, p1, tL, tU, parE = c(1, 1),
sim = 50000, wiggle = FALSE, randomdist = NULL, nnF = nnE) {
nn <- sort(unique(c(nnF, nnE)))
assert_number(sim, lower = 1, finite = TRUE)
if (sim < 50000) {
warning("Advise to use sim >= 50000 to achieve convergence")
}
decision <- vector(length = sim)
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
all_sizes <- vector(length = sim)
assert_logical(decision)
assert_logical(all_sizes)
for (k in seq_len(sim)) {
if (length(nn) != 1 && wiggle && is.null(randomdist)) {
dist <- get_distance(nn = nn)
nnr <- get_looks(dist = dist, nnE = nnE, nnF = nnF)
dist <- h_get_distance(nn = nn)
nnr <- h_get_looks(dist = dist, nnE = nnE, nnF = nnF)
nnrE <- nnr$nnrE
nnrF <- nnr$nnrF
} else {
nnrE <- nnE
nnrF <- nnF
}
nnr <- unique(c(nnrE, nnrF))
tmp <- get_decision(
tmp <- h_get_decision(
nnr = nnr, response = response,
truep = truep, p0 = p0, p1 = p1,
parE = c(1, 1), nnE = nnrE,
Expand All @@ -257,7 +247,7 @@ ocPostprob <- function(nnE, truep, p0, p1, tL, tU, parE = c(1, 1),
decision[k] <- tmp$decision
all_sizes[k] <- tmp$all_sizes
}
oc <- get_oc(all_sizes = all_sizes, nnr = nnr, decision = decision, nnrE = nnrE, nnrF = nnrF)
oc <- h_get_oc(all_sizes = all_sizes, nnr = nnr, decision = decision, nnrE = nnrE, nnrF = nnrF)
list(
oc = oc,
Decision = decision,
Expand Down
12 changes: 5 additions & 7 deletions R/postprob.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ postprobOld <- function(x, n, p, a = 1, b = 1) {
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.
#'
Expand Down Expand Up @@ -63,20 +62,19 @@ postprobOld <- function(x, n, p, a = 1, b = 1) {
#' @export
postprob <- function(x, n, p, parE = c(1, 1), weights, betamixPost, log.p = FALSE) {
if (missing(betamixPost)) {
## if parE is a vector => situation where there is only one component
# If betamixPost is missing, then we would use the default parE
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
if (is.vector(parE)) {
# Here there is only one component.
# Here there is only one component in the parE vector.
assert_true(identical(length(parE), 2L))
# To get matrix with one row.
# To get matrix with one row, we transpose parE.
parE <- t(parE)
}

## if prior weights of the beta mixture are not supplied
# If prior weights of the beta mixture are not supplied, weights are given
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
if (missing(weights)) {
weights <- rep(1, nrow(parE))
}

## now compute updated parameters
betamixPost <- getBetamixPost(
x = x,
n = n,
Expand All @@ -85,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:
# Here, we compute the survival function at p, i.e. 1 - cdf at p as lower.tail = FALSE:
ret <- with(
betamixPost,
pbetaMix(q = p, par = par, weights = weights, lower.tail = FALSE)
Expand Down
23 changes: 0 additions & 23 deletions examples/get_decision.R

This file was deleted.

5 changes: 0 additions & 5 deletions examples/get_distance.R

This file was deleted.

16 changes: 0 additions & 16 deletions examples/get_looks.R

This file was deleted.

19 changes: 0 additions & 19 deletions examples/get_oc.R

This file was deleted.

Loading
Loading