<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/confidence-intervals-without-your-collaborators-tears/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://staff.math.su.se/hoehle/blog/2017/06/22/interpretcis.html"> Theory meets practice...</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Abstract</h2>
<p>We provide an interpretation for the confidence interval for a binomial proportion hidden as the transcript of an hypothetical statistical consulting session.</p>
<p><center><br>
<img src="https://i1.wp.com/staff.math.su.se/hoehle/blog/figure/source/2017-06-22-interpretcis/BETABINOMIALCI-1.png?w=456" data-recalc-dims="1"><br>
</center></p>
<p> <a rel="nofollow" href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank"><img alt="Creative Commons License" src="https://i1.wp.com/i.creativecommons.org/l/by-sa/4.0/88x31.png?w=456&ssl=1" data-recalc-dims="1"></a> This work is licensed under a <a rel="nofollow" href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank">Creative Commons Attribution-ShareAlike 4.0 International License</a>. The markdown+Rknitr source code of this blog is available under a <a rel="nofollow" href="https://www.gnu.org/licenses/gpl-3.0.html" target="_blank">GNU General Public License (GPL v3)</a> license from <a rel="nofollow" href="https://github.com/hoehleatsu/hoehleatsu.github.io" target="_blank"><img alt="github" src="https://i2.wp.com/staff.math.su.se/hoehle/blog/images/GitHub-Mark-32px.png?w=456" data-recalc-dims="1"></a>.</p>
<p><h2>The Statistical Consulting Session</h2>
<p><strong>[Client]:</strong> So I did this important experiment where I investigated 420 turtles whether they had a specific phenotype trait. 52 out of my 420 turtles had the trait, i.e. the proportion in the sample was 12.4%. Now I’d like to state a 95% confidence interval for what the proportion of the trait is in the population of turtles (which is pretty large).</p>
<p><strong>[Statistician]:</strong> Ok, that should be possible. Since your population is large, we do not need a finite population correction, Since your sample is also pretty large, let’s therefore just go with the textbook large-sample confidence interval for a binomial proportion, that is <span>\(\hat{p} \pm 1.96 \sqrt{\hat{p}(1-\hat{p})/n}\)</span>. Don’t worry about the equation, let’s just use R and the <code>binom</code> package to compute the interval:</p>
<div>
<pre><code>(ci &lt;-<span> </span>binom::<span>binom.asymp</span>(<span>x=</span>x, <span>n=</span>n, <span>conf.level=</span><span>0.95</span>))</code></pre>
</div>
<pre><code>##       method  x   n      mean      lower     upper
## 1 asymptotic 52 420 0.1238095 0.09231031 0.1553087</code></pre>
<p>So the requested 95% CI is 9.2%- 15.5%. Happy?</p>
<p><strong>[Client]:</strong> Well, yes, that looks nice, but I wonder how I can interpret the confidence interval. Is it correct to say that the interval denotes a region, which contains the true value with 95% probability?</p>
<p><strong>[Statistician]:</strong> A confidence interval constructing procedure yields a random interval, because it depends on quantities (in particular <span>\(x\)</span>), which are random. However, once you plug-in the realization of the random variable (i.e. your observation), you get a realization of the confidence interval consisting of two numbers. Would we know the true value then we could tell, whether the value is covered by this particular interval or not. The specific interval we calculated above thus contains the true value with probability 0% or 100% – which of the two is the cases we unfortunately do not know.</p>
<p>The correct interpretation is thus that the confidence interval is constructed by a procedure, which, when you repeat the experiment many many times, is such that in 95% of the experiments the corresponding confidence interval would cover the true value. I can illustrate this using a small simulation study in R. Let’s assume your true proportion is 10%.</p>
<div>
<pre><code>##Assumed true value of the proportion of turtles with the trait
theta_true &lt;-<span> </span><span>0.1</span>

##Prepare data.frame with 10000 realizations of the experiment
df &lt;-<span> </span><span>data.frame</span>(<span>x=</span><span>rbinom</span>(<span>n=</span><span>1e4</span>, <span>size=</span>n, <span>prob=</span>theta_true))

##Compute 95% CI for each experiment
df %&lt;>%<span> </span><span>do</span>( {
  <span>as.data.frame</span>(<span>cbind</span>(.,<span>rate=</span>.$x/n,binom::<span>binom.asymp</span>(<span>x=</span>.$x, <span>n=</span>n)[<span>c</span>(<span>&quot;lower&quot;</span>,<span>&quot;upper&quot;</span>)]))
})
##Check if the interval covers the true value
df %&lt;>%<span> </span><span>mutate</span>(<span>covers_true_value =</span> lower <span> </span>theta_true &amp;<span> </span>upper ><span> </span>theta_true)

