######################################################################################################################################
######################### Appendix 1 : Comparison of strictly conservative unequal tailed intervals ##################################
######################################################################################################################################

n=174
eiv=c("cic","ciblaker", "ciwang2014", "cisterne", "cischillingdoi","ciblythstill","ciexactscore","ciexactlr","ciasinbartlett","cicmidp")

compute_ivc=function(eiv,n) {
	ivc=lapply(eiv, function (iv) {
		m = t(sapply(0:n, function(x) {(get(iv))(x,n)}))
		m = as.data.frame(m)
		colnames(m)=c("lower","upper")
		p=(0:n)/n
		cbind(m, width = m[,2] - m[,1], p=p, upper.width=m[,2]-p, lower.width=p-m[,1])
	})
	names(ivc)=eiv
	ivc
}

ivc174=compute_ivc(eiv,n)
ivc32=compute_ivc(eiv,32)

ivc = ivc174
st=t(sapply(ivc, function (iv) {
	c(mean(iv$width), exp(mean(log(iv$width))))
}))
round(st,5)
write.table(round(st,5),"unequal-tails-avg-widths.csv",sep=";",dec=",",row.names=T,col.names=F)

labels_en=function() {
	cilabels<<-list(
	cic="Clopper-Pearson",
	cischillingdoi="Schilling-Doi",
	ciblaker="Blaker",
	cisterne="Sterne",
	ciwang2014="Wang",
	ciblythstill="Blyth-Still-Casella",
	ciexactscore="Exact score",
	ciexactlr="Exact LR",
	ciasinbartlett="Bartlett Arc-Sine",
	cicmidp="Clopper-Pearson mid-P"
	)
	sciv_ylab<<-"Relative change in length (%)"
	sciv_xlab<<-"x/n = Observed proportion of successes"
	sciv_rightlength <<- " right length"
	sciv_leftlength <<- " left length"
	sciv_length <<- " length"
}
labels_fr=function() {
	cilabels<<-list(
	cic="Clopper-Pearson",
	cischillingdoi="Schilling-Doi",
	ciblaker="Blaker",
	cisterne="Sterne",
	ciwang2014="Wang",
	ciblythstill="Blyth-Still-Casella",
	ciexactscore="Score exact",
	ciexactlr="RV exact",
	ciasinbartlett="Arc-Sinus de Bartlett",
	cicmidp="Clopper-Pearson mid-P"
	)
	sciv_ylab<<-"Changement relatif en largeur (%)"
	sciv_xlab<<-"x/n = Proportion observée de succès"
	sciv_rightlength <<- " demi-largeur à dte"
	sciv_leftlength <<- " demi-largeur à gche"
	sciv_length <<- " largeur"
}

if (is_french) {labels_fr()} else {labels_en()}

sciv=function(name1, name2, panel="",ylim=c(-8,14),ystep=2, varwidth="width", widthname1=sciv_length , widthname2=sciv_length, ivc=ivc174) {
	n=nrow(ivc[[1]])-1
	label1=cilabels[[name1]]
	label2=cilabels[[name2]]
	if (is.null(ivc[[name1]]) | is.null(ivc[[name2]])) {
		stop("Intervals not defined in ivc")
	}
	plot(x=(0:n)/n,100*((ivc[[name1]])[,varwidth] / (ivc[[name2]])[,varwidth] - 1),
		ylab=sciv_ylab,
		type="h",
		xlab=sciv_xlab,
		bty="l",xaxs="i",las=1,
		ylim=ylim,yaxs="i",xaxt="n",yaxt="n"
	);
	yy=seq(ylim[1],ylim[2],ystep)
	lbl=ifelse(yy>0, paste0("+",yy,"%"), paste0(yy, "%"))
	axis(2,at=yy, labels=lbl,las=1)
	abline(h=0);
	axis(1,at=seq(0,1,0.1));
	text(0.5,ylim[1]+2,paste0(label1,widthname1, " / ", label2, widthname2),adj=c(0.5,0),cex=1.2)
	text(0.05,ylim[2],panel,cex=2,adj=c(0,1))
}
#par(mfrow=c(1,1))
#sciv("ciblythstill", "cischillingdoi",panel="A")
#sciv("ciblythstill", "cischillingdoi",panel="A")

cic_vs_hwidth=function(name1, name2, panel, ylim=c(-20,20), ...) {
	sciv(name1, name2, panel=paste0(panel,1), ylim=ylim, ystep=4, varwidth="upper.width",widthname1="", widthname2=sciv_rightlength , ...)
	sciv(name1, name2, panel=paste0(panel,2), ylim=ylim, ystep=4, varwidth="lower.width",widthname1="", widthname2=sciv_leftlength , ...)
}

dirfile0("plotOther/figure13", width=10,height=10)
par(mfrow=c(3,2))
sciv("cisterne", "cischillingdoi",panel="A")
sciv("ciblaker", "cischillingdoi",panel="B")
sciv("ciwang2014", "cischillingdoi",panel="C")
sciv("ciexactscore","cischillingdoi",panel="E",ylim=c(-8,+14),ystep=4)
sciv("ciexactlr","cischillingdoi",panel="F")
sciv("ciblythstill","cischillingdoi",panel="G")
dev.off()

dirfile0("plotOther/figure14", width=9,height=11)
par(mfrow=c(3,2))
cic_vs_hwidth("cischillingdoi","cic",panel="A")
cic_vs_hwidth("ciwang2014","cic",panel="B")
cic_vs_hwidth("ciblaker","cic",panel="C")
dev.off()

dirfile0("plotOther/figure15", width=9,height=11)
par(mfrow=c(3,2))
cic_vs_hwidth("ciexactscore","cic",panel="A")
cic_vs_hwidth("ciexactlr","cic",panel="B")
cic_vs_hwidth("ciblythstill","cic",panel="C")
dev.off()

dirfile0("plotOther/figure16", width=9,height=11)
par(mfrow=c(3,2))
cic_vs_hwidth("ciblaker","cischillingdoi",panel="A")
cic_vs_hwidth("ciblythstill","ciblaker",panel="B")
cic_vs_hwidth("ciwang2014","ciblaker",panel="C")
dev.off()
round(cic(6,174),4)
round(cischillingdoi(6,174),4)
round(ciblaker(6,174),4)
round(ciblythstill(6,174),4)
