Skip to content

Commit 7821d55

Browse files
committed
test for old and fixed version
1 parent 3fafb34 commit 7821d55

File tree

6 files changed

+97
-7774
lines changed

6 files changed

+97
-7774
lines changed

R/dbetabinom.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ h_getBetamixPost <- function(x, n, par, weights) {
9696
)
9797
# We compute the updated weights of the posterior
9898
postWeights <- weights * tmp / sum(weights * tmp)
99-
# postWeights <- exp(log(weights) + log(tmp)) - sum(weights * tmp)
10099
assert_numeric(postWeights)
101100
list(
102101
par = postPar,
@@ -211,10 +210,9 @@ qbetaMix <- function(p, par, weights, lower.tail = TRUE) {
211210
grid <- seq(0, 1, len = 31)
212211
f_grid <- .pbetaMix(grid, par, weights, lower.tail = lower.tail)
213212

214-
# diff <- f_grid - p
215-
# pos <- diff > 0
216-
# grid_interval <- c(grid[!pos][which.max(diff[!pos])], grid[pos][which.min(diff[pos])])
217-
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])])
218216

219217
sapply(p, function(p) {
220218
# special cases

R/ocPostprob.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ h_get_looks <- function(dist, nnE, nnF) {
102102
h_get_decision <- function(nnr,
103103
truep,
104104
p0, p1,
105-
parE = c(1, 1),
105+
parE,
106106
nnE,
107107
nnF,
108108
tL,

man/h_get_decision.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-ocPostprob.R

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ test_that("h_get_decision will give GO decision in favourable conditions", {
6666
p1 = 0.5,
6767
tL = 0.2,
6868
tU = 0.3,
69+
parE = c(1,1),
6970
nnE = c(10, 20, 30),
7071
nnF = c(10, 20, 30),
7172
orig_nnr = orig_nnr
@@ -161,3 +162,93 @@ test_that("ocPostprob gives results that are within range to stats::pbinom", {
161162
p.stop <- pbinom(q = 13, size = 40, prob = 0.5)
162163
expect_true(abs(p.stop - result$oc$PrFutility) < 1e-2)
163164
})
165+
166+
test_that("hard coded parameters fail and give incorrect results in old_ocPostprob", {
167+
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(
227+
nnE = 30,
228+
truep = 0.4,
229+
p0 = 0.2,
230+
p1 = 0.5,
231+
tL = 0.7,
232+
tU = 0.3,
233+
parE = c(4, 24),
234+
sim = 100,
235+
wiggle = TRUE,
236+
nnF = 10), "Advise to use sim >= 50000 to achieve convergence")
237+
# result_old$oc
238+
expect_warning(result_new <- ocPostprob(
239+
nnE = 30,
240+
truep = 0.4,
241+
p0 = 0.2,
242+
p1 = 0.5,
243+
tL = 0.7,
244+
tU = 0.3,
245+
parE = c(4, 24),
246+
sim = 100,
247+
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"])
253+
}
254+
)

tests/testthat/test-ocPostprobDist.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,4 @@ test_that("the PrEfficacy increases with increase Efficacy looks", {
166166
), "Advise to use sim >= 50000 to achieve convergence")
167167
expect_true(res_more_eff$oc$PrEfficacy > res_eff$oc$PrEfficacy)
168168
})
169+

0 commit comments

Comments
 (0)