################################################################################################################################# ### Figure 1: R Code to reproduce simulation results (tables and figures) ################################################################################################################################# library(ggplot2) library(xtable2) parameter = seq(from=0.01, to=0.10,by=0.01) result = data.frame(min = rep(0,length(parameter)), sigma1Star2 = 0, sigma2Star2 = 0, pStar = 0, N = 0, N12=0, kappa = 0) shape1 <- c(3,3,3) shape2 <- c(3,2,1) output = data.frame(min = 0, N = 0, N12 = 0, pStar = 0, kappa = 0) sim <- 10^6 set.seed(0) for(k in 1:length(shape1)) { for(kk in 1:length(parameter)){ cat(kk,"/",length(parameter),"\n") x1 = rbeta(sim, 5,5) x2 = rbeta(sim, shape1[k],shape2[k]) alpha=parameter[kk] beta=0.8 m1 <- length(x1) m2 <- length(x2) # ranks among union of samples: R <- rank(c(x1,x2), ties.method="average") R1 <- R[1:m1] R2 <- R[m1+(1:m2)] # ranks within samples: R11 <- rank(x1, ties.method="average") R22 <- rank(x2, ties.method="average") # placements: P1 <- R1 - R11 P2 <- R2 - R22 # effect size: pStar <- (mean(R2)-mean(R1)) / (m1+m2) + 0.5 # variances: sigmaStar <- sqrt(sum((R11-((m1+1)/2))^2) / m1^3) sigma1Star <- sqrt(sum((P1-mean(P1))^2) / (m1*m2^2)) sigma2Star <- sqrt(sum((P2-mean(P2))^2) / (m1^2*m2)) ss = function(t){ return((sigmaStar*qnorm(1-alpha/2) + qnorm(beta)*sqrt(t*sigma2Star^2 + (1-t)*sigma1Star^2))^2 / (t*(1-t)*(pStar-0.5)^2)) } kappa=sigma2Star/sigma1Star result[kk,1]=optimize(ss,interval=c(0.3,0.7), maximum=FALSE,tol = .Machine$double.eps)$minimum result[kk,2]=sigma1Star^2 result[kk,3]=sigma2Star^2 result[kk,4]=pStar result[kk,5]=optimize(ss,interval=c(0.3,0.7), maximum=FALSE,tol = .Machine$double.eps)$objective result[kk,6] = ss(1/2) result[kk,7] = kappa } output = rbind(output, result[,c("min","N","N12","pStar","kappa")]) } output <- output[-1,] output$parameter = parameter output$Effect = factor(gl(length(shape1),10), labels = c("small", "medium", "large")) library(ggplot2) p1 <- ggplot(data=output, aes(x=parameter, y= min, col = Effect,group=Effect)) + geom_line()+ geom_point() + xlab(expression(paste("Type-I error rate ", alpha))) + ylab(expression(paste("Optimal allocation rate t"))) + theme(legend.position = "top") + theme(axis.text=element_text(size=12), axis.title=element_text(size=12,face="bold")) + scale_colour_discrete(name = "Relative Effect") + labs(size = expression(paste("Ratio Variances"))) + theme(plot.margin = unit(c(2,2,2,2), "cm")) + theme(axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0))) + theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) + annotate("text", label = "p = 0.50", x = 0.05, y= 0.48, color = "red") + annotate("text", label = "p = 0.65", x = 0.05, y= 0.4725, color = "darkgreen") + annotate("text", label = "p = 0.84", x = 0.05, y= 0.4675, color = "blue") ggsave("Simulation_1.pdf", plot = p1) print(xtable(output, digits = 3), include.rownames = FALSE) ################################################################################################################################# ### Figure 2: R Code to reproduce simulation results ################################################################################################################################# parameter = seq(from=0.5, to=0.95,by=0.05) result = data.frame(min = rep(0,length(parameter)), sigma1Star2 = 0, sigma2Star2 = 0, pStar = 0, N = 0, N12=0, kappa = 0) shape1 <- c(3,3,3) shape2 <- c(3,2,1) output = data.frame(min = 0, N = 0, N12 = 0, pStar = 0, kappa = 0) sim <- 10^6 set.seed(0) for(k in 1:length(shape1)) { for(kk in 1:length(parameter)){ cat(kk,"/",length(parameter),"\n") x1 = rbeta(sim, 5,5) x2 = rbeta(sim, shape1[k],shape2[k]) alpha=0.05 beta=parameter[kk] m1 <- length(x1) m2 <- length(x2) # ranks among union of samples: R <- rank(c(x1,x2), ties.method="average") R1 <- R[1:m1] R2 <- R[m1+(1:m2)] # ranks within samples: R11 <- rank(x1, ties.method="average") R22 <- rank(x2, ties.method="average") # placements: P1 <- R1 - R11 P2 <- R2 - R22 # effect size: pStar <- (mean(R2)-mean(R1)) / (m1+m2) + 0.5 # variances: sigmaStar <- sqrt(sum((R11-((m1+1)/2))^2) / m1^3) sigma1Star <- sqrt(sum((P1-mean(P1))^2) / (m1*m2^2)) sigma2Star <- sqrt(sum((P2-mean(P2))^2) / (m1^2*m2)) ss = function(t){ return((sigmaStar*qnorm(1-alpha/2) + qnorm(beta)*sqrt(t*sigma2Star^2 + (1-t)*sigma1Star^2))^2 / (t*(1-t)*(pStar-0.5)^2)) } kappa=sigma2Star/sigma1Star result[kk,1]=optimize(ss,interval=c(0.3,0.7), maximum=FALSE,tol = .Machine$double.eps)$minimum result[kk,2]=sigma1Star^2 result[kk,3]=sigma2Star^2 result[kk,4]=pStar result[kk,5]=optimize(ss,interval=c(0.3,0.7), maximum=FALSE,tol = .Machine$double.eps)$objective result[kk,6] = ss(1/2) result[kk,7] = kappa } output = rbind(output, result[,c("min","N","N12","pStar","kappa")]) } output <- output[-1,] output$parameter = parameter output$Effect = factor(gl(length(shape1),10), labels = c("small", "medium", "large")) library(ggplot2) p2 <- ggplot(data=output, aes(x=parameter, y= min, col = Effect,group=Effect)) + geom_line()+ geom_point() + xlab(expression(paste("Power 1-", beta))) + ylab(expression(paste("Optimal allocation rate ", t))) + theme(legend.position = "top") + theme(axis.text=element_text(size=12), axis.title=element_text(size=12,face="bold")) + scale_colour_discrete(name = "Relative Effect") + labs(size = expression(paste("Ratio Variances"))) + theme(plot.margin = unit(c(2,2,2,2), "cm")) + theme(axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0))) + theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) + annotate("text", label = "p = 0.50", x = 0.75, y= 0.487, color = "red") + annotate("text", label = "p = 0.65", x = 0.9, y= 0.467, color = "darkgreen") + annotate("text", label = "p = 0.84", x = 0.75, y= 0.465, color = "blue") ggsave("Simulation_2.pdf", plot = p2) print(xtable(output, digits = 3), include.rownames = FALSE)