R/alloc.def4.R

Defines functions alloc.def4

Documented in alloc.def4

#’ Allocate sample size across regions by Definition 4 using simulation.
#‘
#‘ @description This function achieves optimal sample size allocation across regions
#' by maximizing conditional assurance probability by Definition 4 using simulation
#' when regional treatment effects are sightly different.
#'
#' @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 4
#' @param grid Grid interval of the grid research
#' @param n Simulation times
#'
#' @return A list
#' @export
#'
#' @examples
#' set.seed(123)
#' Alloc4 <- alloc.def4(r0=log(0.7), alpha=0.05, beta=0.2, lamda=1, lamda_cen=1, L=2,
#' s=3, u=c(1.1,1,0.9), eps=0.1, grid=0.1, n=1000)
#'
alloc.def4 <- function(r0, alpha=0.05, beta=0.2, lamda, lamda_cen, L, s, u, eps=0.1, grid=0.1, n=100000){
    if(sum(u==rep(1,s))==s) stop("Regional treatment effects should be slightly differenct.")
    t1 <- Sys.time()
    E0 = Eventnum(r0, alpha, beta)
    num = seq(grid,1-grid*(s-1),grid)
    y = t(combn(rep(num,s),s))
    y = unique(y)
    y = as.data.frame(y)
    y$sum = apply(y,1,sum)
    all_comb <- y[y$sum==1,1:s]
    colnames(all_comb) <- paste0("f",1:s)
    all_comb$con.AP <- rep(NA,nrow(all_comb))
    
    for(i in 1:nrow(all_comb)){
        f = as.numeric(all_comb[i,1:s])
        all_comb$con.AP[i] = prob.def4(r0=r0, alpha, s=s, E0=E0, u=u, f=f, eps, n, lamda, lamda_cen, L)$prob.cn
    }
    final_comb <- all_comb[order(-all_comb$con.AP),]
    final_comb$rank <- rank(-final_comb$con.AP)
    rownames(final_comb) <- NULL
    f <- as.numeric(final_comb[1,1:s])
    t2 <- Sys.time()
    Alloc_list <- list(optimal_alloc=f, alloc_cn.AP=final_comb, duration=t2-t1)
    return(Alloc_list)
}
carolinewei/apsurvival documentation built on Nov. 4, 2019, 8:44 a.m.