I have the following algorithm
1- Generate U
2- Define x=0,P=(1-p)^n and F=P
3- If U < F,then X=x and stop.
4- Define P=(n-x)pP/(x+1)*(1-p), F=F+P and x=x+1
5- Come back to step 3.
According to my knowledge, on r would be
varBinom<-function(n,p)
{
U<-runif(n)
x<-0
P<-(1-p)^n
FF<-P
for(i in 1:n)
{
if(U<FF)
{
X<-x
break
}
P<-(n-x)*p*P/(x+1)*(1-p)
x<-x+1
}
return(x)
}
However when compiling the code I get 10 warning messages all of them say:
Warning messages: 1: In if (U < FF) { : the condition has length > 1 and only the first element will be used
Why does that happen?
I don't see why is wrong
How can I fix the code?
Aucun commentaire:
Enregistrer un commentaire