#------------------------------------------------------------------------------------------#
#
# Minimum sample size - Binomial
# Authors: Eliardo G. Costa and Julio M. Singer
# License: GPL 2
# Last update: 29/03/2015
#
# References:
# 1. Chen, X. (2007). Exact computation of minimum sample size for estimation 
#    of Poisson parameters. http://arxiv.org/abs/0707.2116.
# 2. Chen, X. (2011). Exact computation of minimum sample size for estimation
#    of binomial parameters. Journal of Statistical Planning and Inference 141, 2622-2632.
#
#------------------------------------------------------------------------------------------#

sample.sizeB=function(a=NULL,b=NULL,n0=2,ea=NULL,er=NULL,d)
{

#--------------------------------------------------------#
#   Probabilities absolute error - Theo. 2 (Chen, 2011)
#--------------------------------------------------------#

caA=function(a,n,e)
{
pbinom(ceiling(n*(a+e))-1,size=n,prob=a)-pbinom(floor(n*(a-e)),size=n,prob=a)
}

cbA=function(b,n,e)
{
pbinom(ceiling(n*(b+e))-1,size=n,prob=b)-pbinom(floor(n*(b-e)),size=n,prob=b)
}

cmaisA=function(l,n,e)
{
pbinom(l-1+ceiling(2*n*e),size=n,prob=l/n+e)-pbinom(l,size=n,prob=l/n+e)
}

cmenosA=function(l,n,e)
{
pbinom(l-1,size=n,prob=l/n-e)-pbinom(l-ceiling(2*n*e),size=n,prob=l/n-e)
}

#--------------------------------------------------------------#
#  Sets wich form 'S' absolute error - Theo. 2 (Chen, 2011)
#--------------------------------------------------------------#

con.maisA=function(a,b,n,e)
{
lim1=1+floor(n*(a-e))
lim2=ceiling(n*(b-e))-1

l=seq.int(from=lim1,by=1,length.out=lim2-lim1+1)
con=cmaisA(l,n,e)
con
}

con.menosA=function(a,b,n,e)
{
lim1=1+floor(n*(a+e))
lim2=ceiling(n*(b+e))-1

l=seq.int(from=lim1,by=1,length.out=lim2-lim1+1)
con=cmenosA(l,n,e)
con
}

#-------------------------------------------------------#
#  Probabilities relative error - Theo. 4 (Chen, 2011)
#-------------------------------------------------------#

caR=function(a,n,e)
{
pbinom(ceiling(n*a*(1+e))-1,size=n,prob=a)-pbinom(floor(n*a*(1-e)),size=n,prob=a)
}

cbR=function(b,n,e)
{
pbinom(ceiling(n*b*(1+e))-1,size=n,prob=b)-pbinom(floor(n*b*(1-e)),size=n,prob=b)
}

cmaisR=function(l,n,e)
{
pbinom(ceiling(l*(1+e)/(1-e))-1,size=n,prob=l/(n*(1-e)))-pbinom(l,size=n,prob=l/(n*(1-e)))
}

cmenosR=function(l,n,e)
{
pbinom(l-1,size=n,prob=l/(n*(1+e)))-pbinom(floor(l*(1-e)/(1+e)),size=n,prob=l/(n*(1+e)))
}

#-------------------------------------------------------------#
#  Sets which form 'S' relative error - Theo. 4 (Chen, 2011)
#-------------------------------------------------------------#

con.maisR=function(a,b,n,e)
{
lim1=1+floor(n*a*(1-e))
lim2=ceiling(n*b*(1-e))-1

l=seq.int(from=lim1,by=1,length.out=lim2-lim1+1)
con=cmaisR(l,n,e)
con
}

con.menosR=function(a,b,n,e)
{
lim1=1+floor(n*a*(1+e))
lim2=ceiling(n*b*(1+e))-1

l=seq.int(from=lim1,by=1,length.out=lim2-lim1+1)
con=cmenosR(l,n,e)
con
}

#----------------------------------------------#
#            Set 'S' absolute error
#----------------------------------------------#

SA=function(a,b,n,ea)
{
CAA1=caA(a,n,ea)
CBA1=cbA(b,n,ea)
CMAA1=con.maisA(a,b,n,ea)
CMEA1=con.menosA(a,b,n,ea)

s=c(CAA1,CBA1,CMAA1,CMEA1)
return(s)
}

#----------------------------------------------#
#            Set 'S' relative error
#----------------------------------------------#

SR=function(a,b,n,er)
{
CAR2=caR(a,n,er)
CBR2=cbR(b,n,er)
CMAR2=con.maisR(a,b,n,er)
CMER2=con.menosR(a,b,n,er)

s=c(CAR2,CBR2,CMAR2,CMER2)
return(s)
}

#----------------------------------------------#
#     Set 'S' absolute and relative errors
#----------------------------------------------#


SAR=function(a,b,n,ea,er)
{
CAA3=caA(a,n,ea)
CBA3=cbA(ea/er,n,ea)
CMAA3=con.maisA(a,ea/er,n,ea)
CMEA3=con.menosA(a,ea/er,n,ea)

CAR3=caR(ea/er,n,er)
CBR3=cbR(b,n,er)
CMAR3=con.maisR(ea/er,b,n,er)
CMER3=con.menosR(ea/er,b,n,er)

s=c(CAA3,CBA3,CMAA3,CMEA3,CAR3,CBR3,CMAR3,CMER3)
return(s)
}

#-------------------------#
#     Initial checks
#-------------------------#

if(is.null(ea) && is.null(er))
{
stop("At least one error must be specified")
}

if(length(d)<1)
{
stop("The length of vector 'd' must be at least one")
}

if(any(d<0) || any(d>1))
{
stop("The elements of vector 'd' must lie in interval (0,1)")
}

if(is.null(a) || is.null(b))
{
stop("The bounds 'a' and 'b' must be specified")
}

if(a>b)
{
stop("The bound 'a' must be smaller than bound 'b'")
}

#---------------------------------------------------------------------------#
#   Exact sample size - Absolute and relative errors - Theo. 5 (Chen ,2011)
#---------------------------------------------------------------------------#

if(!is.null(ea) && !is.null(er))
{

if(any(c(ea,er)<0) || any(c(ea,er)>1))
{
stop("The errors must lie in interval (0,1)")
}

if(ea/er<=a || ea/er>=b)
{
stop("The ratio 'ea/er' must lie in interval (a,b)")
}

cat("Calculating...\n")

d=sort(d,decreasing=T)

samples=numeric()

n=n0

for(i in 1:length(d))
{
s=SAR(a,b,n,ea,er)

while(min(s)<=1-d[i])
{
n=n+1
s=SAR(a,b,n,ea,er)
}
samples=append(samples,n)
}

}

#---------------------------------------------------------------------#
#   Exact sample size - Absolute error - Theos. 1 and 2 (Chen, 2011)
#---------------------------------------------------------------------#

if(!is.null(ea) && is.null(er))
{

if(ea<0 || ea>1)
{
stop("The absolute error must lie in interval (0,1)")
}

cat("Calculating...\n")

d=sort(d,decreasing=T)

n=n0

samples=numeric()

for(i in 1:length(d))
{
s=SA(a,b,n,ea)
while(min(s)<=1-d[i])
{
n=n+1
s=SA(a,b,n,ea)
}
samples=append(samples,n)
}

}

#----------------------------------------------------------------------#
#   Exact sample size - Relative error - Theos. 3 and 4 (Chen, 2011)
#----------------------------------------------------------------------#

if(is.null(ea) && !is.null(er))
{

if(er<0 || er>1)
{
stop("The relative error must lie in interval (0,1)")
}

cat("Calculating...\n")

d=sort(d,decreasing=T)

n=n0

samples=numeric()

for(i in 1:length(d))
{
s=SR(a,b,n,er)

while(min(s)<=1-d[i])
{
n=n+1
s=SR(a,b,n,er)
}
samples=append(samples,n)
}

}

#----------------------------#
#         Output
#----------------------------#

cat("\nCall to binomial dist.: \n")
cat("-----------------------\n")
cat(a,"< p <",b,"\n")
if(!is.null(ea) && !is.null(er))
{
cat("Absolute error = ",ea, "\n")
cat("Relative error = ",er, "\n")
}
if(!is.null(ea) && is.null(er))
{
cat("Absolute error = ",ea, "\n")
}
if(is.null(ea) && !is.null(er))
{
cat("Relative error = ",er, "\n")
}
cat("-----------------------\n")
cat("\nSample size: \n")
cat("------------- \n")
print(matrix(c(samples,1-d),length(samples),2,dimnames=list(rep("",length(samples)),c("n","1-d"))))
cat("-------------\n")

} # END FUNCTION


#-------------------------------------------------#
#                   EXAMPLES
#-------------------------------------------------#


# sample.sizeB(a=0,b=20,n0=2,ea=0.8,d=0.1)

# sample.sizeB(a=0,b=1,n0=2,ea=0.05,er=0.1,d=c(0.01,0.05))



