title: Uniroot error when estimating coverage probability for an unspecified distribution
body: <p>I am trying to compute the coverage probability for an unspecified distribution. I have copied my <code>r</code> codes below and the corresponding error message. </p>
  
  <p>Firstly, I defined the CDF as below.</p>
  
  <pre><code>CD_theta &lt;- function(x, p, n) {
          1  -  pbinom (x, size = n, prob = p) + 1/2 * dbinom(x, size = n, prob = p)
  </code></pre>
  
  <p>Then used the following command to estimate the coverage probability.</p>
  
  <pre><code>N &lt;- 10000
  n &lt;- 10
  p &lt;- 0.5
  set.seed(1)
  
  x &lt;- rbinom(N, prob=p, size=n)
  
  covered &lt;- numeric()
  cilower &lt;- numeric() 
  ciupper &lt;- numeric() 
  for (i in 1:N) {
    CD_theta &lt;- function(x, p, n) {
      1  -  pbinom (x, size = n, prob = p) + 1/2 * dbinom(x, size = n, prob = p)
    }
     cilower &lt;- uniroot(function(p) CD_theta(x[i], p, n) - 0.025, c(0, 1))$root
     ciupper &lt;- uniroot(function(p) CD_theta(x[i], p, n) - 0.975, c(0, 1))$root
     covered[i] &lt;- (cilower &lt; p) &amp; (p &lt; ciupper) 
     }
  mean(covered) 
  </code></pre>
  
  <p>Error message and the output are below.</p>
  
  <p>Error in uniroot(function(p) CD_theta(x[i], p, n) - 0.975, c(0, 1)) : 
    f() values at end points not of opposite sign</p>
  
  <blockquote>
    <p>mean(covered)
    [1] 0.9820282</p>
  </blockquote>
  
  <p>Am I doing any mistake in coding? How can I prevent this?</p>