'For loop across binomial probabilities in R
Hi I have a list of x values (x for the nCx formula for binomial probability) and a list of probabilities that I have to iterate over.
x=c(8,16,24,32)
p=c(0.01,0.05,0.1,0.15)
Basically I want to compute the combinations of these two vectors using the nCx formula and pbinom in R... I should essentially end up with a 4x4 matrix with probabilities. The 'q' parameter in my question is always 1.
Can someone let me know the simplest way to do this?
Solution 1:[1]
Perhaps
outer(x, p, FUN = function(S, P) pbinom(1, S, P))
# [,1] [,2] [,3] [,4]
# [1,] 0.9973099 0.9427553 0.8131047 0.65718303
# [2,] 0.9890671 0.8107597 0.5147278 0.28390121
# [3,] 0.9761456 0.6608173 0.2924770 0.10592423
# [4,] 0.9593174 0.5199624 0.1564234 0.03664672
Verification: the second x and the third p should show up on row 2 column 3,
pbinom(1, x[2], p[3])
# [1] 0.5147278
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | r2evans |
