<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/another-rule-of-three-this-one-in-statistics/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.decisionsciencenews.com/2017/04/14/another-rule-three-one-statistics/"> R – Decision Science News</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>SAMPLE UNTIL THE FIRST SUCCESS, PUT A CONFIDENCE INTERVAL ON P(SUCCESS)</p>
<p>
<a target="_blank" rel="nofollow" href="https://i0.wp.com/www.decisionsciencenews.com/wp-content/uploads/2017/04/upper_bound_lastpossible.png"><img sizes="(max-width: 485px) 100vw, 485px" alt="" class="wp-image-6134" src="https://i0.wp.com/www.decisionsciencenews.com/wp-content/uploads/2017/04/upper_bound_lastpossible.png?w=450" data-recalc-dims="1"></a>
</p>
<p>We wrote last week of Charles Darwin’s love of the “rule of three” which, <a target="_blank" rel="nofollow" href="http://www.decisionsciencenews.com/2017/04/05/darwin-rule-three-little-use-higher-mathematics/">according to Stigler</a> “is simply the mathematical proposition that if a/b = c/d, then any three of a, b, c, and d suffice to determine the fourth.”</p>
<p>We were surprised to learn this is called the rule of three, as we had only heard of the rule of three in comedy. We were even more surprised when a reader wrote in telling us about <strong>a rule of three in statistics</strong>. <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Rule_of_three_(statistics)">According to Wikipedia</a>: the rule of three states that if a certain event did not occur in a sample with n subjects … the interval from 0 to 3/n is a 95% confidence interval for the rate of occurrences in the population.</p>
<p>It’s a heuristic. We love heuristics!</p>
<p>We decided to give it a test run in a little simulation. You can imagine that we’re testing for defects in products coming off of a production line. Here’s how the simulation works:</p>
<ul>
<li>We test everything that comes off the line, one by one, until we come across a defect (the n+1st test).</li>
<li>We then make a confidence interval bounded by 0 and 3/n. and make note of it. 95% of such intervals should contain the true underlying probability of defect.</li>
<li>Because it’s a simulation and we know the true underlying probability of defect, we make note of whether the interval contains the true probability of defect.</li>
<li>We repeat this 10,000 times at each of the following underlying probabilities: .001, .002, and .003.</li>
</ul>
<p>The simulation thus far assumes the testers have the patience to keep testing until they find a defect. In reality, however, they might get bored and stop testing before the first defect is found. To address this, we also simulated another condition in which the testing is stopped at n/2, halfway before the first defect is found. Of course, in reality, one has no way of knowing when one is half the way to the first defective test, but our simulation can at least let us know what kind of confidence interval one will get if one does indeed stop halfway.</p>
<p>Here’s the result on bracketing, that is, the probability that the confidence intervals contain the correct value:</p>
<p>
<a target="_blank" rel="nofollow" href="https://i0.wp.com/www.decisionsciencenews.com/wp-content/uploads/2017/04/bracketing.png"><img sizes="(max-width: 485px) 100vw, 485px" alt="" class="wp-image-6131" src="https://i0.wp.com/www.decisionsciencenews.com/wp-content/uploads/2017/04/bracketing.png?w=450" data-recalc-dims="1"></a>
</p>
<p>Across all three levels of true underlying probability, when stopping just before the first defect, we get 95% confidence intervals. However, when we stop half way to the first defect, we get closer to 100% intervals (99.73%, 99.80%, and 99.86%, respectively).</p>
<p>So we know that the upper bounds of these confidence intervals fall above the true probability 95% to about 100% of the time, but where to they fall?</p>
<p>In the uppermost figure of this post, we see the locations of the upper bounds of the simulated confidence intervals when we stop immediately before the first defect. For convenience, we draw blue lines at the true underlying probabilities of .001 (top), .002 (middle), and .003 (bottom). When it’s a 95% confidence interval, about 95% of the upper bounds should fall to the right of the blue line, and 5% to the left. We cut the axis at .05 so you can actually see something, but realize it extends all the way to 1.0, with the heights of the bars trailing off.</p>
<p>For comparison, let’s look at the case in which we stop halfway to the first defect. As suggested by the bracketing probabilities, here we see almost all of the upper bounds exceed the true underlying probability. As our applied statistician reader wrote us about the rule of three, “the weakness is that in some situations it’s a very broad confidence interval.”</p>
<p>
<a target="_blank" rel="nofollow" href="https://i1.wp.com/www.decisionsciencenews.com/wp-content/uploads/2017/04/upper_bound_halfway.png"><img sizes="(max-width: 485px) 100vw, 485px" alt="" class="wp-image-6133" src="https://i1.wp.com/www.decisionsciencenews.com/wp-content/uploads/2017/04/upper_bound_halfway.png?w=450" data-recalc-dims="1"></a>
</p>
<p>REFERENCE<br>
A Look at the Rule of Three<br>
B. D. Jovanovic and P. S. Levy<br>
The American Statistician<br>
Vol. 51, No. 2 (May, 1997), pp. 137-139<br>
DOI: 10.2307/2685405<br>
Stable URL: http://www.jstor.org/stable/2685405</p>
<p>R CODE FOR THOSE WHO WANT TO PLAY ALONG AT HOME<br>
<code><br>
require(scales)<br>
library(ggplot2)<br>
library(dplyr)<br>
levels = c(.001,.002,.003)<br>
ITER = 10000<br>
res_list = vector('list', ITER*length(levels))<br>
index=1<br>
for(true_p in levels) {<br>
for (i in 1:ITER) {<br>
onesam = sample(<br>
x = c(1, 0),<br>
size = 10*1/true_p,<br>
prob = c(true_p, 1 - true_p),<br>
replace = TRUE<br>
)<br>
cut = which.max(onesam) - 1<br>
upper_bound_halfway = min(3 / (cut/2),1)<br>
upper_bound_lastpossible = min(3/cut,1)<br>
res_list[[index]] =<br>
data.frame(<br>
true_p = true_p,<br>
cut = cut,<br>
upper_bound_halfway = upper_bound_halfway,<br>
bracketed_halfway = true_p &lt; upper_bound_halfway,<br>
upper_bound_lastpossible = upper_bound_lastpossible,<br>
bracketed_lastpossible = true_p &lt; upper_bound_lastpossible ) index=index+1 } } df = do.call('rbind',res_list) rm(res_list) plot_data = rbind( df %>% group_by(true_p) %>% summarise(bracketing_probability = mean(bracketed_halfway),type=&quot;halfway&quot;),<br>
df %>% group_by(true_p) %>% summarise(bracketing_probability = mean(bracketed_lastpossible),type=&quot;last possible&quot;)<br>
)<br>
plot_data<br>
p=ggplot(plot_data,aes(x=true_p,y=bracketing_probability,group=type,fill=type)) +<br>
geom_bar(stat=&quot;identity&quot;,position=&quot;dodge&quot;) +<br>
coord_cartesian(ylim=c(.5,1)) +<br>
theme_bw() +<br>
theme(legend.position = &quot;bottom&quot;,<br>
panel.grid.minor.x = element_blank()) +<br>
labs(x=&quot;True Probability&quot;,y=&quot;Bracketing Probability&quot;)<br>
p<br>
ggsave(plot=p,file=&quot;bracketing.png&quot;,width=4,height=4)<br>
plot_data2 = df %>%<br>
dplyr::select(-bracketed_halfway,-bracketed_lastpossible) %>%<br>
tidyr::gather(bound_type,upper_bound,c(upper_bound_halfway,upper_bound_lastpossible)) %>%<br>
arrange(bound_type,upper_bound) %>%<br>
mutate(bin = floor(upper_bound/.001)*.001) %>%<br>
group_by(bound_type,true_p,bin) %>%<br>
summarise(count = n()) %>%<br>
ungroup()<br>
p=ggplot(subset(plot_data2,bound_type==&quot;upper_bound_lastpossible&quot;),aes(x=bin+.0005,y=count)) +<br>
geom_bar(stat=&quot;identity&quot;,width = .0005) +<br>
geom_vline(aes(xintercept = true_p),color=&quot;blue&quot;) +<br>
coord_cartesian(xlim = c(0,.05)) +<br>
labs(x=&quot;Upper Bound&quot;,y=&quot;Count&quot;) +<br>
facet_grid(true_p ~ .) +<br>
theme_bw() +<br>
theme(legend.position = &quot;none&quot;)<br>
p<br>
ggsave(plot=p,file=&quot;upper_bound_lastpossible.png&quot;,width=4,height=4)<br>
#repeat for upper_bound_halfway<br>
p=ggplot(subset(plot_data2,bound_type==&quot;upper_bound_halfway&quot;),aes(x=bin+.0005,y=count)) +<br>
geom_bar(stat=&quot;identity&quot;,width = .0005) +<br>
geom_vline(aes(xintercept = true_p),color=&quot;blue&quot;) +<br>
coord_cartesian(xlim = c(0,.05),ylim=c(0,1750)) +<br>
labs(x=&quot;Upper Bound&quot;,y=&quot;Count&quot;) +<br>
facet_grid(true_p ~ .) +<br>
theme_bw() +<br>
theme(legend.position = &quot;none&quot;)<br>
p<br>
ggsave(plot=p,file=&quot;upper_bound_halfway.png&quot;,width=4,height=4)<br>
</code></p>
<p>The post <a target="_blank" rel="nofollow" href="http://www.decisionsciencenews.com/2017/04/14/another-rule-three-one-statistics/">Another rule of three, this one in statistics</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.decisionsciencenews.com/">Decision Science News</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.decisionsciencenews.com/2017/04/14/another-rule-three-one-statistics/"> R – Decision Science News</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/cHOoNGE1hKI"></a></div>