---
title: "Probability of Success at an Interim Analysis"
author: "Yue Li"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Probability of Success at an Interim Analysis}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
---
  
```{r init, include=FALSE}
library(ggplot2)
library(scales)
library(knitr)
library(dplyr)
library(RBesT)
theme_set(theme_bw())
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=50, RBesT.MC.iter=100, RBesT.MC.chains=2, RBesT.MC.thin=1)
}
```

At different stages of drug development, there are questions about how
likely a study will be successful given previously collected data
within the trial itself or data from other earlier trials. For example
given Ph2a (PoC, proof of concept) and Ph2b (DRF, dose range finding)
studies, how likely a new Ph3 study would be successful is of great
interest. Another example, at an interim analysis (IA) of a PoC study,
one would also be interested to understand the probability of success
at the end of the study given the partial data observed. **`pos1S()`**
and **`pos2S()`** are constructed to calculate predictive
probabilities for this purpose to inform quantitative decision
making. This vignette shows an example from an IA in a PoC study,
where **`pos2S()`** was used to explore the probability of success for
the final analysis given the interim data.

The primary endpoint for this study is log transformed facial lesion
count, assumed to be normally distributed. Decrease in the lesion count
upon treatment is considered improvement in the patients. Below is the
summary statistics of this primary endpoint by group at the interim.

```{r ia data summary}
ia <- data.frame(n=c(12, 14),
                 median_count=c(20.5, 21),
                 mean_count=c(23.3, 27),
                 mean_log=c(2.96, 3.03),
                 sd_log=c(0.67, 0.774),
                 row.names=c("active", "placebo")) %>%
    transform(se_log=round(sd_log/sqrt(n), 3))
sd_log_pooled <- with(ia, sqrt(sum(sd_log^2*(n-1))/(sum(n)-2)))
kable(ia)
```

The predefined dual PoC criteria is as follows,
```{r rules, eval=TRUE}
n <- 21 # planned total n per arm
rules <- decision2S(c(0.9, 0.5), c(0,-0.357), lower.tail = TRUE)
print(rules)
```

```{r rules2, echo=FALSE, eval=TRUE}
rule1 <- decision2S(0.9, 0, lower.tail = TRUE)
rule2 <- decision2S(0.5, -0.357, lower.tail = TRUE)
```

The interim data were evaluated against the PoC criteria with weakly
informative priors for both active and placebo groups. The criteria
were not met, although it seemed to show some benefit of the active
treatment over placebo numerically. The variability of this endpoint
is higher than what was assumed for study sample size calculation.

```{r ia}
priorP <- priorT <- mixnorm(c(1, log(20), 1), sigma = 0.47, param = 'mn')
## posterior at IA data
postT_interim <- postmix(priorT, m=ia["active","mean_log"], se=ia["active","se_log"])
postP_interim <- postmix(priorP, m=ia["placebo","mean_log"], se=ia["placebo","se_log"])
pmixdiff(postT_interim, postP_interim, 0)
pmixdiff(postT_interim, postP_interim,-0.357)
```

The probability of success at the final analysis, i.e. the probability
of meeting PoC criteria at trial completion given observed interim
data, was computed using function **`pos2S()`**. One could assume that
the new data after the interim would be from the same distribution as
the interim data. If the $\sigma_{1}$ and $\sigma_{2}$ in
**`pos2S()`** were not specified, i.e. the previously assumed $\sigma$
would be used.
```{r pp}
pos_final <- pos2S(
  postT_interim,
  postP_interim,
  n - ia["active","n"],
  n - ia["placebo","n"],
  rules,
  sigma1 = sd_log_pooled,
  sigma2 = sd_log_pooled
  )
```

The function constructed by **`pos2S()`** can produce the predictive
probability given any defined distribution for the two groups. For
example, if the interim posterior distributions are used, the
calculated probability is small, suggesting a low chance of success at
the final analysis given observed IA data.
```{r ppout}
pos_final(postT_interim, postP_interim)
```

One can also use **`oc2S()`** to compute conditional power for any
given treatment effect.
```{r oc, fig.height=4,fig.width=4*1.62}
ia_oc <- oc2S(
    postT_interim,
    postP_interim,
    n - ia["active","n"],
    n - ia["placebo","n"],
    rules,
    sigma1 = sd_log_pooled,
    sigma2 = sd_log_pooled
    )
  
delta <- seq(0, 0.9, 0.01) #pct diff from pbo
pbomean <- ia["placebo","mean_log"]
y1 <- log(exp(pbomean) * (1 - delta)) #active
y2 <- log(exp(pbomean) * (1 - 0 * delta)) #placebo
  
out <-
    data.frame(
        diff_pct = delta,
        diff = round(y1 - y2, 2),
        y_act = y1,
        y_pbo = y2,
        cp = ia_oc(y1, y2)
        )
  
ggplot(data = out, aes(x = diff_pct, y = cp)) + geom_line() +
    scale_x_continuous(labels = scales::percent) +
        labs(y = 'Conditional power',
             x = 'True percentage difference from placebo in lesion count',
             title = 'Conditional power at interim for success at final analysis')
```


### R Session Info

```{r session, echo=FALSE, eval=TRUE}
sessionInfo()
```


```{r,include=FALSE}
options(.user_mc_options)
```
