# BRDT Examples

library(BRDT)

### Binomial RDT Design

###Generate the prior distribution of failure probability
##Beta is conjugate prior to binomial distribution
#Get the non-informative prior Beta(1, 1)
pi <- pi_MCSim_beta(M = 5000, seed = 10, a = 1, b = 1)

#Get the consumer's risk
n = 10
R <- 0.8
c <- 2
b_CR <- bconsumerrisk(n = n, c = c, pi = pi, R = R)
print(b_CR)
#>           [,1]
#> [1,] 0.3330482

##As n increases, CR decreases
#Get the optimal test sample size
thres_CR <- 0.05 #CR < 0.05
b_n <- boptimal_n(c = c, pi = pi, R = R, thres_CR = thres_CR)
print(b_n)
#> [1] 24

### Optimal Test Plans with Minimum Overall Costs

#Vectors to get combinations of different R and c
Rvec <- seq(0.8, 0.85, 0.01)
cvec <- seq(0, 2, 1)

Plan_optimal_cost <- boptimal_cost(Cf = 10, Cv = 10, G = 100, Cw = 10, N = 100, Rvec = Rvec, cvec = cvec, pi = pi, thres_CR = 0.5)
print(Plan_optimal_cost)
#>   n    R c        CR        AP RDT Cost RG Cost RG Cost Expected  WS Cost
#> 6 4 0.85 0 0.4304362 0.2029711       50     100         79.70289 163.8177
#>   WS Failure Probability WS Cost Expected Overall Cost
#> 6              0.1638177         33.25026     162.9532

### Generate Test Plans Dataset

nvec <- seq(0, 10, 1)
Rvec <- seq(0.8, 0.85, 0.01)
cvec <- seq(0, 2, 1)
pi <- pi_MCSim_beta(M = 5000, seed = 10, a = 1, b = 1)

#Get data from all combinations of n, c, R
data_all <- bdata_generator(Cf = 10, Cv = 10, nvec = nvec, G = 10000, Cw = 10, N = 100, Rvec = Rvec, cvec = cvec, pi = pi, par = all(), option = c("all"), thres_CR = 0.05)
#>   n   R c        CR        AP RDT Cost RG Cost RG Cost Expected  WS Cost
#> 1 0 0.8 0 0.7948000 1.0000000       10   10000            0.000 500.3654
#> 2 1 0.8 0 0.6300625 0.4996346       20   10000         5003.654 329.4260
#> 3 2 0.8 0 0.5011545 0.3350420       30   10000         6649.580 246.0267
#> 4 3 0.8 0 0.3994328 0.2526127       40   10000         7473.873 196.5127
#> 5 4 0.8 0 0.3189480 0.2029711       50   10000         7970.289 163.8177
#> 6 5 0.8 0 0.2551039 0.1697208       60   10000         8302.792 140.6811
#>   WS Failure Probability WS Cost Expected Overall Cost
#> 1              0.5003654        500.36539     510.3654
#> 2              0.3294260        164.59264    5188.2465
#> 3              0.2460267         82.42927    6762.0096
#> 4              0.1965127         49.64160    7563.5146
#> 5              0.1638177         33.25026    8053.5392
#> 6              0.1406811         23.87651    8386.6681

#Get data with optimal test sample size and minimum overall costs from all combinations of c, R
data_optimal <- bdata_generator(Cf = 10, Cv = 10, nvec = nvec, G = 10000, Cw = 10, N = 100, Rvec = Rvec, cvec = cvec, pi = pi, par = all(), option = c("optimal"), thres_CR = 0.05)
#>    n    R c         CR         AP RDT Cost RG Cost RG Cost Expected  WS Cost
#> 1 13 0.80 0 0.04379640 0.07316579      140   10000         9268.342 67.02070
#> 2 13 0.81 0 0.04952572 0.07316579      140   10000         9268.342 67.02070
#> 3 14 0.82 0 0.04779152 0.06826217      150   10000         9317.378 62.88279
#> 4 15 0.83 0 0.04739077 0.06396965      160   10000         9360.303 59.20539
#> 5 16 0.84 0 0.04933936 0.06018231      170   10000         9398.177 55.91351
#> 6 18 0.85 0 0.04545871 0.05380893      190   10000         9461.911 50.26193
#>   WS Failure Probability WS Cost Expected Overall Cost
#> 1             0.06702070         4.903623     9413.246
#> 2             0.06702070         4.903623     9413.246
#> 3             0.06288279         4.292516     9471.671
#> 4             0.05920539         3.787348     9524.091
#> 5             0.05591351         3.365004     9571.542
#> 6             0.05026193         2.704541     9654.615