## ----include=FALSE------------------------------------------------------------
library(SAMprior)
library(knitr)
knitr::opts_chunk$set(
    fig.width = 1.62*4,
    fig.height = 4
    )
## setup up fast sampling when run on CRAN
is_CRAN <- !identical(Sys.getenv("NOT_CRAN"), "true")
## NOTE: for running this vignette locally, please uncomment the
## following line:
## is_CRAN <- FALSE
.user_mc_options <- list()
if (is_CRAN) {
    .user_mc_options <- options(RBesT.MC.warmup=250, RBesT.MC.iter=500, RBesT.MC.chains=2, RBesT.MC.thin=1, RBesT.MC.control=list(adapt_delta=0.9))
}

## ----results="asis",echo=FALSE------------------------------------------------
## Current trial data
ASAS20 <- data.frame(study = c('Baeten (2013)', 'Deodhar (2016)', 'Deodhar (2019)',
                               'Erdes (2019)', 'Huang (2019)', 'Kivitz (2018)',
                               'Pavelka (2017)', 'Sieper (2017)', 'Van der Heijde (2018)'),
                     n = c(6, 122, 104, 23, 153, 117, 76, 74, 87),
                     r = c(1, 35, 31, 10, 56, 55, 28, 21, 35))
kable(ASAS20)

## -----------------------------------------------------------------------------
# load R packages
library(ggplot2)
theme_set(theme_bw()) # sets up plotting theme
set.seed(22)
map_ASAS20 <- gMAP(cbind(r, n-r) ~ 1 | study,
                   family = binomial,
                   data = ASAS20, 
                   tau.dist = "HalfNormal", 
                   tau.prior = 1,
                   beta.prior = 2)

map_automix <- automixfit(map_ASAS20)
map_automix
plot(map_automix)$mix

## ----message=FALSE------------------------------------------------------------
n <- 35; r = 10 
wSAM_LRT <- SAM_weight(if.prior = map_automix, 
                       delta = 0.2,
                       n = n, r = r)
                   
cat('SAM weight: ', wSAM_LRT)

## ----message=FALSE------------------------------------------------------------
wSAM_PPR <- SAM_weight(if.prior = map_automix, 
                       delta = 0.2,
                       method.w = 'PPR',
                       prior.odds = 3/7,
                       n = n, r = r)
cat('SAM weight: ', wSAM_PPR)

## ----echo=FALSE, message=FALSE------------------------------------------------
weight_grid <- seq(1, 34, by = 1)
weight_res  <- lapply(1:34, function(x) SAM_weight(if.prior = map_automix, 
                                                   delta = 0.2,
                                                   n = 35, r = x))
df_weight <- data.frame(grid   = weight_grid/35,
                        weight = unlist(weight_res))
qplot(grid, weight, data = df_weight, geom = "line", main= "SAM Weight") +
  xlab('Response Rate from Control trial')+ ylab('Weight') +
  geom_vline(xintercept = summary(map_automix)['mean'], linetype = 2, col = 'blue') 

## ----message=FALSE------------------------------------------------------------
SAM.prior <- SAM_prior(if.prior = map_automix, 
                       nf.prior = mixbeta(nf.prior = c(1,1,1)),
                       weight = wSAM_LRT)
SAM.prior

## ----message=FALSE------------------------------------------------------------
TypeI <- get_OC(if.prior = map_automix,       ## MAP prior from historical data
                nf.prior = mixbeta(c(1,1,1)), ## Non-informative prior for mixture prior
                prior.t = mixbeta(c(1,1,1)),  ## Non-informative prior for treatment arm
                delta    = 0.2,               ## CSD for SAM prior
                ## Method to determine the mixture weight for the SAM prior
                method.w = 'LRT',             
                n        = 35, n.t = 70,      ## Sample size for control and treatment arms
                if.rMAP   = TRUE,             ## Output robust MAP prior for comparison
                weight   = 0.5,               ## Weight for robust MAP prior
                ## Trial settings
                alternative = "greater", ## Direction of the posterior decision. Must be one of "greater" or "less"
                margin = 0,              ## Clinical margin
                ## Response rates for control and treatment arms
                theta    = c(summary(map_automix)['mean'], 0.30, 0.40, 0.60),
                theta.t  = c(summary(map_automix)['mean'], 0.30, 0.38, 0.61))
kable(TypeI)

## ----message=FALSE------------------------------------------------------------
Power <- get_OC(if.prior = map_automix,       ## MAP prior from historical data
                nf.prior = mixbeta(c(1,1,1)), ## Non-informative prior for mixture prior
                prior.t = mixbeta(c(1,1,1)),  ## Non-informative prior for treatment arm
                delta    = 0.2,               ## CSD for SAM prior
                ## Method to determine the mixture weight for the SAM prior
                method.w = 'LRT',             
                n        = 35, n.t = 70,      ## Sample size for control and treatment arms
                if.rMAP   = TRUE,             ## Output robust MAP prior for comparison
                weight   = 0.5,               ## Weight for robust MAP prior
                ## Trial settings
                alternative = "greater", ## Direction of the posterior decision. Must be one of "greater" or "less"
                margin = 0,              ## Clinical margin,
                ## Response rates for control and treatment arms
                theta    = c(summary(map_automix)['mean'], 0.36, 0.42, 0.16),
                theta.t  = c(summary(map_automix)['mean'], 0.56, 0.62, 0.36))
kable(Power)

## -----------------------------------------------------------------------------
## Sample size and number of responses for treatment arm
n_t <- 70; x_t <- 22 
## Calibrate the posterior probability cutoff 
PPC <- calibrate_cutoff_2arm(if.prior = map_automix,        ## MAP prior from historical data
                             nf.prior = mixbeta(c(1,1,1)),  ## Non-informative prior for mixture prior
                             prior.t = mixbeta(c(1,1,1)),   ## Non-informative prior for treatment arm
                             target = 0.05,                 ## Targeted Type I error rate
                             n.t = n_t, n = n,              ## Sample size for treatment and control arms, respectively
                             theta.t = summary(map_automix)['mean'],  ## The true response rate for treatment arm
                             theta = summary(map_automix)['mean'],    ## The true response rate for control arm
                             ## Method to determine the mixture weight for the SAM prior
                             method = 'SAM',    ## Method 
                             delta = 0.2,       ## CSD for SAM prior
                             ## Trial settings
                             alternative = "greater", ## Direction of the posterior decision. Must be one of "greater" or "less".
                             margin = 0      ## Clinical margin
                             )

## ----include=F----------------------------------------------------------------
cat("Calibrated posterior probability cutoff:", round(PPC$cutoff, 4), "\n")

## -----------------------------------------------------------------------------
## first obtain posterior distributions...
post_SAM <- postmix(priormix = SAM.prior,         ## SAM Prior
                    r = r,   n = n)
post_trt <- postmix(priormix = mixbeta(c(1,1,1)), ## Non-informative prior
                    r = x_t, n = n_t)

## Define the decision function
decision = decision2S(PPC$cutoff, 0, lower.tail=FALSE)

## Decision-making
decision(post_trt, post_SAM)

## -----------------------------------------------------------------------------
sessionInfo()

## ----include=FALSE------------------------------------------------------------
options(.user_mc_options)

