Skip to content

Commit 83f2409

Browse files
committed
tests added
1 parent 7821d55 commit 83f2409

File tree

4 files changed

+109
-81
lines changed

4 files changed

+109
-81
lines changed

R/dbetabinom.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,6 @@ qbetaMix <- function(p, par, weights, lower.tail = TRUE) {
210210
grid <- seq(0, 1, len = 31)
211211
f_grid <- .pbetaMix(grid, par, weights, lower.tail = lower.tail)
212212

213-
diff <- f_grid - p
214-
pos <- diff > 0
215-
grid_interval <- c(grid[!pos][which.max(diff[!pos])], grid[pos][which.min(diff[pos])])
216-
217213
sapply(p, function(p) {
218214
# special cases
219215
if (p == 0) {
@@ -229,7 +225,7 @@ qbetaMix <- function(p, par, weights, lower.tail = TRUE) {
229225

230226
uniroot(
231227
f = function(q) .pbetaMix(q, par, weights, lower.tail = lower.tail) - p,
232-
interval = grid_interval,
228+
interval = grid_interval, # more precise x-axis for root finding
233229
f.lower = -p,
234230
f.upper = 1 - p,
235231
tol = sqrt(.Machine$double.eps)

tests/testthat/test-ocPostprob.R

Lines changed: 17 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -163,92 +163,33 @@ test_that("ocPostprob gives results that are within range to stats::pbinom", {
163163
expect_true(abs(p.stop - result$oc$PrFutility) < 1e-2)
164164
})
165165

166-
test_that("hard coded parameters fail and give incorrect results in old_ocPostprob", {
166+
test_that("two function calls that differ in parE does not give the same result.", {
167167
set.seed(1989)
168-
old_ocPostprob <- function(nnE, truep, p0, p1, tL, tU, parE = c(1, 1),
169-
sim = 50000, wiggle = FALSE, nnF = nnE) {
170-
nn <- sort(unique(c(nnF, nnE)))
171-
Nmax <- max(nn)
172-
assert_number(sim, lower = 1, finite = TRUE)
173-
assert_flag(wiggle)
174-
if (sim < 50000) {
175-
warning("Advise to use sim >= 50000 to achieve convergence")
176-
}
177-
decision <- vector(length = sim)
178-
all_sizes <- vector(length = sim)
179-
all_looks <- vector(length = sim)
180-
for (k in seq_len(sim)) {
181-
if (length(nn) != 1 && wiggle) {
182-
dist <- h_get_distance(nn = nn)
183-
nnr <- h_get_looks(dist = dist, nnE = nnE, nnF = nnF)
184-
nnrE <- nnr$nnrE
185-
nnrF <- nnr$nnrF
186-
orig_nnE <- nnE
187-
orig_nnF <- nnF
188-
} else {
189-
dist <- 0
190-
nnrE <- nnE
191-
nnrF <- nnF
192-
orig_nnE <- nnrE
193-
orig_nnF <- nnrF
194-
}
195-
nnr <- unique(c(nnrE, nnrF))
196-
orig_nnr <- unique(c(orig_nnE, orig_nnF))
197-
tmp <- h_get_decision(
198-
nnr = nnr,
199-
truep = truep,
200-
p0 = p0,
201-
p1 = p1,
202-
parE = c(1,1),
203-
nnE = nnrE,
204-
nnF = nnrF,
205-
tL = tL,
206-
tU = tU,
207-
orig_nnr = orig_nnr
208-
)
209-
decision[k] <- tmp$decision
210-
all_sizes[k] <- tmp$all_sizes
211-
all_looks[k] <- tmp$all_looks
212-
}
213-
oc <- h_get_oc(all_sizes = all_sizes, Nmax = Nmax, decision = decision)
214-
list(
215-
oc = oc,
216-
Decision = decision,
217-
Looks = all_looks,
218-
SampleSize = all_sizes,
219-
union_nn = nnr,
220-
wiggled_nnrE = nnrE,
221-
wiggled_nnrF = nnrF,
222-
dist = dist,
223-
params = as.list(match.call(expand.dots = FALSE))
224-
)
225-
}
226-
expect_warning(result_old <- old_ocPostprob(
168+
expect_warning(result_uniform_hard_coded <- ocPostprob(
227169
nnE = 30,
228170
truep = 0.4,
229-
p0 = 0.2,
230-
p1 = 0.5,
231-
tL = 0.7,
171+
p0 = 0.1,
172+
p1 = 0.3,
173+
tL = 0.8,
232174
tU = 0.3,
233-
parE = c(4, 24),
175+
parE = c(1, 1), # weak prior gives more PrGrayZone
234176
sim = 100,
235177
wiggle = TRUE,
236-
nnF = 10), "Advise to use sim >= 50000 to achieve convergence")
237-
# result_old$oc
238-
expect_warning(result_new <- ocPostprob(
178+
nnF = 30), "Advise to use sim >= 50000 to achieve convergence")
179+
# result_uniform_hard_coded$oc
180+
expect_warning(result_no_hard_code <- ocPostprob(
239181
nnE = 30,
240182
truep = 0.4,
241-
p0 = 0.2,
242-
p1 = 0.5,
243-
tL = 0.7,
183+
p0 = 0.1,
184+
p1 = 0.3,
185+
tL = 0.8,
244186
tU = 0.3,
245-
parE = c(4, 24),
187+
parE = c(4, 10), # stronger prior gives higher PrEfficacy
246188
sim = 100,
247189
wiggle = TRUE,
248-
nnF = 10), "Advise to use sim >= 50000 to achieve convergence")
249-
# result_new$oc
250-
expect_true(result_new$oc["PrStopEarly"] != result_old$oc["PrStopEarly"])
251-
expect_true(result_old$oc["PrFutility"] < result_new$oc["PrFutility"])
252-
expect_true(result_old$oc["PrGrayZone"] < result_new$oc["PrGrayZone"])
190+
nnF = 30), "Advise to use sim >= 50000 to achieve convergence")
191+
# result_no_hard_code$oc
192+
expect_true(result_no_hard_code$oc["PrEfficacy"] > result_uniform_hard_coded$oc["PrEfficacy"])
193+
expect_true(result_no_hard_code$oc["PrGrayZone"] < result_uniform_hard_coded$oc["PrGrayZone"])
253194
}
254195
)

tests/testthat/test-ocPostprobDist.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,38 @@ test_that("the PrEfficacy increases with increase Efficacy looks", {
167167
expect_true(res_more_eff$oc$PrEfficacy > res_eff$oc$PrEfficacy)
168168
})
169169

170+
test_that("two function calls that differ in parE does not give the same result.", {
171+
set.seed(1989)
172+
expect_warning(result_uniform_hard_coded <- ocPostprobDist(
173+
nnE = 30,
174+
truep = 0.15,
175+
deltaE = 0.1,
176+
deltaF = -0.1,
177+
tL = 0.45,
178+
tU = 0.4,
179+
parE = c(1, 1), # PrGrayZone higher due to same, weak priors
180+
parS = c(1, 1),
181+
sim = 100,
182+
wiggle = FALSE,
183+
nnF = 30
184+
), "Advise to use sim >= 50000 to achieve convergence")
185+
result_uniform_hard_coded$oc
186+
expect_warning(result_no_hard_code <- ocPostprobDist(
187+
nnE = 30,
188+
truep = 0.15,
189+
deltaE = 0.1,
190+
deltaF = -0.1,
191+
tL = 0.45,
192+
tU = 0.4,
193+
parE = c(5, 10), # PrFutility lower due to stronger prior
194+
parS = c(1, 1),
195+
sim = 100,
196+
wiggle = FALSE,
197+
nnF = 30
198+
), "Advise to use sim >= 50000 to achieve convergence")
199+
result_no_hard_code$oc
200+
expect_true(result_no_hard_code$oc["PrFutility"] > result_uniform_hard_coded$oc["PrFutility"])
201+
expect_true(result_no_hard_code$oc["PrGrayZone"] < result_uniform_hard_coded$oc["PrGrayZone"])
202+
}
203+
)
204+

tests/testthat/test-ocRctPostprobDist.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,59 @@ test_that("ocRctPostprobDist gives higher PrEfficacy with increased pE", {
226226
), "Advise to use sim >= 50000 to achieve convergence")
227227
expect_true(res_high_truep$oc$PrEfficacy > res_eff$oc$PrEfficacy)
228228
})
229+
230+
test_that("two function calls that differ in parE does not give the same result.", {
231+
set.seed(1989)
232+
input <- list(
233+
nnE = 30,
234+
pE = 0.4,
235+
pS = 0.2,
236+
deltaE = 0.1,
237+
deltaF = 0.1,
238+
relativeDelta = FALSE,
239+
tL = 0.8,
240+
tU = 0.8,
241+
parE = c(a = 4, b = 10),
242+
parS = c(a = 1, b = 1),
243+
nnF = 30,
244+
sim = 5,
245+
Nmax = 15
246+
)
247+
expect_warning(result_uniform_hard_coded <- ocRctPostprobDist(
248+
nnE = input$nnE,
249+
pE = input$pE,
250+
pS = input$pS,
251+
deltaE = input$deltaE,
252+
deltaF = input$deltaF,
253+
relativeDelta = TRUE,
254+
tL = input$tL,
255+
tU = input$tU,
256+
parE = c(1, 1),
257+
parS = input$parS,
258+
randRatio = 1,
259+
sim = 50,
260+
wiggle = FALSE,
261+
nnF = 10
262+
), "Advise to use sim >= 50000 to achieve convergence")
263+
result_uniform_hard_coded$oc
264+
expect_warning(result_no_hard_code <- ocRctPostprobDist(
265+
nnE = input$nnE,
266+
pE = input$pE,
267+
pS = input$pS,
268+
deltaE = input$deltaE,
269+
deltaF = input$deltaF,
270+
relativeDelta = TRUE,
271+
tL = input$tL,
272+
tU = input$tU,
273+
parE = input$parE, # stronger prior, higher difference needed to pass Go
274+
parS = input$parS,
275+
randRatio = 1,
276+
sim = 50,
277+
wiggle = FALSE,
278+
nnF = 10
279+
), "Advise to use sim >= 50000 to achieve convergence")
280+
result_no_hard_code$oc
281+
expect_true(result_no_hard_code$oc["PrEfficacy"] < result_uniform_hard_coded$oc["PrEfficacy"])
282+
expect_true(result_no_hard_code$oc["PrGrayZone"] > result_uniform_hard_coded$oc["PrGrayZone"])
283+
}
284+
)

0 commit comments

Comments
 (0)