2

I think this is a rather common problem, but I could not find a solution.

I want to solve the following equation: pbinom(18,25,p)=0.05.

Is there a way to find the unknown p with the program R?

Every help is appreciated.

garondal
  • 123
  • 4
  • Hi Garondal, welcome to Stackoverflow! With the the way that you ask your question now, I am not exactly sure what it is that you want. However, the following website shows how to work with `pbinom` in R so might be useful to you: http://www.r-tutor.com/elementary-statistics/probability-distributions/binomial-distribution – Emil Jun 17 '20 at 16:05

2 Answers2

2

Root finding:

print(
  res <- uniroot(function(p) pbinom(18,25,p) - 0.05, c(0, 1), 
          tol = .Machine$double.eps)
)

pbinom(18,25,res$root)
#[1] 0.05
Roland
  • 117,893
  • 9
  • 163
  • 255
0

Brute force :

p = 0.0001 # starting point
while (abs(pbinom(18,25,p) -  0.05) > 0.001) p <- p + 0.001

This code evaluates the pdf for different values of p until you are "close enough" to 0.05. Here "close enough" means at the 0.001 range.

> p
[1] 0.8601
> pbinom(18,25,0.8601)
[1] 0.05070763
Trusky
  • 385
  • 1
  • 12
  • Why seek an approximate solution when @Roland's exact solution is simple and (probably) quicker? – Limey Jun 17 '20 at 13:38
  • 1
    @Limey My solution is approximate (as all numeric solutions must be). It is basically the same approach as Trusky's but using a better algorithm. – Roland Jun 17 '20 at 13:43
  • Indeed, the main difference is the tolerance set at `.Machine$double.eps` and the high quality algorithm behind `uniroot`. Roland's answers is perfect, for sure. – Trusky Jun 17 '20 at 13:45
  • Actually, the answer is nice if somebody wants to see how the while-loop works. – garondal Jun 17 '20 at 13:45