Sample size from proportions Let $X$ be a random variable that follows a Bernoulli distribution with parameter $p$. If the maximum error of the $90$% confidence interval is $0.2$, what sample size is required under the following scenarios
(i) $p$ is unknown.
(ii) $p \geq 0.8$
My attempt:
(i)
As $p$ is unknown the sample size needed is:
$$n = \frac{z_{\alpha/2}^2 \cdot 0.25}{E^2} = \frac{1.6448536^2 \cdot 0.25}{0.2^2} \approx 16.9096$$
We take the upper integer $17$.
(ii)
$p\geq 0.8 \implies 1-p \leq 0.2 \implies p(1-p) \leq 0.16$
The sample size needed here is:
$$n = \frac{z_{\alpha/2}^2 \cdot 0.16}{E^2} = \frac{1.6448536^2 \cdot 0.16}{0.2^2} \approx 10.8222$$
We take the upper integer $11$.
Are these correct? Any assistance is appreciated.
 A: Reliable CIs for $n = 11$ and $p<.2$ or $p>.8$ should not be based on
the formula for margin of error $E$ you are using. You should use some
kind of exact binomial Ci.
For example, in R, the procedure binom.test makes a style of CI with
reliable coverage probability. It uses exact binomial CDFs instead of
normal approximation. (See the Wikipedia page on binomial confidence
intervals for several styles of intervals.)
Here are two confidence intervals based on $X \sim \mathsf{Binom}(n=11,p=.8).$
They have margins of error $E = 0.247$ and $0.275.$ respectively.
set.seed(2021)
CI = binom.test(rbinom(1,11,.8), 11)$conf.int 
CI; diff(CI);  diff(CI)/2
[1] 0.4822441 0.9771688
attr(,"conf.level")
[1] 0.95
[1] 0.4949247  # length of CI
[1] 0.2474623  $ E

CI = binom.test(rbinom(1,11,.8), 11)$conf.int 
CI; diff(CI);  diff(CI)/2
[1] 0.3902574 0.9397823
attr(,"conf.level")
[1] 0.95
[1] 0.5495248
[1] 0.2747624

In a simulation of 100,000 95% CIs with $n=11, p=0.8,$ the average
margin of error was $0.24,$ not $0.20.$
set.seed(410)
E = replicate(10^5, diff(binom.test(rbinom(1,11,.8), 11)$conf.int)/2)
mean(E)
[1] 0.2418242

Using $n = 20$ would probably be better:
set.seed(410)
E = replicate(10^5, diff(binom.test(rbinom(1,20,.8), 20)$conf.int)/2)
mean(E)
[1] 0.1834582

hist(E, prob=T, col="skyblue2")


