@@ -5,6 +5,49 @@ beta_prob_gt <- function(a, b, M, n_draws) {
55 .Call(`_bcts_beta_prob_gt` , a , b , M , n_draws )
66}
77
8+ # ' @title Analytic Power Calculation for Single-Arm Beta-Binomial Design
9+ # '
10+ # ' @description Computes the exact Bayesian power for a single-arm binomial trial with a
11+ # ' conjugate Beta prior. Power is defined as the probability that the posterior probability
12+ # ' that the true response rate exceeds a threshold \code{M} is greater than or equal to
13+ # ' a prespecified cutoff \code{threshold}, under a fixed true response rate \code{p_t}.
14+ # '
15+ # ' This function avoids simulation and uses a deterministic summation over all possible
16+ # ' outcomes of the binomial distribution.
17+ # '
18+ # ' @param p_t Numeric in \[0, 1\]. True response probability for the treatment arm.
19+ # ' @param n_t Integer. Sample size of the treatment arm.
20+ # ' @param M Numeric in \[0, 1\]. Threshold on the response rate for decision-making,
21+ # ' e.g., \code{M = 0.6}.
22+ # ' @param threshold Numeric in \[0, 1\]. Posterior probability cutoff for declaring success,
23+ # ' e.g., \code{0.95}.
24+ # ' @param prior Character string. Either \code{"flat"} for a Beta(1,1) prior or
25+ # ' \code{"beta"} to specify a custom prior using \code{a_base} and \code{b_base}.
26+ # ' @param a_base Numeric. Alpha parameter for the Beta prior (used only if \code{prior = "beta"}).
27+ # ' @param b_base Numeric. Beta parameter for the Beta prior (used only if \code{prior = "beta"}).
28+ # '
29+ # ' @return A named list with:
30+ # ' \describe{
31+ # ' \item{\code{estimate}}{The exact Bayesian power (a number between 0 and 1).}
32+ # ' \item{\code{mc_se}}{\code{NA_real_}, returned for compatibility (Monte Carlo SE not applicable).}
33+ # ' \item{\code{B}}{\code{NA_integer_}, returned for compatibility with simulation version.}
34+ # ' \item{\code{successes}}{\code{NA_integer_}, returned for compatibility.}
35+ # ' }
36+ # '
37+ # ' @examples
38+ # ' singlearm_beta_power_exact(
39+ # ' p_t = 0.75, n_t = 35, M = 0.6,
40+ # ' threshold = 0.95, prior = "flat"
41+ # ' )
42+ # '
43+ # ' @seealso \code{\link{singlearm_beta_power}} for the simulation-based version.
44+ # '
45+ # ' @author Thomas Debray \email{[email protected] }46+ # ' @export
47+ singlearm_beta_power_exact <- function (p_t , n_t , M , threshold , prior = " flat" , a_base = 1 , b_base = 1 ) {
48+ .Call(`_bcts_singlearm_beta_power_exact` , p_t , n_t , M , threshold , prior , a_base , b_base )
49+ }
50+
851# ' @title Compute p-values for a t-distribution with Fixed Degrees of Freedom
952# '
1053# ' @description Simulates a single-arm binomial trial with a conjugate Beta prior,
@@ -22,21 +65,20 @@ beta_prob_gt <- function(a, b, M, n_draws) {
2265# ' \code{"beta"} to specify a custom prior using \code{a_base} and \code{b_base}.
2366# ' @param a_base Numeric. Alpha parameter for the Beta prior (only used if \code{prior = "beta"}).
2467# ' @param b_base Numeric. Beta parameter for the Beta prior (only used if \code{prior = "beta"}).
25- # ' @param n_draws Integer. Number of posterior draws per trial.
2668# ' @param show_progress Logical. If \code{TRUE}, prints a simple progress bar to console.
2769# '
2870# ' @return A list with: estimate (power), mc_se, successes, B.
2971# '
3072# ' @examples
3173# ' singlearm_beta_power(
3274# ' B = 1000, p_t = 0.75, n_t = 35, M = 0.60,
33- # ' threshold = 0.95, prior = "flat", n_draws = 2000
75+ # ' threshold = 0.95, prior = "flat"
3476# ' )
3577# '
3678# ' @author Thomas Debray \email{[email protected] }3779# ' @export
38- singlearm_beta_power <- function (B , p_t , n_t , M , threshold , prior = " flat" , a_base = 1 , b_base = 1 , n_draws = 2000L , show_progress = TRUE ) {
39- .Call(`_bcts_singlearm_beta_power` , B , p_t , n_t , M , threshold , prior , a_base , b_base , n_draws , show_progress )
80+ singlearm_beta_power <- function (B , p_t , n_t , M , threshold , prior = " flat" , a_base = 1 , b_base = 1 , show_progress = TRUE ) {
81+ .Call(`_bcts_singlearm_beta_power` , B , p_t , n_t , M , threshold , prior , a_base , b_base , show_progress )
4082}
4183
4284# ' @title Estimate Type-I Error for Single-Arm Trial
@@ -52,20 +94,64 @@ singlearm_beta_power <- function(B, p_t, n_t, M, threshold, prior = "flat", a_ba
5294# ' @param prior "flat" or "beta".
5395# ' @param a_base Alpha parameter for Beta prior (if prior = "beta").
5496# ' @param b_base Beta parameter for Beta prior (if prior = "beta").
55- # ' @param n_draws Number of posterior draws per trial.
5697# ' @param show_progress Logical. Show progress in console?
5798# '
5899# ' @return A list with \code{estimate} (type-I error), \code{mc_se}, \code{B}, and \code{rejections}.
59100# '
60101# ' @examples
61102# ' singlearm_beta_type1(
62103# ' B = 1000, n_t = 35, M = 0.6,
63- # ' threshold = 0.95, prior = "flat", n_draws = 2000
104+ # ' threshold = 0.95, prior = "flat"
64105# ' )
65106# '
66107# ' @author Thomas Debray \email{[email protected] }67108# ' @export
68- singlearm_beta_type1 <- function (B , n_t , M , threshold , prior = " flat" , a_base = 1 , b_base = 1 , n_draws = 2000L , show_progress = TRUE ) {
69- .Call(`_bcts_singlearm_beta_type1` , B , n_t , M , threshold , prior , a_base , b_base , n_draws , show_progress )
109+ singlearm_beta_type1 <- function (B , n_t , M , threshold , prior = " flat" , a_base = 1 , b_base = 1 , show_progress = TRUE ) {
110+ .Call(`_bcts_singlearm_beta_type1` , B , n_t , M , threshold , prior , a_base , b_base , show_progress )
111+ }
112+
113+ # ' @title Exact Type-I Error for Single-Arm Trial (Beta-Binomial)
114+ # '
115+ # ' @description Computes the exact Type-I error for a single-arm binomial trial
116+ # ' using a conjugate Beta prior. This is done by summing the probability of all
117+ # ' outcomes where the posterior probability that \eqn{\theta > M} exceeds a
118+ # ' decision threshold \code{threshold}, under the null hypothesis.
119+ # '
120+ # ' By default, the null hypothesis is \eqn{\theta = M}, but a more conservative
121+ # ' frequentist setting can be evaluated by setting \code{p_null < M}.
122+ # '
123+ # ' @param n_t Integer. Sample size of the treatment arm.
124+ # ' @param M Numeric in \[0, 1\]. Decision threshold on the response rate, e.g., \code{M = 0.6}.
125+ # ' @param threshold Numeric in \[0, 1\]. Posterior probability cutoff for declaring success,
126+ # ' e.g., \code{threshold = 0.95}.
127+ # ' @param prior Character string. Either \code{"flat"} for a Beta(1,1) prior or
128+ # ' \code{"beta"} to specify a custom prior using \code{a_base} and \code{b_base}.
129+ # ' @param a_base Numeric. Alpha parameter for the Beta prior (only used if \code{prior = "beta"}).
130+ # ' @param b_base Numeric. Beta parameter for the Beta prior (only used if \code{prior = "beta"}).
131+ # ' @param p_null Optional. True response probability under the null hypothesis (e.g., \code{p_null = 0.6}).
132+ # ' If not specified, defaults to \code{p_null = M} (boundary case).
133+ # '
134+ # ' @return A named list with:
135+ # ' \describe{
136+ # ' \item{\code{estimate}}{Exact Type-I error (a number between 0 and 1).}
137+ # ' \item{\code{mc_se}}{\code{NA_real_}, included for compatibility.}
138+ # ' \item{\code{B}}{\code{NA_integer_}, included for compatibility.}
139+ # ' \item{\code{rejections}}{\code{NA_integer_}, included for compatibility.}
140+ # ' }
141+ # '
142+ # ' @examples
143+ # ' # Type-I error under flat prior at boundary
144+ # ' singlearm_beta_type1_exact(n_t = 40, M = 0.65, threshold = 0.9, prior = "flat")
145+ # '
146+ # ' # Type-I error under true p < M (frequentist view)
147+ # ' singlearm_beta_type1_exact(n_t = 40, M = 0.65, threshold = 0.9,
148+ # ' prior = "flat", p_null = 0.60)
149+ # '
150+ # ' @seealso \code{\link{singlearm_beta_type1}} for the simulation-based version.
151+ # '
152+ # ' @author Thomas Debray \email{[email protected] }153+ # ' @export
154+ singlearm_beta_type1_exact <- function (n_t , M , threshold , prior = " flat" , a_base = 1 , b_base = 1 , p_null = - 1.0 ) {
155+ .Call(`_bcts_singlearm_beta_type1_exact` , n_t , M , threshold , prior , a_base , b_base , p_null )
70156}
71157
0 commit comments