## Load all functions and libraries from file "CodeSimulationsBmc.r"


###############################################
# Figure 5 - Example: Power simulations Code

set.seed(123)

balloc = 1
mu = 1
sigma = 4
sigma.init = sigma
sigma.c = sigma
sigma.c.init = sigma.c
alpha = 0.05
beta = 0.2

nsim=10000

parms = expand.grid(b=c(6, 8, 16), round=c("exact", "multinom", "multinom2"),
                    c=c(23, 46, 92), approx=c("nv_uni", "nv_slope", "nv_ewb", "nv_max"))
nparm = length(parms$b)
oper = vector("list", nparm)

dfb_6 = block(b=6, balloc=balloc)
dfb_8 = block(b=8, balloc=balloc)
dfb_16 = block(b=16, balloc=balloc)


for (i in 1:nparm){
  if (parms$b[i] == 6){ df_b = dfb_6 }
  if (parms$b[i] == 8){ df_b = dfb_8 }
  if (parms$b[i] == 16){ df_b = dfb_16 }
  
  oper[[i]] <- power.fkt(N=0, c=parms$c[i], round=parms$round[i], b=parms$b[i], balloc=balloc, alpha=alpha, beta=beta, 
                         mu0=, mu=mu, sigma=sigma, sigma.init=sigma.init, sigma.c=sigma.c, sigma.c.init=sigma.c.init, 
                         nsim=nsim, approx=parms$approx[i], df_b=df_b)$erg
}

results = rbindlist(oper)


write.csv(results, file="DataFigure5.csv")
save.image("image_Figure5.r")



###############################################
# Figure 5 - Example: Power simulations Figure


# read data sets
df_all = read.csv("DataFigure5.csv", sep=";")
df_lower = subset(df_all, sm == "nv_uni")
df_equal = subset(df_all, sm == "nv_slope")
df_unequal = subset(df_all, sm == "nv_ewb")
df_upper = subset(df_all, sm == "nv_max")


postscript("Figure5.eps", width=16.0, height=12.0, paper="special", horizontal = FALSE)


layout(matrix(c(1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 13,
                1, 3, 3, 3, 7, 7, 7, 10, 10, 10, 13,
                1, 3, 3, 3, 7, 7, 7, 10, 10, 10, 13,
                1, 3, 3, 3, 7, 7, 7, 10, 10, 10, 13,
                1, 4, 4, 4, 8, 8, 8, 11, 11, 11, 13,
                1, 4, 4, 4, 8, 8, 8, 11, 11, 11, 13,
                1, 4, 4, 4, 8, 8, 8, 11, 11, 11, 13,
                1, 5, 5, 5, 9, 9, 9, 12, 12, 12, 13,
                1, 5, 5, 5, 9, 9, 9, 12, 12, 12, 13,
                1, 5, 5, 5, 9, 9, 9, 12, 12, 12 , 13,
                1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 13), 11, 11, byrow = TRUE), respect = TRUE)

#layout.show(13)


#  Labels left
op2=par(mar = c(0,0,0,0)+0.1, cex=2)
plot(x=0.5, y=0.5, ylim=c(0, 1), xlim=c(0, 1), axes=FALSE, col="white")
text(0.2,0.5,"Power", srt=90)

#  Labels top
op2=par(mar = c(0,0,0,0)+0.1, cex=2)
plot(x=0.5, y=0.5, ylim=c(0, 1), xlim=c(0, 1), axes=FALSE, col="white")
text(0.15,0.5,"Equal")
text(0.5,0.5,"Unequal 1")
text(0.85,0.5,"Unequal 2")


# first row, first column
op2=par(mar = c(1, 1, 1.5, 1.5)+0.1, cex=1)
erg = df_upper
    df = subset(erg, b==6 & round == "exact")
    plot(x=df$c, y=df$pd_NV.LS, type="b", col="darkgreen", ylab="", xlab="",
         ylim=c(0.5, 0.85), xlim=c(20, 100), axes=FALSE, lwd=2, pch=16, lty=1)
    axis(side=2, at=(-1:24)/20)
    axis(side=1, at=c(0, 23, 46, 92, 200))
erg = df_unequal
    df = subset(erg, b==6 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="darkgreen", lwd=2, pch=16, type="b", lty=2)
erg =  df_equal
    df = subset(erg, b==6 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="darkgreen", lwd=2, pch=16, type="b", lty=3)
erg  = df_lower
    df = subset(erg, b==6 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="darkgreen", lwd=2, pch=16, type="b", lty=4)
abline(h=0.8, lty=2, col="black", lwd=2)
legend("bottomleft", legend=c("b=6"), col="darkgreen", text.col="darkgreen", bty="n")


