#Author: Joost van Rosmalen #Date: September 22, 2016 #Description: Syntax for simulation study with different methods for #including historical trials in the context of survival analysis. rm(list=ls(all=TRUE)) #Clear workspace #Load packages library(foreign) library(survival) library(stats4) library(rjags) library(mcmc) library(MCMCpack) library(cubature) library(R2Cuba) library(R2jags) library(Hmisc) library(gtools) library(R.utils) library(plyr) library(dplyr) library(parallel) #Settings of simulation study. These also need to be specified in the file SimulationOneDataSet.r n_sims<- 500 #Number of simulated datasets per setting weib_shape<- 0.6 #Shape parameter cens_start<- 36 cens_end <- 84 #All patients are censored between 36 and 84 months, using a uniform distribution n_patients<- 150 #Number of patients in simulated data set n_hist_trials <- 3 #Number of historical trials niter<- 150000 #Number of MCMC iterations per data set baseline_hazard <- -2.5 #Log baseline hazard time_trend<- log(0.95) #Time trend per trial, on log-hazard scale #Set working directories directory_code<-"/home/jvanrosmalen/Joost/AnalysesRevisie/" setwd(directory_code) #Save output to text file. sink(paste("results.txt")) #Initialize counter of computation time tt<-proc.time() #Create the arrays with MCMC output result_overall<-array(0,c(niter/5,ncol=10,n_sims)) result_mean<-matrix(0,6*ncol(result_overall),n_sims) result_RMSE<-matrix(0,6*ncol(result_overall),n_sims) result_SE<-matrix(0,6*ncol(result_overall),n_sims) result_reject_null<-matrix(0,6*ncol(result_overall),n_sims) #Now start the main analyses for(setting in 1:2){ if(setting==1){ logHR_intervention <- log(0.7) #Treatment effect } if(setting==2){ logHR_intervention <- 0 #No treatment effect, to assess type I error } for (sc in c(1:6)){ #Sc denotes the scenario of the simulation study #Values: #1: no time trend #2: time trend in data, without time included in analysis model #3: time trend in data, with time included in analysis model #4: no heterogeneity #5: high between-trial heterogeneity #6: very high between-trial heterogeneity #Create the array with MCMC output result_overall<-array(0,c(niter/5,ncol=10,n_sims)) source("SimulationOneDataSet.r",local=TRUE) cl <- makeCluster(mc <- getOption("cl.cores", 75)) simVector<-1:n_sims temp<-clusterApplyLB(cl, simVector, SimulationOneDataSet,sc=sc,logHR_intervention=logHR_intervention) stopCluster(cl) for(ii in 1:length(simVector)){ result_overall[,,simVector[ii]]<-temp[[ii]][,] } rm(temp) colnames(result_overall)<-c("Current data","Pooled data","Pocock's method","Power prior with $alpha=0.5$","MPP","MAP approach","Method of Murray et al.","Test-then-pool method","Robust MAP approach version 1","Robust MAP approach version 2") for (i in 1:ncol(result_overall)){ for(j in 1:n_sims){ result_mean[i+ncol(result_overall)*(sc-1),j]<-mean(result_overall[,i,j])- logHR_intervention result_RMSE[i+ncol(result_overall)*(sc-1),j]<-sqrt(mean((result_overall[,i,j]- logHR_intervention)^2)) result_SE[i+ncol(result_overall)*(sc-1),j]<-sqrt(var(result_overall[,i,j])) exceedance_prob<- mean(result_overall[,i,j]>0) #This is the one-sided Bayesian p-value of the intervention effect if(exceedance_prob>=0.025 & exceedance_prob<=0.975){ result_reject_null[i+ncol(result_overall)*(sc-1),j]<- 0 } else{ result_reject_null[i+ncol(result_overall)*(sc-1),j]<- 1 } } } rownames(result_mean)<-rep(colnames(result_overall),6) rownames(result_RMSE)<-rep(colnames(result_overall),6) rownames(result_SE)<-rep(colnames(result_overall),6) rownames(result_reject_null)<-rep(colnames(result_overall),6) print(result_mean) print(result_RMSE) print(result_SE) print(result_reject_null) result_mean_tot<-round(matrix(rowSums(result_mean)/ncol(result_mean),nrow=10,ncol=6),3) rownames(result_mean_tot)<-rownames(result_mean)[1:10] colnames(result_mean_tot)<-c("No time trend","Time trend, no adjustment","Time trend, with adjustment","No heterogeneity","High heterogeneity","Very high heterogeneity") result_RMSE_tot<-round(matrix(rowSums(result_RMSE)/ncol(result_RMSE),nrow=10,ncol=6),3) rownames(result_RMSE_tot)<-rownames(result_RMSE)[1:10] colnames(result_RMSE_tot)<-c("No time trend","Time trend, no adjustment","Time trend, with adjustment","No heterogeneity","High heterogeneity","Very high heterogeneity") result_SE_tot<-round(matrix(rowSums(result_SE)/ncol(result_SE),nrow=10,ncol=6),3) rownames(result_SE_tot)<-rownames(result_SE)[1:10] colnames(result_SE_tot)<-c("No time trend","Time trend, no adjustment","Time trend, with adjustment","No heterogeneity","High heterogeneity","Very high heterogeneity") result_reject_null_tot<-round(matrix(rowSums(result_reject_null)/ncol(result_reject_null),nrow=10,ncol=6),6) rownames(result_reject_null_tot)<-rownames(result_reject_null)[1:10] colnames(result_reject_null_tot)<-c("No time trend","Time trend, no adjustment","Time trend, with adjustment","No heterogeneity","High heterogeneity","Very high heterogeneity") print(result_mean_tot) print(result_RMSE_tot) print(result_SE_tot) print(result_reject_null_tot) cat(captureOutput(latex.default(result_mean_tot,file="",label="result_sim_mean", caption="Average bias of estimated intervention effect in simulation study")),sep="\n") cat(captureOutput(latex.default(result_RMSE_tot,file="",label="result_sim_RMSE", caption="Average RMSD of estimated intervention effect in simulation study")),sep="\n") cat(captureOutput(latex.default(result_SE_tot,file="",label="result_sim_SE", caption="Average posterior standard deviation of estimated intervention effect in simulation study")),sep="\n") cat(captureOutput(latex.default(result_reject_null_tot,file="",label="result_sim_reject_null", caption="Power of estimated intervention effect in simulation study")),sep="\n") result_mean_CIs<-data.frame(No_time_trend=rep("c",10),Time_trend_no_adjustment=rep("g",10),Time_trend_with_adjustment=rep("g",10), No_heterogeneity=rep("g",10),High_heterogeneity=rep("g",10),Very_high_heterogeneity=rep("g",10), stringsAsFactors=FALSE) rownames(result_mean_CIs)<-rownames(result_mean)[1:10] for(ii in 1:10){ for(jj in 1:6){ result_mean_CIs[ii,jj]<-paste(toString(formatC(mean(result_mean[10*(jj-1)+ii,]),format="f",digits=3))," (", toString(formatC(mean(result_mean[10*(jj-1)+ii,]) -qt(0.975,df=length(result_mean[10*(jj-1)+ii,])-1)*sd(result_mean[10*(jj-1)+ii,])/sqrt(length(result_mean[10*(jj-1)+ii,])),format="f",digits=3)) ," - ",toString(formatC(mean(result_mean[10*(jj-1)+ii,]) +qt(0.975,df=length(result_mean[10*(jj-1)+ii,])-1)*sd(result_mean[10*(jj-1)+ii,])/sqrt(length(result_mean[10*(jj-1)+ii,])),format="f",digits=3)) ,")",sep="") } } result_mean_CIs cat(captureOutput(latex.default(result_mean_CIs,file="",label="result_sim_mean", caption="Average bias of estimated intervention effect in simulation study")),sep="\n") result_RMSE_CIs<-data.frame(No_time_trend=rep("c",10),Time_trend_no_adjustment=rep("g",10),Time_trend_with_adjustment=rep("g",10), No_heterogeneity=rep("g",10),High_heterogeneity=rep("g",10),Very_high_heterogeneity=rep("g",10), stringsAsFactors=FALSE) rownames(result_RMSE_CIs)<-rownames(result_RMSE)[1:10] for(ii in 1:10){ for(jj in 1:6){ result_RMSE_CIs[ii,jj]<-paste(toString(formatC(mean(result_RMSE[10*(jj-1)+ii,]),format="f",digits=3))," (", toString(formatC(mean(result_RMSE[10*(jj-1)+ii,]) -qt(0.975,df=length(result_RMSE[10*(jj-1)+ii,])-1)*sd(result_RMSE[10*(jj-1)+ii,])/sqrt(length(result_RMSE[10*(jj-1)+ii,])),format="f",digits=3)) ," - ",toString(formatC(mean(result_RMSE[10*(jj-1)+ii,]) +qt(0.975,df=length(result_RMSE[10*(jj-1)+ii,])-1)*sd(result_RMSE[10*(jj-1)+ii,])/sqrt(length(result_RMSE[10*(jj-1)+ii,])),format="f",digits=3)) ,")",sep="") } } result_RMSE_CIs cat(captureOutput(latex.default(result_RMSE_CIs,file="",label="result_sim_RMSE", caption="Average RMSD of estimated intervention effect in simulation study")),sep="\n") result_SE_CIs<-data.frame(No_time_trend=rep("c",10),Time_trend_no_adjustment=rep("g",10),Time_trend_with_adjustment=rep("g",10), No_heterogeneity=rep("g",10),High_heterogeneity=rep("g",10),Very_high_heterogeneity=rep("g",10), stringsAsFactors=FALSE) rownames(result_SE_CIs)<-rownames(result_SE)[1:10] for(ii in 1:10){ for(jj in 1:6){ result_SE_CIs[ii,jj]<-paste(toString(formatC(mean(result_SE[10*(jj-1)+ii,]),format="f",digits=3))," (", toString(formatC(mean(result_SE[10*(jj-1)+ii,]) -qt(0.975,df=length(result_SE[10*(jj-1)+ii,])-1)*sd(result_SE[10*(jj-1)+ii,])/sqrt(length(result_SE[10*(jj-1)+ii,])),format="f",digits=3)) ," - ",toString(formatC(mean(result_SE[10*(jj-1)+ii,]) +qt(0.975,df=length(result_SE[10*(jj-1)+ii,])-1)*sd(result_SE[10*(jj-1)+ii,])/sqrt(length(result_SE[10*(jj-1)+ii,])),format="f",digits=3)) ,")",sep="") } } result_SE_CIs cat(captureOutput(latex.default(result_SE_CIs,file="",label="result_sim_SE", caption="Average posterior standard deviation of estimated intervention effect in simulation study")),sep="\n") result_power_CIs<-data.frame(No_time_trend=rep("c",10),Time_trend_no_adjustment=rep("g",10),Time_trend_with_adjustment=rep("g",10), No_heterogeneity=rep("g",10),High_heterogeneity=rep("g",10),Very_high_heterogeneity=rep("g",10), stringsAsFactors=FALSE) rownames(result_power_CIs)<-rownames(result_reject_null)[1:10] for(ii in 1:10){ for(jj in 1:6){ result_power_CIs[ii,jj]<-paste(toString(formatC(mean(result_reject_null[10*(jj-1)+ii,]),format="f",digits=3)),sep="") } } result_power_CIs cat(captureOutput(latex.default(result_power_CIs,file="",label="result_sim_power", caption="Power of estimated intervention effect in simulation study")),sep="\n") result_sample_size_reduction<-data.frame(No_time_trend=rep("c",10),Time_trend_no_adjustment=rep("g",10),Time_trend_with_adjustment=rep("g",10), No_heterogeneity=rep("g",10),High_heterogeneity=rep("g",10),Very_high_heterogeneity=rep("g",10), stringsAsFactors=FALSE) rownames(result_sample_size_reduction)<-rownames(result_reject_null)[1:10] for(ii in 1:10){ for(jj in 1:6){ result_sample_size_reduction[ii,jj]<-paste(toString(formatC( 100-100*(qnorm(0.025)+qnorm(1-mean(result_reject_null[10*(jj-1)+1,])))^2/ (qnorm(0.025)+qnorm(1-mean(result_reject_null[10*(jj-1)+ii,])))^2 ,format="f",digits=1)),sep="") } } result_sample_size_reduction cat(captureOutput(latex.default(result_sample_size_reduction,file="",label="result_sim_sample_size_reduction", caption="Estimated sample size reduction due to the use of historical data, based on power in simulation study")),sep="\n") if(setting==1){ save.image(file=paste(directory_code,"output_simstudy_treatment_effect", niter,".Rdata",sep="")) } if(setting==2){ save.image(file=paste(directory_code,"output_simstudy_no_treatment_effect", niter,".Rdata",sep="")) } } } print(proc.time()-tt) sink()