##Proportion of intervals covering the true value. This would be 95%
df %>%<span> </span><span>summarise</span>(<span>coverage =</span> <span>mean</span>(covers_true_value))</code></pre>
</div>
<pre><code>##   coverage
## 1   0.9446</code></pre>
<p>A graphic for the first 50 experiments shows their point estimates, corresponding intervals and how they overlap the true value or not: <img src="https://i0.wp.com/staff.math.su.se/hoehle/blog/figure/source/2017-06-22-interpretcis/unnamed-chunk-6-1.png?w=456" data-recalc-dims="1"></p>
<p>The specific confidence interval <code>ci</code> we computed above is thus just one of many possible confidence intervals originating from above procedure.</p>
<p><strong>[Client]:</strong> Ok, I get that is what happens when you do it many times. But I just have this one experiment with x = 52 out of n = 420 subjects having the trait of interest. Your above output does contain a very specific CI, with some very specific numbers, i.e. 9.2%- 15.5%. Is the true value in this interval with 95% probability?</p>
<p><strong>[Statistician]:</strong> No, the realized interval either covers the true value or not. But since we do not know the true value it’s a bit like Schrödinger’s cat… To keep things very sketchy: The width of the interval gives you an indication of your estimation certainty, but the particular values are hard to interpret – except maybe as the critical limits in a corresponding hypothesis test.</p>
<p><strong>[Client]:</strong> I’m sorry, but this is <strong>useless</strong>! You suggest to follow a common statistical protocol, you compute some numbers, but these numbers don’t really mean anything?</p>
<p><strong>[Statistician]:</strong> Sorry, that’s the way it is. However, allow me to offer a different explanation: We both appear to agree that there is a true value of the proportion, right? Let’s denote this <span>\(\theta\)</span> with <span>\(0 \leq \theta \leq 1\)</span>. We don’t know the true value, but we might have some prior idea about the range of plausible values for it. Would you be willing to characterize your belief about this by a distribution for <span>\(\theta\)</span>? This could be something as simple as just assuming <span>\(\theta \sim U(0,1)\)</span>, i.e. you would assume that every value of <span>\(\theta\)</span> between zero and one is equally probable initially. Then the interval we computed above denotes a 95% equal tail probability <strong>credibility region</strong> in a Bayesian framework when we assume this flat prior and use an asymptotic argument resulting in the posterior being Gaussian with the same mean and variance as the one would get from the asymptotic frequentist procedure.</p>
<p><strong>[Client]:</strong> Umm, ok, but how’s that helpful?</p>
<p><strong>[Statistician]:</strong> In the Bayesian context a 95% credibility region is a summary of your posterior belief about the parameter. Hence, it is ok to interpret this interval as that your belief after seeing the data is such that <em>the true value is in that interval with 95% probability</em>.</p>
<p><strong>[Client]:</strong> I love this Bayes thing! This is what I wanted initially. But hang on, I don’t really believe in all values of the parameter being initially equally probable. I’ve seen some previous studies in the literature who under pretty similar conditions find that proportion is around 5-15%. How would I incorporate that?</p>
<p><strong>[Statistician]:</strong> You could modify the large-sample Gaussian posterior such that it includes this prior information. Instead of using Gaussians you could alternatively also use a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Beta_distribution" target="_blank">Beta distribution</a> to perform so called <strong>conjugate prior-posterior updating</strong> of your belief about the true proportion. Let’s say the 5-15% denotes your prior 95% credibility region for the parameter. We would use this to find the parameters of a beta distribution. Then we would update your prior belief with the observed data to get the posterior distribution. A feature of such a conjugate approach is that this updated distribution, i.e. the posterior, is again beta. Doing this in R is easy:</p>
<div>
<pre><code>##Function to determine beta parameters s.t. the 2.5% and 97.5% quantile match the specified values
target &lt;-<span> </span>function(theta, prior_interval, <span>alpha=</span><span>0.05</span>) {
  <span>sum</span>( (<span>qbeta</span>(<span>c</span>(alpha/<span>2</span>, <span>1</span>-alpha/<span>2</span>), theta[<span>1</span>], theta[<span>2</span>]) -<span> </span>prior_interval)^<span>2</span>)
}

