### R-code for constructing FGCI, HPD Bayesian, and Standard Bootstrap ### library("EnvStats") library("HDInterval") library("invgamma") library("fitdistrplus") library("MASS") library("survival") library("ggplot2") library("magick") CI.diffCVs <- function(M=15000,m=5000,k=3000,n1=25,n2=25,var1=0.5,var2=0.5,mean1=0,mean2=0, delta1=0.5,delta2=0.5,alpha=0.05){ starttime <- proc.time() cv1 <- sqrt(exp(var1)-1) #variance of the LN distribution in population 1 cv2 <- sqrt(exp(var2)-1) #variance of the LN distribution in population 2 ex1 <- delta1*exp(mean1+var1/2) #mean of the delta-LN distribution in population 1 ex2 <- delta2*exp(mean2+var2/2) #mean of the delta-LN distribution in population 2 var_x1 <- delta1*exp((2*mean1)+var1)*(exp(var1)-delta1) #variance of the delta-LN distribution in population 1 var_x2 <- delta2*exp((2*mean2)+var2)*(exp(var2)-delta2) #variance of the delta-LN distribution in population 2 eta1 <- sqrt(var_x1)/ex1 #cv of the delta-LN distribution in population 1 eta2 <- sqrt(var_x2)/ex2 #cv of the delta-LN distribution in population 2 gamma <- eta1-eta2 #difference between cvs of the delta-LN distribution #FGCI U1 <- matrix(rep(0,m)) U2 <- matrix(rep(0,m)) beta11 <- matrix(rep(0,m)) beta12 <- matrix(rep(0,m)) beta21 <- matrix(rep(0,m)) beta22 <- matrix(rep(0,m)) var1_GFQ <- matrix(rep(0,m)) var2_GFQ <- matrix(rep(0,m)) delta1_GFQ <- matrix(rep(0,m)) delta2_GFQ <- matrix(rep(0,m)) R1.GFQ <- matrix(rep(0,m)) R2.GFQ <- matrix(rep(0,m)) R.GFQ <- matrix(rep(0,m)) #Bayesian var1.linvj <- matrix(rep(0,m)) var2.linvj <- matrix(rep(0,m)) delta1.linvj <- matrix(rep(0,m)) delta2.linvj <- matrix(rep(0,m)) beta1.linvj <- matrix(rep(0,m)) beta2.linvj <- matrix(rep(0,m)) eta1.linvj <- matrix(rep(0,m)) eta2.linvj <- matrix(rep(0,m)) gamma.linvj <- matrix(rep(0,m)) var1.jrule <- matrix(rep(0,m)) var2.jrule <- matrix(rep(0,m)) delta1.jrule <- matrix(rep(0,m)) delta2.jrule <- matrix(rep(0,m)) beta1.jrule <- matrix(rep(0,m)) beta2.jrule <- matrix(rep(0,m)) eta1.jrule <- matrix(rep(0,m)) eta2.jrule <- matrix(rep(0,m)) gamma.jrule <- matrix(rep(0,m)) var1.uni <- matrix(rep(0,m)) var2.uni <- matrix(rep(0,m)) delta1.uni <- matrix(rep(0,m)) delta2.uni <- matrix(rep(0,m)) beta1.uni <- matrix(rep(0,m)) beta2.uni <- matrix(rep(0,m)) eta1.uni <- matrix(rep(0,m)) eta2.uni <- matrix(rep(0,m)) gamma.uni <- matrix(rep(0,m)) #FGCI L.rFGCI <- numeric() U.rFGCI <- numeric () CIr1 <- c(0,0) CP.rFGCI <- numeric() Length.rFGCI <- numeric() #HPD Bayesian using the left invariant Jeffreys prior hpd.linvj <- matrix(rep(0,2)) L.rBaye_hpd.linvj <- numeric() U.rBaye_hpd.linvj <- numeric () CIr2 <- c(0,0) CP.rBaye_hpd.linvj <- numeric() Length.rBaye_hpd.linvj <- numeric() #HPD Bayesian using the Jeffreys rule prior hpd.jrule <- matrix(rep(0,2)) L.rBaye_hpd.jrule <- numeric() U.rBaye_hpd.jrule <- numeric () CIr3 <- c(0,0) CP.rBaye_hpd.jrule <- numeric() Length.rBaye_hpd.jrule <- numeric() #HPD Bayesian using the uniform prior hpd.uni <- matrix(rep(0,2)) L.rBaye_hpd.uni <- numeric() U.rBaye_hpd.uni <- numeric () CIr4 <- c(0,0) CP.rBaye_hpd.uni <- numeric() Length.rBaye_hpd.uni <- numeric() #Standard Bootstrap L.rSB <- numeric() U.rSB <- numeric () CIr5 <- c(0,0) CP.rSB <- numeric() Length.rSB <- numeric() i = 0 while(i < M){ #Begin loop i x1 <- rzmlnormAlt(n1,exp(mean1+var1/2),cv1,1-delta1) x2 <- rzmlnormAlt(n2,exp(mean2+var2/2),cv2,1-delta2) nonzero_value1 <- log(x1[which(x1!=0)]) #Find values of non-zero in population 1 nonzero_value2 <- log(x2[which(x2!=0)]) #Find values of non-zero in population 2 size_nonzero1 <- length(nonzero_value1) #Find size of non-zero in population 1 if(size_nonzero1 < n1*delta1){ next } size_nonzero2 <- length(nonzero_value2) #Find size of non-zero in population 2 if(size_nonzero2 < n2*delta2){ next } mean_hat1 <- mean(nonzero_value1) var_hat1 <- var(nonzero_value1) delta_hat1 <- size_nonzero1/n1 eta_hat1 <- sqrt((exp(var_hat1)-delta_hat1)/delta_hat1) mean_hat2 <- mean(nonzero_value2) var_hat2 <- var(nonzero_value2) delta_hat2 <- size_nonzero2/n2 eta_hat2 <- sqrt((exp(var_hat2)-delta_hat2)/delta_hat2) #Fiducial generalized confidence interval (FGCI)........................................................... U1 <- rchisq(m,(size_nonzero1-1)) #Generate U1 from chi-square with n1(1)-1 df in population 1 U2 <- rchisq(m,(size_nonzero2-1)) #Generate U2 from chi-square with n2(1)-1 df in population 2 beta11 <- rbeta(m,size_nonzero1,(n1-size_nonzero1)+1) #Generate beta11 from beta distribution in population 1 beta12 <- rbeta(m,size_nonzero1+1,n1-size_nonzero1) #Generate beta12 from beta distribution in population 2 beta21 <- rbeta(m,size_nonzero2,(n2-size_nonzero2)+1) #Generate beta21 from beta distribution in population 1 beta22 <- rbeta(m,size_nonzero2+1,n2-size_nonzero2) #Generate beta22 from beta distribution in population 2 var1_GFQ <- ((size_nonzero1-1)*var_hat1)/U1 #Compute GFQ for delta-lognormal variance in population 1 var2_GFQ <- ((size_nonzero2-1)*var_hat2)/U2 #Compute GFQ for delta-lognormal variance in population 2 delta1_GFQ <- (1/2)*beta11+(1/2)*beta12 #Compute GFQ for probability of non-zero in population 1 delta2_GFQ <- (1/2)*beta21+(1/2)*beta22 #Compute GFQ for probability of non-zero in population 2 R1.GFQ <- sqrt((exp(var1_GFQ)-delta1_GFQ)/delta1_GFQ) #Compute GFQ for delta-lognormal coefficient of variation in population 1 R2.GFQ <- sqrt((exp(var2_GFQ)-delta2_GFQ)/delta2_GFQ) #Compute GFQ for delta-lognormal coefficient of variation in population 2 R.GFQ <- R1.GFQ-R2.GFQ #Compute GFQ for the difference cvs of delta-lognormal if(is.na(R.GFQ)){ next } L.rFGCI[i] <-quantile(R.GFQ,alpha/2) #Compute lower bounded based on FGCI U.rFGCI[i] <-quantile(R.GFQ,(1-alpha/2)) #Compute upper bounded based on FGCI CIr1 <- rbind(c(L.rFGCI[i],U.rFGCI[i])) CP.rFGCI[i] <- ifelse(L.rFGCI[i]< gamma && gamma