Reputation: 11
I am executing this operation and when the process gives me NaN,
Choques<-c(1:10)
print (Choques)
pr<-0
n<-3818
p<-0.040633627
for(i in Choques) {
pr[i]<-(factorial(n)/(factorial(Choques[i])*factorial(n-Choques[i])))*p^Choques[i]*(1-p)^(n-Choques[i])
print (pr[i])
}
However changing the value of the variable n for a smaller number let's say 20, if it shows numbers, I would like to know if there is any method to change the NaN by numbers, I suppose the numbers are too big.
Upvotes: 1
Views: 122
Reputation: 52008
You are computing binomial probabilities in a loop and accumulating them in a vector. You can drop the factorials and even the loop by just using the vectorized function dbinom
. Your loop can be replaced by the single line:
pr <- dbinom(Choques,n,p)
Upvotes: 1
Reputation: 84719
Replace
factorial(n)/(factorial(Choques[i])*factorial(n-Choques[i]))
with
choose(n, Choques[i])
Upvotes: 2