# R/sample.def3.R In carolinewei/apsurvival: Total sample size and its allocation for the MRCT

#### Documented in sample.def3

```#’ Calculate total sample size and its optimal allocation by Definition 3
#' using asymptotic distributions derived.
#‘
#‘ @description This function calculates total sample size with and without
#' considering desired conditional assurance probability to claim overall consistency and
#' determines optimal sample size allocation across regions by maximizing conditional
#' assurance probability based on Definition 3 if regional treatment effects
#' are slightly different. (Allocate equal sample size to each region
#' if treatment effects across regions are the same.)
#'
#' @param r0 True overall log hazard ratio
#' @param alpha The risk of rejecting the null hypothesis H0:r0>=0
#' when it is really true
#' @param beta The risk of failing to reject the null
#' hypothesis H0:r0>=0 when it is really false
#' @param lamda The event hazard rate for placebo
#' @param lamda_cen The discontinuation hazard rate
#' @param L The whole study duration of fixed study duration
#' design
#' @param s Number of regions participating in the MRCT
#' @param u A vector presents ratios of true regional log
#' hazard ratios to true overall log hazard ratio r=u*r0
#' @param eps Significance level of not rejecting H0 in Definition 3
#' @param pai The preserved proportion of overall treatment effect
#' @param grid Grid interval of the grid research
#' @param consistency A numeric value is the desired conditional assurance
#' probability to claim overall consistency showing only two decimal places.
#'
#' @return A list
#' @export
#'
#' @examples
#' set.seed(123)
#' Sampsize3 <- sample.def3(r0=log(0.7), alpha=0.05, beta=0.2, lamda=1, lamda_cen=1, L=2,
#' s=3, u=c(0.9,1,1.1), eps=0.3, pai=1/3, grid=0.1, consistency=0.8)
#'
sample.def3 <- function(r0, alpha=0.05, beta=0.2, lamda, lamda_cen, L, s, u, eps, pai, grid=0.1, consistency){
if(consistency!=round(consistency,2)) stop("The value only shows two decimal places.")
t1 <- Sys.time()
E0 = 4*(qnorm(alpha,0,1)+qnorm(beta,0,1))^2/(r0)^2
sampsize <- Samplesize(r0, alpha=alpha, beta=beta, E=ceiling(E0), lamda, lamda_cen, L)
if(sum(u==rep(1,s))==s){
f = rep(1/s,s)
prob1 <- prob.def3(r0=r0, s=s, E0=ceiling(E0), u=u, f=f, eps=eps, pai=pai, alpha=alpha)\$prob.cn
final <- as.data.frame(t(f))
colnames(final) <- paste0("f",1:s)
final\$con.AP <- prob1
}else{
final <- alloc.def3(r0=r0,alpha=alpha,beta=beta,s=s,u=u,eps=eps,pai=pai,grid=grid)\$alloc_cn.AP
f <- as.numeric(final[1,1:s])
prob1 <- final\$con.AP[1]
}

if(prob1 < (consistency-0.005)){
fold <- ceiling((consistency-prob1)/0.1)+1
prob3 <- prob.def3(r0=r0, s=s, E0=ceiling(fold*E0), u=u, f=f, eps=eps, pai=pai,alpha=alpha)\$prob.cn
while(prob3 < (consistency-0.005)){
fold = fold * (ceiling((consistency-prob3)/0.1)+1)
prob3 <- prob.def3(r0=r0, s=s, E0=ceiling(fold*E0), u=u, f=f, eps=eps, pai=pai,alpha=alpha)\$prob.cn
}
fold0=1
fold1 <- (fold0+fold)/2
prob2 <- prob.def3(r0=r0, s=s, E0=ceiling(fold1*E0), u=u, f=f, eps=eps, pai=pai,alpha=alpha)\$prob.cn
while(prob2<(consistency-0.005)|prob2>=(consistency+0.005)){
if(prob2>=(consistency+0.005)){
fold = fold1
prob3 = prob2
fold1 = (fold0+fold)/2
}
if(prob2<(consistency-0.005)){
fold0 = fold1
prob1 = prob2
fold1 = (fold0+fold)/2
}
prob2 <- prob.def3(r0=r0, s=s, E0=ceiling(fold1*E0), u=u, f=f, eps=eps, pai=pai,alpha=alpha)\$prob.cn
}
for(i in 1:ceiling(fold1*E0)){
prob <- prob.def3(r0=r0, s=s, E0=ceiling(fold1*E0)-i, u=u, f=f, eps=eps, pai=pai,alpha=alpha)\$prob.cn
if(prob < (consistency-0.005)) break
}
sampsize_AP <- Samplesize(r0, alpha=alpha, beta=beta, E=ceiling(fold1*E0)-i+1, lamda, lamda_cen, L)
}else{
sampsize_AP <- sampsize
}
t2 <- Sys.time()
full_list <- list(sampsize_AP, sampsize, f, final, t2-t1)
names(full_list) <- c("samplesize_AP","samplesize","optimal_alloc","alloc_cn.AP","duration")
return(full_list)
}
```
carolinewei/apsurvival documentation built on Nov. 4, 2019, 8:44 a.m.