---
title: "Bayesian Hybrid Design - Dynamic Borrowing Plot"
author: "Philip He"
date: "2024-04-15"
output:
  pdf_document: default
  html_document:  
    number_sections: true
    toc: true
    toc_float: true
---

```{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)
```

```{r}
library(BayesianHybridDesign)
```

# Dynamic Borrowing: Empirical Bayes

```{r}
nc = 40

rc = w.EB = 1:nc
for (i in 1:length(rc)){
  w.EB[i] = borrow.wt(Yc=rc[i], nc=nc, Ych=60, nch=200, nche=40, 
          a0c=0.001, b0c=0.001, delta_threshold=0.1, method="Empirical Bayes")$wd
}

png("w.EB.png", width=6, height=4, units="in", res = 1200)
plot(rc, w.EB, type="n", ylim=c(0, max(w.EB)), 
     xlab="Number of Responders in concurrent control out of 40",
     ylab="")

lines(rc, w.EB, col=1)
dev.off()

```

# Dynamic Borrowing: Bayesian P

```{r}
nc = 40

rc = 1:nc; eta = c(0.5, 1, 1.5)

w.Bp = matrix(NA, nrow=length(rc), ncol=length(eta))
for (i in 1:length(rc)){
  for (j in 1:length(eta)){
  w.Bp[i,j] = borrow.wt(Yc=rc[i], nc=nc, Ych=60, nch=200, nche=40, 
          a0c=0.001, b0c=0.001, delta_threshold=0.1, method="Bayesian p", eta=eta[j])$wd
  }
}

png("w.Bp.png", width=6, height=4, units="in", res = 1200)
plot(rc, w.Bp[,1], type="n", ylim=c(0, max(w.Bp)), 
     xlab="Number of Responders in concurrent control out of 40",
     ylab="")
for (j in 1:length(eta)){
  lines(rc, w.Bp[,j], col=j, lty=j)
}

legend("topright", c(expression("Bayesian P ("*eta*"=0.5)"), 
                     expression("Bayesian P ("*eta*"=1)"),
                     expression("Bayesian P ("*eta*"=1.5)")), 
       lty=1:length(eta), col=1:length(eta), bty="n")

dev.off()

```

# Dynamic Borrowing: Generalized Bhattacharyya Coefficient

```{r}
nc = 40
theta = c(0.3, 0.4, 0.5, 0.5, 0.5); eta=c(1,1,1,2,3)
rc = 1:nc
w.GBC = matrix(NA, nrow = length(rc), ncol = length(theta))

for (i in 1:length(rc)){
  for (j in 1:length(theta)){
    w.GBC[i,j] = borrow.wt(Yc=rc[i], nc=nc, Ych=60, nch=200, nche=40, 
                       a0c=0.001, b0c=0.001, delta_threshold=0.1, 
                       method="Generalized BC", theta=theta[j], eta=eta[j])$wd
  }
}

png("w.GBC.png", width=6, height=4, units="in", res = 1200)
plot(rc, w.GBC[,1], type="n", ylim=c(0, max(w.GBC)), 
     xlab="Number of Responders in concurrent control out of 40",
     ylab="")
for (j in 1:length(theta)){
  lines(rc, w.GBC[,j], col=j, lty=j)
}

legend("topright", legend=c(expression(theta~": 0.3;"~eta~" = 1"),
                            expression(theta~": 0.4;"~eta~" = 1"),
                            expression(theta~": 0.5;"~eta~" = 1"),
                            expression(theta~": 0.5;"~eta~" = 2"),
                            expression(theta~": 0.5;"~eta~" = 3")),
       col=1:5, lty=1:5, cex=0.8, bty="n")

dev.off()
```

# Dynamic Borrowing: Jensen-Shannon Divergence (JSD) Method

```{r}
nc = 40
eta = c(1, 2, 3, 4, 5)

rc = 1:(nc-1)
w.JSD = matrix(NA, nrow = length(rc), ncol = length(eta))

for (i in 1:length(rc)){
  for (j in 1:length(eta)){
    w.JSD[i,j] = borrow.wt(Yc=rc[i], nc=nc, Ych=60, nch=200, nche=40, 
                       a0c=0.001, b0c=0.001, delta_threshold=0.1, method="JSD", eta=eta[j])$wd
  }
}

png("w.JSD.png", width=6, height=4, units="in", res = 1200)
plot(rc, w.JSD[,1], type="n", ylim=c(0, max(w.JSD)), 
     xlab="Number of Responders in concurrent control out of 40",
     ylab="")
for (j in 1:length(eta)){
  lines(rc, w.JSD[,j], col=j, lty=j)
}

legend("topright", legend=c(expression("JSD("*eta*"=1)"),
                            expression("JSD("*eta*"=2)"),
                            expression("JSD("*eta*"=3)"),
                            expression("JSD("*eta*"=4)"),
                            expression("JSD("*eta*"=5)")),
       col=1:5, lty=1:5, bty="n")
dev.off()
```


