16/11/19 23:07:40.35 +gOnxegu.net
debugged
# Exact法(Clopper-Pearson)信頼区間のconf.levelを変更した信頼区間を返す
# RR:Risk Ratio, l0,u0: conf.level=cl0での信頼区間をcl1の区間に変更
ConvExactCI=function(RR,l0,u0,cl0=0.95,cl1=0.90){ # 95%→90%に
L0 = function(n,rr,cl) 1/(1+(n-n*rr+1)/(n*rr*qf((1-cl)/2, 2*n*rr,2*(n-n*rr+1))))
fl = function(x,u)L0(x,rr=RR,cl=cl0)-u
(nL=uniroot(fl,u=l0,c(1,10^4))$root)
(L1=L0(nL,RR,cl1))
U0 = function(n,rr,cl) 1/(1+(n-n*rr)/((n*rr+1)*qf(1-(1-cl)/2, 2*(n*rr+1), 2*(n-n*rr))))
fu = function(x,u)U0(x,rr=RR,cl=cl0)-u
(nU=uniroot(fu,u=u0,c(1,10^4))$root)
(U1=U0(nU,RR,cl1))
CI=c(L1,U1,cl1)
names(CI)=c("lower","upper","conf.level")
return(round(CI,4))
}