# second row, first column
op2=par(mar = c(1, 1, 1.5, 1.5)+0.1, cex=1)
erg = df_upper
    df = subset(erg, b==8 & round == "exact")
    plot(x=df$c, y=df$pd_NV.LS, type="b", col="red", ylab="", xlab="",
         ylim=c(0.5, 0.85), xlim=c(20, 100), axes=FALSE, lwd=2, pch=16, lty=1)
    axis(side=2, at=(-1:24)/20)
    axis(side=1, at=c(0, 23, 46, 92, 200))
erg = df_unequal
    df = subset(erg, b==8 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="red", lwd=2, pch=16, type="b", lty=2)
erg = df_equal
    df = subset(erg, b==8 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="red", lwd=2, pch=16, type="b", lty=3)
erg = df_lower
    df = subset(erg, b==8 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="red", lwd=2, pch=16, type="b", lty=4)
abline(h=0.8, lty=2, col="black", lwd=2)
legend("bottomleft", legend=c("b=8"), col="red", text.col="red", bty="n")

# third row, first column
 op2=par(mar = c(1, 1, 1.5, 1.5)+0.1, cex=1)
 erg = df_upper
    df = subset(erg, b==16 & round == "exact")
    plot(x=df$c, y=df$pd_NV.LS, type="b", col="black", ylab="", xlab="",
         ylim=c(0.5, 0.85), xlim=c(20, 100), axes=FALSE, lwd=2, pch=16, lty=1)
    axis(side=2, at=(-1:24)/20)
    axis(side=1, at=c(0, 23, 46, 92, 200))
erg = df_unequal
    df = subset(erg, b==16 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="black", lwd=2, pch=16, type="b", lty=2)
erg = df_equal
    df = subset(erg, b==16 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="black", lwd=2, pch=16, type="b", lty=3)
erg = df_lower
    df = subset(erg, b==16 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="black", lwd=2, pch=16, type="b", lty=4)
abline(h=0.8, lty=2, col="black", lwd=2)
legend("bottomleft", legend=c("b=16"), col="black", text.col="black", bty="n")

#  label bottom
op2=par(mar = c(0,0,0,0)+0.1, cex=2)
plot(x=0.5, y=0.5, ylim=c(0, 1), xlim=c(0, 1), axes=FALSE, col="white")
text(0.5,0.2,"Number of centres")


# first row, second column
op2=par(mar = c(1, 1, 1.5, 1.5)+0.1, cex=1)
erg = df_upper
    df = subset(erg, b==6 & round == "multinom")
    plot(x=df$c, y=df$pd_NV.LS, type="b", col="darkgreen", ylab="", xlab="",
         ylim=c(0.5, 0.85), xlim=c(20, 100), axes=FALSE, lwd=2, pch=16, lty=1)
    axis(side=2, at=(-1:24)/20)
    axis(side=1, at=c(0, 23, 46, 92, 200))
erg = df_unequal
    df = subset(erg, b==6 & round == "multinom")
    points(x=df$c, y=df$pd_NV.LS, col="darkgreen", lwd=2, pch=16, type="b", lty=2)
erg = df_equal
    df = subset(erg, b==6 & round == "multinom")
    points(x=df$c, y=df$pd_NV.LS, col="darkgreen", lwd=2, pch=16, type="b", lty=3)
erg = df_lower
    df = subset(erg, b==6 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="darkgreen", lwd=2, pch=16, type="b", lty=4)
abline(h=0.8, lty=2, col="black", lwd=2)

# second row, second column
op2=par(mar = c(1, 1, 1.5, 1.5)+0.1, cex=1)
erg = df_upper
df = subset(erg, b==8 & round == "multinom")
    plot(x=df$c, y=df$pd_NV.LS, type="b", col="red", ylab="", xlab="",
         ylim=c(0.5, 0.85), xlim=c(20, 100), axes=FALSE, lwd=2, pch=16, lty=1)
    axis(side=2, at=(-1:24)/20)
    axis(side=1, at=c(0, 23, 46, 92, 200))
erg = df_unequal
    df = subset(erg, b==8 & round == "multinom")
    points(x=df$c, y=df$pd_NV.LS, col="red", lwd=2, pch=16, type="b", lty=2)
erg = df_equal
    df = subset(erg, b==8 & round == "multinom")
    points(x=df$c, y=df$pd_NV.LS, col="red", lwd=2, pch=16, type="b", lty=3)
erg = df_lower
    df = subset(erg, b==8 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="red", lwd=2, pch=16, type="b", lty=4)
abline(h=0.8, lty=2, col="black", lwd=2)

 

# third row, second column
 op2=par(mar = c(1, 1, 1.5, 1.5)+0.1, cex=1)
 erg = df_upper
df = subset(erg, b==16 & round == "multinom")
    plot(x=df$c, y=df$pd_NV.LS, type="b", col="black", ylab="", xlab="",
         ylim=c(0.5, 0.85), xlim=c(20, 100), axes=FALSE, lwd=2, pch=16, lty=1)
    axis(side=2, at=(-1:24)/20)
    axis(side=1, at=c(0, 23, 46, 92, 200))
