 
Confidence intervals of p  using R
Binomial confidence interval functions
 Simple (largesample) normal approximation Wald interval
 Adjusted (modified) Wald interval
 Wilson score binomial interval
 Exact binomial, ClopperPearson (conventional P) interval
All of these can be calculated, as 95% confidence limits, as follows:
Note, these instructions assume you have already installed the epitools function library.
> # Wald interval (no c.c.)
> binom.approx(y, n)
x n proportion lower upper conf.level
1 5 25 0.2 0.04320288 0.3567971 0.95
>
> # Adjusted Wald interval
> binom.approx(y+1.96, n+1.96^2)
x n proportion lower upper conf.level
1 6.96 28.8416 0.2413181 0.08516046 0.3974757 0.95
>
> # Wilson score binomial interval
> binom.wilson(y, n)
x n proportion lower upper conf.level
1 5 25 0.2 0.08860585 0.3913095 0.95
>
> # Conventional exact binomial interval
> binom.exact(y, n)
x n proportion lower upper conf.level
1 5 25 0.2 0.06831146 0.4070374 0.95 
  
Note:
 binom.approx calculates an interval using the normal approximation to the binomial distribution. The intervals are not continuity corrected, which is why they are sometimes described as 'equivalent to midP' intervals. To obtain continuity corrected intervals use the textbook formula  as in our worked example.
 Given that 1.96 ≅ 2, you can approximate the adjusted Wald 95% interval, as follows:
But WHY you should wish to use 2 instead of 1.96, or qnorm(.975), is hard to imagine.
 binom.wilson calculates the Wilson score binomial interval. These intervals are not continuity corrected.
 binom.exact gives the conventional exact binomial interval obtained by test inversion using the binomial test.

MidP exact by test inversion
The following code instructs R to estimate midP exact binomial confidence limits by test inversion, using a predetermined series of values of its test parameter (P).
Gave us:
> CL # Lower conf. limit
[1] 0.07718503
> CU # Upper conf. limit
[1] 0.3890471

  
Note:
 In order to make this code quick and simple, these midPvalues are of 1sided tests. Using 2sided tests would produce the same result, but only because we are using midPvalues.
 The simplest way to increase the accuracy of these intervals is to increase R=1000 to perhaps R=100000.

Pvalue plots
Since Pvalue plots are commonly used as analogues of (2sided) confidence limits, they are usually plotted using Pvalues from 2sided tests  even when it is computationally easier to obtain those Pvalues from 1sided tests. Then again, for various reasons you may prefer to rearrange plots of 1sided Pvalues such that Pvalues greater than 0.5 are plotted as 1Pvalue. Plots thusrearranged are identical to those of 2sided tests  provided those tests use midPvalues, or the statistic's distribution is smooth.
The code below produces a Pvalue plot, using midPvalues from 1sided exact binomial tests of the observed proportion p (=f/n), and a predefined set of uniformlydistributed values for these tests' nulldistribution parameter (P).
Gave us:
  
Note:
 On this plot the 95% confidence limits are where these Pvalues intersect the α/2 line  which you could indicate using abline(h=0.05/2)
 Prior to rearrangement, each Pvalue is the probability of observing a proportion less than the observed value of p, plus half the probability of observing that value.
 For tests of discrete statistics using conventional Pvalues, the upper tailed Pvalue does not equal one minus the lowertailed Pvalue  so they must be obtained separately.
The following instructions give a Pvalue plot of conventional Pvalues from 2tailed tests, again using exact binomial tests.
This code sets up its own test function, and assumes you have set the number of test parameters (R).
N.B. these conventional Pvalues have a maximum which is above 0.5 (where p=P), whereas midPvalues peak at 0.5 (again where p=P).
 You could, of course obtain a Pvalue plot using midPvalues of 2tailed tests, as is done by the code below.
 The binomial test functions shown above work quickly because they can deal with many testparameters at once. This may be impossible for more complicated tests. In that situation you may have to do the tests onebyone. The following instructions use an 'iterative loop' to produce Pvalues to plot, using the midP exact binomial test function (test.mpv, given above), again with a predefined set of parameters.
Using R, this is relatively slow. But if you want to estimate confidence limits by a process of successive approximation (iteratively), each test parameter will depend upon the outcome of previous tests.

Poisson confidence interval functions
 Normal approximation Poisson interval for counts & rates
 Exact Poisson interval for counts & rates
Either of these can be calculated, as 95% confidence limits, as follows:
Note, these instructions assume you have already installed the epitools function library.
> # Approximate Poisson interval (no c.c.)
> pois.approx(y, n)
x pt rate lower upper conf.level
1 13 46021 0.0002824797 0.0001289248 0.0004360347 0.95
>
> # Exact Poisson interval
> pois.exact(y, n)
x pt rate lower upper conf.level
1 13 46021 0.0002824797 0.0001504085 0.0004830486 0.95

  