##Find the prior parameters
prior_params &lt;-<span> </span><span>optim</span>(<span>c</span>(<span>10</span>,<span>10</span>),target, <span>prior_interval=</span><span>c</span>(<span>0.05</span>, <span>0.15</span>))$par
prior_params</code></pre>
</div>
<pre><code>## [1]  12.04737 116.06022</code></pre>
<p>Actually, you can interpret the above prior parameters as the number of turtles you have seen with the trait (12) and without the trait (116), respectively, before conducting your above investigation. The posterior parameters are then just 12 + 52 = 64 turtles with the trait and 116 + 368 = 484 turtles without the trait. You get the credible interval based on this posterior in R by:</p>
<div>
<pre><code>##Compute credibile interval from a beta-binomial conjugate prior-posterior approach
(ci_bayes &lt;-<span> </span>binom::<span>binom.bayes</span>(<span>x=</span>x, <span>n=</span>n, <span>type=</span><span>&quot;central&quot;</span>, <span>prior.shape1=</span>prior_params[<span>1</span>], <span>prior.shape2=</span>prior_params[<span>2</span>]))</code></pre>
</div>
<pre><code>##   method  x   n   shape1   shape2      mean      lower     upper  sig
## 1  bayes 52 420 64.04737 484.0602 0.1168518 0.09134069 0.1450096 0.05</code></pre>
<p>Graphically, your belief updating looks as follows:</p>
<div>
<pre><code>##Plot of the beta-posterior
p2 &lt;-<span> </span>binom::<span>binom.bayes.densityplot</span>(ci_bayes)
##Add plot of the beta-prior
df &lt;-<span> </span><span>data.frame</span>(<span>x=</span><span>seq</span>(<span>0</span>,<span>1</span>,<span>length=</span><span>1000</span>)) %>%<span> </span><span>mutate</span>(<span>pdf=</span><span>dbeta</span>(x, prior_params[<span>1</span>], prior_params[<span>2</span>]))
p2 +<span> </span><span>geom_line</span>(<span>data=</span>df, <span>aes</span>(<span>x=</span>x, <span>y=</span>pdf), <span>col=</span><span>&quot;darkgray&quot;</span>,<span>lty=</span><span>2</span>) +
<span> </span><span>coord_cartesian</span>(<span>xlim=</span><span>c</span>(<span>0</span>,<span>0.25</span>)) +<span> </span><span>scale_x_continuous</span>(<span>labels=</span>scales::percent)</code></pre>
</div>
<p><img src="https://i1.wp.com/staff.math.su.se/hoehle/blog/figure/source/2017-06-22-interpretcis/BETABINOMIALCI-1.png?w=456" data-recalc-dims="1"></p>
<p>The dashed line in the graphic shows the density of your beta prior distribution. The shaded region shows the density of your beta posterior distribution, in particular, the gray shaded area denotes your 95% credibility region on the x-axis. One advantage of the beta-binomial approach is that this ensures that the prior as well as the posterior distribution have the same support as your parameter, i.e. the interval between zero and one.</p>
<p><strong>[Client]:</strong> So I can really write that the interval 9.1%- 14.5% contains the true parameter with a probability of 95%?</p>
<p><strong>[Statistician]:</strong> Depending on the technical level of your readership you might want to state that it’s a 95% equi-tailed credible interval resulting from a beta-binomial conjugate Bayesian approach obtained when using a prior beta with parameters such that the similar 95% equi-tailed <strong>prior credible interval</strong> has limits 0.05 and 0.15. Given these assumptions the interval 9.1%- 14.5% contains 95% of your subjective posterior density for the parameter.</p>
<p><strong>[Client]:</strong> ???</p>
<p><strong>[Statistician]:</strong> You might just want to skip all the details and just write that the <strong>95% Bayesian confidence interval</strong> is 9.1%- 14.5%.</p>
<p><strong>[Client]:</strong> And what if the reviewers do not agree with my prior distribution?</p>
<p><strong>[Statistician]:</strong> Then return to your flat prior, i.e. the uniform. Nobody usually questions that even though you just told me that you don’t believe in it yourself:</p>
<div>
<pre><code>(ci_bayes_unif &lt;-<span> </span>binom::<span>binom.bayes</span>(<span>x=</span>x, <span>n=</span>n, <span>type=</span><span>&quot;central&quot;</span>, <span>prior.shape1=</span><span>1</span>, <span>prior.shape2=</span><span>1</span>))</code></pre>
</div>
<pre><code>##   method  x   n shape1 shape2      mean      lower    upper  sig
## 1  bayes 52 420     53    369 0.1255924 0.09574062 0.158803 0.05</code></pre>
<p><strong>[Client]:</strong> Alright, that’s settled then – I just use a Bayesian confidence interval with a flat prior and am done. That was not so hard after all. Now to something else, the reviewers of the paper, which by the way has to be revised by tomorrow, also asked us to substantiate our findings with a <strong>p-value</strong>. How would I get a p-value related to the above?</p>
<p><strong>[Statistician]:</strong> Look how late it already is! I really need to go. I have this Bayesian Methods class to teach…</p>
<div>
</div>
	<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://staff.math.su.se/hoehle/blog/2017/06/22/interpretcis.html"> Theory meets practice...</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/oIyFXOjQaTI" height="1"></a></div>