Skip to content

Commit

Permalink
res-> result, removed randomdist in vignette too, clean
Browse files Browse the repository at this point in the history
  • Loading branch information
audreyyeoCH committed Jan 5, 2024
1 parent 91b5d6e commit 31f939b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
8 changes: 2 additions & 6 deletions R/ocPostprob.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,6 @@ h_get_oc <- function(all_sizes, nnr, decision, nnrE, nnrF) {
#' @typed wiggle : flag
#' generate random look locations (not default).
#' if `TRUE`, optional to specify `dist` (see @details).
#' @typed randomdist : flag
#' Random distance added to looks. if `TRUE`, and `wiggle = TRUE`, function will
#' generate and add a random distance within range of the closest looks.
#'
#' @return A list with the following elements:
#'
Expand All @@ -231,18 +228,17 @@ h_get_oc <- function(all_sizes, nnr, decision, nnrE, nnrF) {
#' @example examples/ocPostprob.R
#' @export
ocPostprob <- function(nnE, truep, p0, p1, tL, tU, parE = c(1, 1),
sim = 50000, wiggle = FALSE, randomdist = FALSE, nnF = nnE) {
sim = 50000, wiggle = FALSE, nnF = nnE) {
nn <- sort(unique(c(nnF, nnE)))
assert_number(sim, lower = 1, finite = TRUE)
assert_flag(wiggle)
assert_flag(randomdist)
if (sim < 50000) {
warning("Advise to use sim >= 50000 to achieve convergence")
}
decision <- vector(length = sim)
all_sizes <- vector(length = sim)
for (k in seq_len(sim)) {
if (length(nn) != 1 && wiggle && randomdist) {
if (length(nn) != 1 && wiggle) {
dist <- h_get_distance(nn = nn)
nnr <- h_get_looks(dist = dist, nnE = nnE, nnF = nnF)
nnrE <- nnr$nnrE
Expand Down
10 changes: 5 additions & 5 deletions examples/ocPostprob.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@
# Prior of treatment arm parE= Beta(1,1).
res <- ocPostprob(
nnE = c(10, 20, 30), truep = 0.40, p0 = 0.20, p1 = 0.30, tL = 0.60, tU = 0.80, parE = c(1, 1),
sim = 50000, wiggle = TRUE, randomdist = TRUE, nnF = c(10, 20, 30)
sim = 50000, wiggle = TRUE, nnF = c(10, 20, 30)
)

res$oc

# We specify the distance in this example.
res <- ocPostprob(
nnE = c(10, 20, 30), truep = 0.40, p0 = 0.20, p1 = 0.30, tL = 0.60, tU = 0.80, parE = c(1, 1),
sim = 50000, wiggle = TRUE, randomdist = TRUE, nnF = c(10, 20, 30)
sim = 50000, wiggle = TRUE, nnF = c(10, 20, 30)
)

res$oc

# Here, nnE = nnF, and no wiggle room is allowed. Random distance also not supplied.
res <- ocPostprob(
nnE = c(10, 20, 30), truep = 0.40, p0 = 0.20, p1 = 0.30, tL = 0.60, tU = 0.80, parE = c(1, 1),
sim = 50000, wiggle = FALSE, randomdist = TRUE, nnF = c(10, 20, 30)
sim = 50000, wiggle = FALSE, nnF = c(10, 20, 30)
)

res$oc

# Here, we only have one Futility and Efficacy look or stop.
res <- ocPostprob(
nnE = c(10), truep = 0.40, p0 = 0.20, p1 = 0.30, tL = 0.60, tU = 0.80, parE = c(1, 1),
sim = 50000, wiggle = FALSE, randomdist = TRUE, nnF = 10
sim = 50000, wiggle = FALSE, nnF = 10
)

res$oc

# Here, we only have one Futility but many Efficacy looks or stop.
res <- ocPostprob(
nnE = c(10, 20, 30), truep = 0.40, p0 = 0.20, p1 = 0.30, tL = 0.60, tU = 0.80, parE = c(1, 1),
sim = 50000, wiggle = FALSE, randomdist = TRUE, nnF = 10
sim = 50000, wiggle = FALSE, nnF = 10
)

res$oc
14 changes: 5 additions & 9 deletions man/ocPostprob.Rd

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

44 changes: 22 additions & 22 deletions tests/testthat/test-ocPostprob.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,62 +97,62 @@ test_that("the ExpectedN is within range based on vector of looks", {
# ocPostprob ----
test_that("the sum of Eff, Fut, Gray zone probabiliy is 1", {
set.seed(1989)
expect_warning(res1 <- ocPostprob(
expect_warning(result <- ocPostprob(
nnE = 40, truep = 0.5, p0 = 0.45, p1 = 0.45, tL = 0.9, tU = 0.7,
parE = c(1, 1), sim = 10000
), "Advise to use sim >= 50000 to achieve convergence")
results <- sum(res1$oc[5:7])
expect_equal(results, 1)
result_sum <- sum(result$oc[5:7])
expect_equal(result_sum, 1)
})

test_that("the PrFutility increases with increase futility looks", {
set.seed(1989)
expect_warning(res_fut <- ocPostprob(
expect_warning(result_fut <- ocPostprob(
nnE = c(10, 20, 30), truep = 0.40, p0 = 0.20, p1 = 0.30, tL = 0.60, tU = 0.80, parE = c(1, 1),
sim = 10000, wiggle = FALSE, randomdist = TRUE, nnF = c(10, 20, 30)
sim = 10000, wiggle = FALSE, nnF = c(10, 20, 30)
), "Advise to use sim >= 50000 to achieve convergence")

res_fut$oc$PrFutility
expect_warning(res_one_fut <- ocPostprob(
result_fut$oc$PrFutility
expect_warning(result_one_fut <- ocPostprob(
nnE = c(10, 20, 30), truep = 0.40, p0 = 0.20, p1 = 0.30, tL = 0.60, tU = 0.80, parE = c(1, 1),
sim = 10000, wiggle = FALSE, randomdist = TRUE, nnF = 10
sim = 10000, wiggle = FALSE, nnF = 10
), "Advise to use sim >= 50000 to achieve convergence")
res_one_fut$oc$PrFutility
expect_true(res_fut$oc$PrFutility > res_one_fut$oc$PrFutility)
result_one_fut$oc$PrFutility
expect_true(result_fut$oc$PrFutility > result_one_fut$oc$PrFutility)
})

test_that("the PrEfficacy increases with increase Efficacy looks", {
set.seed(1989)
expect_warning(res_eff <- ocPostprob(
expect_warning(result_eff <- ocPostprob(
nnE = c(30), truep = 0.40, p0 = 0.20, p1 = 0.30, tL = 0.60, tU = 0.80, parE = c(1, 1),
sim = 10000, wiggle = FALSE, randomdist = TRUE, nnF = 30
sim = 10000, wiggle = FALSE, nnF = 30
), "Advise to use sim >= 50000 to achieve convergence")

res_eff$oc$PrEfficacy
expect_warning(res_more_eff <- ocPostprob(
result_eff$oc$PrEfficacy
expect_warning(result_more_eff <- ocPostprob(
nnE = c(10, 20, 30), truep = 0.40, p0 = 0.20, p1 = 0.30, tL = 0.60, tU = 0.80, parE = c(1, 1),
sim = 10000, wiggle = FALSE, randomdist = TRUE, nnF = c(10, 20, 30)
sim = 10000, wiggle = FALSE, nnF = c(10, 20, 30)
), "Advise to use sim >= 50000 to achieve convergence")
res_more_eff$oc$PrEfficacy
expect_true(res_more_eff$oc$PrEfficacy > res_eff$oc$PrEfficacy)
result_more_eff$oc$PrEfficacy
expect_true(result_more_eff$oc$PrEfficacy > result_eff$oc$PrEfficacy)
})

# ocPostprob ---
test_that("ocPostprob gives results that are within range to stats::pbinom", {
set.seed(1989)
# Go criteria is P_E(truep >= 0.45) > 0.70
# Stop criteria is P_E(truep <= 0.45) > 0.90
res1 <- ocPostprob(
result <- ocPostprob(
nnE = 40, truep = 0.5, p0 = 0.45, p1 = 0.45, tL = 0.9, tU = 0.7,
parE = c(1, 1), sim = 50000
)
# Pre-calculation indicate that :
# Go criteria: 20 out of 40, means >= 50% response rate
expect_equal(res1$oc$PrEfficacy, 0.56226)
expect_equal(result$oc$PrEfficacy, 0.56226)
p.go <- 1 - pbinom(q = 20 - 1, size = 40, prob = 0.5)
expect_true(abs(p.go - res1$oc$PrEfficacy) < 1e-3)
expect_true(abs(p.go - result$oc$PrEfficacy) < 1e-3)
# Stop criteria: 13 out of 40, means <= 32.5% response rate.
expect_equal(res1$oc$PrFutility, 0.01998)
expect_equal(result$oc$PrFutility, 0.01998)
p.stop <- pbinom(q = 13, size = 40, prob = 0.5)
expect_true(abs(p.stop - res1$oc$PrFutility) < 1e-2)
expect_true(abs(p.stop - result$oc$PrFutility) < 1e-2)
})
2 changes: 1 addition & 1 deletion vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ we issue the following command:
set.seed(4)
results <- ocPostprob(
nnE = c(10, 20, 30), truep = 0.4, p0 = 0.2, p1 = 0.3, tL = 0.6,
tU = 0.8, parE = c(1, 1), sim = 10000, wiggle = FALSE, randomdist = FALSE, nnF = c(10, 20, 30)
tU = 0.8, parE = c(1, 1), sim = 10000, wiggle = FALSE, nnF = c(10, 20, 30)
)
results$oc
```
Expand Down

0 comments on commit 31f939b

Please sign in to comment.