erg = df_unequal
    df = subset(erg, b==16 & round == "multinom")
    points(x=df$c, y=df$pd_NV.LS, col="black", lwd=2, pch=16, type="b", lty=2)
erg = df_equal
    df = subset(erg, b==16 & round == "multinom")
    points(x=df$c, y=df$pd_NV.LS, col="black", lwd=2, pch=16, type="b", lty=3)
erg = df_lower
    df = subset(erg, b==16 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="black", lwd=2, pch=16, type="b", lty=4)
abline(h=0.8, lty=2, col="black", lwd=2)


# first row, third column
op2=par(mar = c(1, 1, 1.5, 1.5)+0.1, cex=1)
erg = df_upper
df = subset(erg, b==6 & round == "multinom2")
    plot(x=df$c, y=df$pd_NV.LS, type="b", col="darkgreen", ylab="", xlab="",
         ylim=c(0.5, 0.85), xlim=c(20, 100), axes=FALSE, lwd=2, pch=16, lty=1)
    axis(side=2, at=(-1:24)/20)
    axis(side=1, at=c(0, 23, 46, 92, 200))
erg = df_unequal
    df = subset(erg, b==6 & round == "multinom2")
    points(x=df$c, y=df$pd_NV.LS, col="darkgreen", lwd=2, pch=16, type="b", lty=2)
erg = df_equal
    df = subset(erg, b==6 & round == "multinom2")
    points(x=df$c, y=df$pd_NV.LS, col="darkgreen", lwd=2, pch=16, type="b", lty=3)
erg = df_lower
    df = subset(erg, b==6 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="darkgreen", lwd=2, pch=16, type="b", lty=4)
abline(h=0.8, lty=2, col="black", lwd=2)
legend("bottomleft", legend=c(expression(N["upper"]), expression(N["MC,U"]), expression(N["MC,E"]), expression(N["lower"])), col="darkgreen", text.col="darkgreen", lty=1:4, bty="n")

# second row, third column
 op2=par(mar = c(1, 1, 1.5, 1.5)+0.1, cex=1)
 erg = df_upper
df = subset(erg, b==8 & round == "multinom2")
    plot(x=df$c, y=df$pd_NV.LS, type="b", col="red", ylab="", xlab="",
         ylim=c(0.5, 0.85), xlim=c(20, 100), axes=FALSE, lwd=2, pch=16, lty=1)
    axis(side=2, at=(-1:24)/20)
    axis(side=1, at=c(0, 23, 46, 92, 200))
erg = df_unequal
    df = subset(erg, b==8 & round == "multinom2")
    points(x=df$c, y=df$pd_NV.LS, col="red", lwd=2, pch=16, type="b", lty=2)
erg = df_equal
    df = subset(erg, b==8 & round == "multinom2")
    points(x=df$c, y=df$pd_NV.LS, col="red", lwd=2, pch=16, type="b", lty=3)
erg = df_lower
    df = subset(erg, b==8 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="red", lwd=2, pch=16, type="b", lty=4)
abline(h=0.8, lty=2, col="black", lwd=2)
legend("bottomleft", legend=c(expression(N["upper"]), expression(N["MC,U"]), expression(N["MC,E"]), expression(N["lower"])), col="red", text.col="red", lty=1:4, bty="n")
 

# third row, third column
op2=par(mar = c(1, 1, 1.5, 1.5)+0.1, cex=1)
erg = df_upper
    df = subset(erg, b==16 & round == "multinom2")
    plot(x=df$c, y=df$pd_NV.LS, type="b", col="black", ylab="", xlab="",
         ylim=c(0.5, 0.85), xlim=c(20, 100), axes=FALSE, lwd=2, pch=16, lty=1)
    axis(side=2, at=(-1:24)/20)
    axis(side=1, at=c(0, 23, 46, 92, 200))
erg = df_unequal
    df = subset(erg, b==16 & round == "multinom2")
    points(x=df$c, y=df$pd_NV.LS, col="black", lwd=2, pch=16, type="b", lty=2)
erg = df_equal
    df = subset(erg, b==16 & round == "multinom2")
    points(x=df$c, y=df$pd_NV.LS, col="black", lwd=2, pch=16, type="b", lty=3)
erg = df_lower
    df = subset(erg, b==16 & round == "exact")
    points(x=df$c, y=df$pd_NV.LS, col="black", lwd=2, pch=16, type="b", lty=4)
abline(h=0.8, lty=2, col="black", lwd=2)
legend("bottomleft", legend=c(expression(N["upper"]), expression(N["MC,U"]), expression(N["MC,E"]), expression(N["lower"])), col="black", text.col="black", lty=1:4, bty="n")


dev.off()



