---
title: "Table 3"
author: "Philip He"
date: "2024-04-16"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

After install the package, load the functions in the package.
```{r}
#(1) Install devtools if not installed. Remove # below to install the package

#install.packages("devtools")

#(2) Install BayesianHybridDesign. Remove # below to install the package

#devtools::install_github("phe9480/BayesianHybridDesign")

library(BayesianHybridDesign)
```

## Table 3

This is an R Markdown document for producing Table 3 in the manuscript.

```{r}
##################################
# Table 3 Outputs
##################################


table3 = function(pt = 0.47, pt1=0.37, pt2=0.57,
                  pc = 0.27, pc1=0.17, pc2=0.37,
                  pch = 0.27, pc.calib=0.27, nch = 500, 
                  r = 2,  q = 1, alpha = 0.1, power = 0.8,
                  delta_threshold = 0.1, 
                  method = "Empirical Bayes", theta = 0.5, eta = 1,
                  a0c = 0.001, b0c = 0.001, a0t = 0.001, b0t = 0.001,
                  nsim = 10000, seed = 2000){
  
  #Find out the sample size
  SS = DPP(pt = pt, pc = pc, pch = pch, pc.calib=pc.calib, nch = nch, 
           r = r, q = q, 
           alpha = alpha, power = power,
           delta_threshold = delta_threshold,
           method = method, theta = theta, eta = eta,
           a0c = a0c, b0c = b0c, a0t = a0t, b0t = b0t,
           nsim = nsim, seed = seed)
  
  #output
  out0 = c(SS$nt, SS$nc, SS$nche, pc, pt, 
           SS$type1err, SS$power, SS$mean.PMD)
  
  #smaller pc: type I error
  s.type1err = power.DPP(pt=pc1, nt=SS$nt,
                         pc=pc1, nc=SS$nc,
                         pch=pch,nche=SS$nche,nch=nch,pc.calib=pc.calib,
                         alpha=alpha, tau=SS$tau,
                         delta_threshold = delta_threshold,
                         method = method, theta = theta, eta = eta,
                         a0c = a0c, b0c = b0c, a0t = a0t, b0t = b0t,
                         nsim = nsim, seed = seed)
  s.power = power.DPP(pt=pt1, nt=SS$nt,
                      pc=pc1, nc=SS$nc,
                      pch=pch,nche=SS$nche,nch=nch,pc.calib=pc.calib,
                      alpha=alpha, tau=SS$tau,
                      delta_threshold = delta_threshold,
                      method = method, theta = theta, eta = eta,
                      a0c = a0c, b0c = b0c, a0t = a0t, b0t = b0t,
                      nsim = nsim, seed = seed)
  
  s.out = c(SS$nt, SS$nc, SS$nche, pc1, pt1, 
            s.type1err$power, s.power$power,
            s.power$pc.PMD)
  
  #Bigger pc: type I error
  b.type1err = power.DPP(pt=pc2, nt=SS$nt,
                         pc=pc2, nc=SS$nc,
                         pch=pch,nche=SS$nche,nch=nch,pc.calib=pc.calib,
                         alpha=alpha, tau=SS$tau,
                         delta_threshold = delta_threshold,
                         method = method, theta = theta, eta = eta,
                         a0c = a0c, b0c = b0c, a0t = a0t, b0t = b0t,
                         nsim = nsim, seed = seed)
  b.power = power.DPP(pt=pt2, nt=SS$nt,
                      pc=pc2, nc=SS$nc,
                      pch=pch,nche=SS$nche,nch=nch,pc.calib=pc.calib,
                      alpha=alpha, tau=SS$tau,
                      delta_threshold = delta_threshold,
                      method = method, theta = theta, eta = eta,
                      a0c = a0c, b0c = b0c, a0t = a0t, b0t = b0t,
                      nsim = nsim, seed = seed)
  
  b.out = c(SS$nt, SS$nc, SS$nche, pc2, pt2, 
            b.type1err$power, b.power$power,
            b.power$pc.PMD)
  
  out=rbind(s.out, out0, b.out)
  return(out)
}

###############################################
EBa = table3 (pt = 0.47, pt1=0.37, pt2=0.57,
              pc = 0.27, pc1=0.17, pc2=0.37,
              pch = 0.27, nch = 500, pc.calib=0.27,
              r = 2,  q = 1, alpha = 0.1, power = 0.8,
              delta_threshold = 0.1, 
              method = "Empirical Bayes", theta = 0.5, eta = 1,
              a0c = 0.001, b0c = 0.001, a0t = 0.001, b0t = 0.001,
              nsim = 100000, seed = 2000)

EBb = table3 (pt = 0.47, pt1=0.37, pt2=0.57,
              pc = 0.27, pc1=0.17, pc2=0.37,
              pch = 0.27, nch = 500,pc.calib=0.27,
              r = 2,  q = 1.5, alpha = 0.1, power = 0.8,
              delta_threshold = 0.1, 
              method = "Empirical Bayes", theta = 0.5, eta = 1,
              a0c = 0.001, b0c = 0.001, a0t = 0.001, b0t = 0.001,
              nsim = 100000, seed = 2000)

EBc = table3 (pt = 0.47, pt1=0.37, pt2=0.57,
              pc = 0.27, pc1=0.17, pc2=0.37,
              pch = 0.27, nch = 500,pc.calib=0.27,
              r = 2,  q = 2, alpha = 0.1, power = 0.8,
              delta_threshold = 0.1, 
              method = "Empirical Bayes", theta = 0.5, eta = 1,
              a0c = 0.001, b0c = 0.001, a0t = 0.001, b0t = 0.001,
              nsim = 100000, seed = 2000)

###############################################
Bpa = table3 (pt = 0.47, pt1=0.37, pt2=0.57,
               pc = 0.27, pc1=0.17, pc2=0.37,
               pch = 0.27, nch = 500,pc.calib=0.27,
               r = 2,  q = 1, alpha = 0.1, power = 0.8,
               delta_threshold = 0.1, 
               method = "Bayesian p", theta = 0.5, eta = 1,
               a0c = 0.001, b0c = 0.001, a0t = 0.001, b0t = 0.001,
               nsim = 100000, seed = 2000)

Bpb = table3 (pt = 0.47, pt1=0.37, pt2=0.57,
               pc = 0.27, pc1=0.17, pc2=0.37,
               pch = 0.27, nch = 500,pc.calib=0.27,
               r = 2,  q = 1.5, alpha = 0.1, power = 0.8,
               delta_threshold = 0.1, 
               method = "Bayesian p", theta = 0.5, eta = 1,
               a0c = 0.001, b0c = 0.001, a0t = 0.001, b0t = 0.001,
               nsim = 100000, seed = 2000)

Bpc = table3 (pt = 0.47, pt1=0.37, pt2=0.57,
               pc = 0.27, pc1=0.17, pc2=0.37,
               pch = 0.27, nch = 500,pc.calib=0.27,
               r = 2,  q = 2, alpha = 0.1, power = 0.8,
               delta_threshold = 0.1, 
               method = "Bayesian p", theta = 0.5, eta = 1,
               a0c = 0.001, b0c = 0.001, a0t = 0.001, b0t = 0.001,
               nsim = 100000, seed = 2000)

###############################################
#EESS expected ESS
###############################################

theta = rep(0.5, 63)
eta = c(rep(NA, 9), rep(1, 9), rep(2,9), rep(2, 9), rep(3,9), rep(1,9), rep(0.5, 9))
eess = rep(NA, 63)
method = c(rep("Empirical Bayes", 9), rep("Generalized BC", 18), rep("JSD", 18), rep("Bayesian p", 18))

for (i in 1:nrow(tbl)){
  nc = as.numeric(tbl[i,4])
  nche = as.numeric(tbl[i, 5])
  pc=as.numeric(tbl[i,6])
  
  eess[i] = EESS(pc=pc,nc=nc,pch=0.27,nche=nche,nch=500,
       a0c=0.001,b0c=0.001,delta_threshold=0.1,
       method=method[i], theta=theta[i], eta=eta[i])
  
}

round(eess, 2)
  
###############################################
#automatically produce a latex table format
# install.packages("dplyr")
# install.packages("kableExtra")
library(dplyr)
library(kableExtra)

out = rbind(EBa, EBb, EBc, Bpa, Bpb, Bpc)
tbl = cbind(out[,1:5], round(eess, 2), round(out[,6:7], 4), round(out[,8]*100,2))
  
tbl = cbind(tbl, round(eess, 2))

tbl %>%
  kbl(caption="Table 3: Hybrid design for a novel combination therapy with borrowing from a historical trial KEYNOTE-042 of monotherapy in NSCLC  using empirical Bayes method",
      format= "latex",
      col.names = c("method", "ratio", "nt", "nc", "nche","EESS", 
                    "pc", "pt", "type1err", "power",
                    "pc.PMD"),
      align="c") %>%
  kable_classic(full_width = F, html_font = "helvetica")
  
  






```
