#Tailrank depends on biobase, which is within bioconductor. You may need to run: #if (!requireNamespace("BiocManager", quietly = TRUE)) #install.packages("BiocManager") #BiocManager::install("Biobase") #install.packages("TailRank",dependencies=T) library(TailRank) #simulatetrials generates niterations replicates of simulated trials. Currently written to only allow three stages/ #Used in conjunction with findtrialproperties #Arguments: p0 - null response rate, p1 - alt response rate, a0,b0 - parameters for beta prior #neachstage - number of patients recruited per stage (must be vector with three integers, e.g. c(6,3,3) for monotherapy or c(10,5,5) for combination) #alpha - type I error rate used for final comparison in the futility stopping calculation #niterations - number of simulation replicates #printprogress - set to 1 if the progress in number of replicates simulated should be printed out, 0 for silent simulatetrials=function(p0=0.3, p1=0.6, a0=0.3, b0=0.7, neachstage, alpha=0.1, niterations=25000, printprogress=0) { totaln=sum(neachstage) #define vectors/matrices to store results for each iteration posteriormeanmorethanp0.null=matrix(0,niterations,3) predictiveprobabilityoftrialsuccess.null=matrix(0,niterations,2) finalteststatistic.null=rep(0,niterations) finalteststatistic.alt=rep(0,niterations) posteriormeanmorethanp0.alt=matrix(0,niterations,3) predictiveprobabilityoftrialsuccess.alt=matrix(0,niterations,2) #first simulate null situation with response rate equal to p0: for(iteration in 1:niterations) { if(iteration%%1000==0) { if(printprogress==1) { print(iteration) } } responses=0 for(stage in 1:length(neachstage)) { responses=responses+rbinom(1,neachstage[stage],p0) n=cumsum(neachstage)[stage] #Update prior beta parameters to posterior beta parameters using conjugate distribution an=a0+responses bn=b0+n-responses #efficacy check - if posterior mean that proportion is more than null value posteriormeanmorethanp0.null[iteration,stage]=1-pbeta(p0,an,bn) #futility assessment: if predictive probability of trial success is less than futility threshold, stop trial early #to declare significance at alpha level, need there to be a total of qbinom(1-alpha,totaln,p0) responses. #Probability of this is beta-binomial (totaln-currentn,an,bn). Use pbb in tailrank package responsesneededforsignificance=qbinom(1-alpha,totaln,p0) if(stageefficacythreshold,1,0)})) efficacystopping.alt=t(apply(posteriormeanmorethanp0.alt[,-3],1,function(x){ifelse(x>efficacythreshold,1,0)})) whichstagestopforefficacy.null=apply(efficacystopping.null,1,function(x){return(min(which(x==1)))}) whichstagestopforefficacy.alt=apply(efficacystopping.alt,1,function(x){return(min(which(x==1)))}) whichstagestopforfutility.null=apply(futilitystopping.null,1,function(x){return(min(which(x==1)))}) whichstagestopforfutility.alt=apply(futilitystopping.alt,1,function(x){return(min(which(x==1)))}) #null hypothesis could be rejected if: #-efficacy stopping is equal to or smaller than futility stopping #no futility stopping and final teststatistic is significant typeIerror=ifelse((whichstagestopforefficacy.null