# [R] Add constraints to rbinom

Anamika Chaudhuri canamika at gmail.com
Fri Jun 28 04:05:40 CEST 2013

```Hi:

I am trying to generate Beta-Binomial random variables and then
calculate Binomial exact confidence intervals to the rate..I was
wondering if I needed to add a constraint such that x<=n to it, how do
I go about it. The reason is I am getting data where x>n which is
giving a rate>1. Heres my code:

set.seed(111)
k<-63
x<-NULL
p<-rbeta(k,3,3)# so that the mean nausea rate is alpha/(alpha+beta)
min<-10
max<-60
n<-as.integer(runif(k,min,max))
for(i in 1:k)
x<-cbind(x,rbinom(300,n,p[i]))
x<-t(x)
rate<-t(t(x)/n)
se_rate<-sqrt(rate*(1-rate)/n)

# Exact Confidence Interval

l_cl_exact<-qbeta(.025,x,n-x+1)
u_cl_exact<-qbeta(.975,x+1,n-x)

for (i in 1:63){

for (j in 1:300)
{

if (x[i,j]==0)
{
l_cl_exact[i,j]<-0
u_cl_exact[i,j]<-u_cl_exact[i,j]
}
else if (x[i,j]==n[i])
{
l_cl_exact[i,j]<-l_cl_exact[i,j]
u_cl_exact[i,j]<-1
}
else
l_cl_exact[i,j]<-l_cl_exact[i,j]
u_cl_exact[i,j]<-u_cl_exact[i,j]

#print(c(i,j))

}
}

Really appreciate any help.
Thanks
Anamika

```