###Use this file to generate results ################################################################################ #Set up parameters for the trials ################################################################################ #source the functions.R file source() #Fixed parameters for the trials alpha <- 0.025 lambda <- 0.5 tau <- 0.5 info <- ((qnorm(1-0.025)+qnorm(0.9))/10)^2 delta <- c(0,0) #set sample size to be used in simulations #n is when simulating trials n <- 10^6 #m is when computing decision boundary m <- 10^6 #parameters for displayed decision boundaries #number of iterations you want maxit0 <- 9 #number of iterations for decision rule computing Bayes optimal AE expected #performance maxit <- 9 #Prior distribution to be investigated mu <- c(12,4) sigma <- c(5,5) rho <- 0.75 print(noquote(c("mu =",mu," sigma =",sigma," rho =",rho))) ################################################################################ ##computing the gain of the fixed sample designs ################################################################################ #draw from the prior, note if you wish to find the gain at given #theta, change each to rep(theta,n) theta1 <- rnorm(n,mu[1],sigma[1]) theta2 <- rnorm(n,mu[2]+(sigma[2]/sigma[1])*rho*(theta1-mu[1]), sqrt(1-rho^2)*sigma[2]) #compute the fixed sample gains as given by the parameters outFS=FEgain(alpha,lambda,info,n,delta,theta1) outFF=MHgain(alpha,lambda,info,n,delta,theta1,theta2) print(c("outFS=",outFS)) print(c("outFF=",outFF)) ################################################################################ ##plotting a Bayes optimal decision ################################################################################ # note thetahat1 and thetahat2 determine the window this shall be computed over, # adjust these as necessary thetahat1 <- c(qnorm(0.05,mu[1], sigma[1]+sqrt(1/(lambda*tau*info))), qnorm(0.95,mu[1], sigma[1]+sqrt(1/(lambda*tau*info)))) thetahat2 <- c(qnorm(0.05,mu[2], sigma[2]+sqrt(1/((1-lambda)*tau*info))), qnorm(0.95,mu[2], sigma[2]+sqrt(1/((1-lambda)*tau*info)))) #repackage the prior and related information prior.v=array(0,c(2,2)) prior.v[1,1]=sigma[1]^2 prior.v[2,2]=sigma[2]^2 prior.v[1,2]=rho*sigma[1]*sigma[2] prior.v[2,1]=prior.v[1,2] var.that=array(0,c(2,2)) var.that[1,1]=1/(lambda*tau*info) var.that[2,2]=1/((1-lambda)*tau*info) prior.v.inv=solve(prior.v) var.that.inv=solve(var.that) postr.v=solve(prior.v.inv+var.that.inv) a=postr.v%*%prior.v.inv%*%mu b=postr.v%*%var.that.inv c=postr.v #parameters for the optimisation dparm <- list(mu,sigma,rho,a,b,c) #create the decision grid object, called pictab #set pctd to a positive value to show region within which decisions are within #pctd% pctd <- 0.05 pictab <- cgrid(thetahat1,thetahat2,maxit,pctd,alpha, lambda,tau,info,m,delta,vbayesopt,dparm) #plot the decision object plotdecis(pictab) leg1 <- c(-10:10)*5 leg2 <- c(-10:10)*5 #add points for legend of decision leg <- expand.grid(leg1,leg2) leg1 <- leg[,1] leg2 <- leg[,2] addleg(leg1,leg2,alpha,lambda,tau,info,delta,m,pictab) ################################################################################ ##computing expected performance of the Bayes optimal AE trial ################################################################################ # window to create look up table for actual trial create the intial grid by # choosing the extreme values, note that the decisions must differ at these # points for the algorithm to work thetahat1 <- c(qnorm(0.0001,mu[1], sigma[1]+sqrt(1/(lambda*tau*info))), qnorm(0.9999,mu[1], sigma[1]+sqrt(1/(lambda*tau*info)))) thetahat2 <- c(qnorm(0.0001,mu[2], sigma[2]+sqrt(1/((1-lambda)*tau*info))), qnorm(0.9999,mu[2], sigma[2]+sqrt(1/((1-lambda)*tau*info)))) #no pctd required here pctd <- 0 #create the decision object decistab <- cgrid(thetahat1,thetahat2,maxit,pctd,alpha, lambda,tau,info,m,delta,vbayesopt,dparm) ##note you may wish to plot this to confirm it has worked as intended #simulate theta from the prior distribution theta1 <- rnorm(n,mu[1],sigma[1]) theta2 <- rnorm(n,mu[2]+(sigma[2]/sigma[1])*rho*(theta1-mu[1]), sqrt(1-rho^2)*sigma[2]) #find the expected gain of the Bayes optimal decision outAE=simAE(alpha,lambda,tau,info,n,delta,theta1,theta2,vdgrid,decistab) print(c("outAE=",outAE)) ################################################################################ ##Ouput results in tidy format ################################################################################ prej1=outAE[1] # Reject H01 only prej3=outAE[2] # Reject H03 only prejb=outAE[3] # Reject both H01 and H03 prej3b=prej3+prejb # Reject H03 (with or without H01) probe=outAE[4] # P(Enrich) egain=outAE[5] # E(gain) printout=array(" ",c(4,10)) printout[1,]=c("mu1 ","mu2 ","s1 ","s2 ","rho ", "Des ","P(R1)","P(R3)","P(En)", "E(G) ") printout[2,1]=as.character(mu[1]) printout[2,2]=as.character(mu[2]) printout[2,3]=as.character(sigma[1]) printout[2,4]=as.character(sigma[2]) printout[2,5]=as.character(rho) printout[2,6]="FS" printout[2,7]=as.character(round(outFS[[1]]*100)/100) printout[2,8]="-" printout[2,9]="-" printout[2,10]=as.character(round(outFS[[2]]*100)/100) printout[3,6]="FF" printout[3,7]=as.character(round(outFF[[1]][1]*100)/100) printout[3,8]=as.character(round((outFF[[1]][2]+outFF[[1]][3])*100)/100) printout[3,9]="-" printout[3,10]=as.character(round(outFF[[2]]*100)/100) printout[4,6]="AE" printout[4,7]=as.character(round(prej1*100)/100) printout[4,8]=as.character(round(prej3b*100)/100) printout[4,9]=as.character(round(probe*100)/100) printout[4,10]=as.character(round(egain*100)/100) print(printout,quote=FALSE)