---
title: "Figure 3"
author: "Zhaohua Lu"
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)
```


```{r}

library(knitr)
library(xtable)

library(foreach)
library(doParallel)

```

# Ratio 2:1, q=1, 1.5, 2, Empirical Bayes for determining $w_d$

```{r}

Res1 = explore.power.DPP(method=c("Empirical Bayes"),
                         pc=c(0.17,0.27,0.37), 
                         nc=23:33,
                         pc.calib = 0.27,
                         q = c(1,1.5,2), 
                         pch=0.27, nch=500,
                         alpha=0.1,
                         a0c=0.001, b0c=0.001, a0t=0.001, b0t=0.001,
                         delta_threshold=0.1,
                         theta=0.5, eta=1,
                         nsim = 100000, seed=2000,
                         ncore=14)

```


## Draw figure 3

```{r}

png("EB.example.png",width=600,height=600)


SRes = Res1


upc = unique(SRes$pc)
unc = unique(SRes$nc)
uq = unique(SRes$q)



SRes1 = SRes[SRes$pc==upc[1],]
SRes2 = SRes[SRes$pc==upc[2],]
SRes3 = SRes[SRes$pc==upc[3],]

layout(matrix(c(c(1,1,1),
                c(1,2,3,
                  4,5,6,
                  7,8,9,
                  10,10,10)+1),ncol=3,byrow=TRUE),heights=c(1.5,4,4,4,1.5))



par(oma = c(6,4,0,0) + 0.1,
    mar = c(0,1,1,1) + 0.1)

plot.new()
text(0.15,0.5,expression(p[c]==0.17),cex=1.8)
text(0.5,0.5,expression(p[c]==0.27),cex=1.8)
text(0.85,0.5,expression(p[c]==0.37),cex=1.8)



FigID = 1
plot(range(c(unc)),c(0.05,0.21), type="n",axes = FALSE,xlab="",ylab="")
grid(nx=NULL,ny=NULL,lty=1,col="gray",lwd=1)
for(j2 in 1:length(uq)){
  SResq = SRes1[SRes1$q==uq[j2],]
  
  points(SResq$nc,SResq$type1err,col=j2,pch = j2, lwd=3,cex=2)
  
}
axis(side = 1,labels = FALSE)
axis(side = 2, cex.axis = 1.5)
box(which = "plot", bty = "l")

FigID = 2
plot(range(c(unc)),c(0.05,0.21), type="n",axes = FALSE,xlab="",ylab="")
grid(nx=NULL,ny=NULL,lty=1,col="gray",lwd=1)
for(j2 in 1:length(uq)){
  SResq = SRes2[SRes2$q==uq[j2],]
  
  points(SResq$nc,SResq$type1err,col=j2,pch = j2, lwd=3,cex=2)
  
}
axis(side = 1,labels = FALSE)
axis(side = 2,labels = FALSE)
box(which = "plot", bty = "l")

FigID = 3
plot(range(c(unc)),c(0.05,0.21), type="n",axes = FALSE,xlab="",ylab="")
grid(nx=NULL,ny=NULL,lty=1,col="gray",lwd=1)
for(j2 in 1:length(uq)){
  SResq = SRes3[SRes3$q==uq[j2],]
  
  points(SResq$nc,SResq$type1err,col=j2,pch = j2, lwd=3,cex=2)
  
}
axis(side = 1,labels = FALSE)
axis(side = 2,labels = FALSE)
box(which = "plot", bty = "l")

################################################################################

FigID = 4
plot(range(c(unc)),c(0.5,1), type="n",axes = FALSE,xlab="",ylab="")
grid(nx=NULL,ny=NULL,lty=1,col="gray",lwd=1)
for(j2 in 1:length(uq)){
  SResq = SRes1[SRes1$q==uq[j2],]
  
  points(SResq$nc,SResq$power,col=j2,pch = j2, lwd=3,cex=2)
  
}
axis(side = 1,labels = FALSE)
axis(side = 2, cex.axis = 1.5)
box(which = "plot", bty = "l")

FigID = 5
plot(range(c(unc)),c(0.5,1), type="n",axes = FALSE,xlab="",ylab="")
grid(nx=NULL,ny=NULL,lty=1,col="gray",lwd=1)
for(j2 in 1:length(uq)){
  SResq = SRes2[SRes2$q==uq[j2],]
  
  points(SResq$nc,SResq$power,col=j2,pch = j2, lwd=3,cex=2)
  
}
axis(side = 1,labels = FALSE)
axis(side = 2,labels = FALSE)
box(which = "plot", bty = "l")

FigID = 6
plot(range(c(unc)),c(0.5,1), type="n",axes = FALSE,xlab="",ylab="")
grid(nx=NULL,ny=NULL,lty=1,col="gray",lwd=1)
for(j2 in 1:length(uq)){
  SResq = SRes3[SRes3$q==uq[j2],]
  
  points(SResq$nc,SResq$power,col=j2,pch = j2, lwd=3,cex=2)
  
}
axis(side = 1,labels = FALSE)
axis(side = 2,labels = FALSE)
box(which = "plot", bty = "l")

################################################################################

FigID = 7
plot(range(c(unc)),range(SRes$mean.PMD), type="n",axes = FALSE,xlab="",ylab="")
grid(nx=NULL,ny=NULL,lty=1,col="gray",lwd=1)
for(j2 in 1:length(uq)){
  SResq = SRes1[SRes1$q==uq[j2],]
  
  points(SResq$nc,SResq$mean.PMD,col=j2,pch = j2, lwd=3,cex=2)
  
}
axis(side = 1, cex.axis = 1.5)
axis(side = 2, cex.axis = 1.5)
box(which = "plot", bty = "l")

FigID = 8
plot(range(c(unc)),range(SRes$mean.PMD), type="n",axes = FALSE,xlab="",ylab="")
grid(nx=NULL,ny=NULL,lty=1,col="gray",lwd=1)
for(j2 in 1:length(uq)){
  SResq = SRes2[SRes2$q==uq[j2],]
  
  points(SResq$nc,SResq$mean.PMD,col=j2,pch = j2, lwd=3,cex=2)
  
}
axis(side = 1, cex.axis = 1.5)
axis(side = 2,labels = FALSE)
box(which = "plot", bty = "l")

FigID = 9
plot(range(c(unc)),range(SRes$mean.PMD), type="n",axes = FALSE,xlab="",ylab="")
grid(nx=NULL,ny=NULL,lty=1,col="gray",lwd=1)
for(j2 in 1:length(uq)){
  SResq = SRes3[SRes3$q==uq[j2],]
  
  points(SResq$nc,SResq$mean.PMD,col=j2,pch = j2, lwd=3,cex=2)
  
}
axis(side = 1, cex.axis = 1.5)
axis(side = 2,labels = FALSE)
box(which = "plot", bty = "l")


par(mai=c(0,0,0,0))
plot.new()

legend(x='bottom',ncol=3,col=1:3,pch=1:3,lwd=3,lty=0,legend = c("q=1","q=1.5","q=2"),fill=FALSE,cex=1.6,border ="white",bty = "n")




title(xlab =expression('n'[c]),
      ylab = "    Mean PMD                   Power                   Type I Error Rate ",
      outer = TRUE, line = 2,cex.lab=1.8)

dev.off()
```



```{r}
save.image("Figure3.RData")
```

```{r}
sessionInfo()
```

