<h2>Abstract</h2>
<p>If your tap water suddenly becomes brown while authorities claim everything is okay, you start to worry. <span>Langkjær-Bain (2017)</span> tells the <a href="https://en.wikipedia.org/wiki/Flint_water_crisis">Flint Water Crisis</a> story from a statistical viewpoint: essentially the interest is in whether the 90th percentile in a sample of lead concentration measurements is above a certain threshold or not. We illustrate how to perform the necessary calculations with R's quantile function and show that the type-argument of the function matters.</p>
<p><br><a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"></a> This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>. The markdown+Rknitr source code of this blog is available under a <a href="https://www.gnu.org/licenses/gpl-3.0.html">GNU General Public License (GPL v3)</a> license from <a href="https://github.com/hoehleatsu/hoehleatsu.github.io"></a>.</p>
<h2>Introduction</h2>
<p>In a recent <a href="https://www.significancemagazine.com/">Significance</a> article, <span>Langkjær-Bain (2017)</span> tells the story about the <a href="https://en.wikipedia.org/wiki/Flint_water_crisis">Flint water crisis</a>. In 2014 the city of Flint, Michigan, USA, decided to change its water supply to Flint River. Due to insufficient treatment of the water with corrosion inhibitors the lead concentration in the drinking water increased, because lead in the aging pipes leached into the water supply. This created serious health problems - as explained in this <a href="https://youtu.be/QnDQFivtCd0">short video summary</a>. In this blog post we investigate further the computation of the 90th percentile of the tap water lead concentration samples described in <span>Langkjær-Bain (2017)</span>. Quantile estimation in this context has already been discussed in a recent blog entry entitled <a href="https://blogs.sas.com/content/iml/2017/05/17/quantiles-flint-water-crisis.html">Quantiles and the Flint water crisis</a> by <a href="https://blogs.sas.com/content/author/rickwicklin/">Rick Wicklin</a>.</p>
<p>The monitoring of drinking water in the US is regulated by the <a href="https://www.epa.gov/dwreginfo/lead-and-copper-rule">Lead and Copper Rule</a> of the United States Environmental Protection Agency. The entire text of the rule is available as electronic code of federal regulation (e-CFR) Title 40: Protection of Environment, <a href="https://www.ecfr.gov/cgi-bin/text-idx?SID=531617f923c3de2cbf5d12ae4663f56d&mc=true&node=sp40.23.141.i&rgn=div6">Part 141 - National Primary Drinking Water Regulations</a>. In particular the regulation defines a sampling plan for collecting tap water samples. The size of the sample depends on the number of people the water system serves. In case this number is bigger than 100,000 a sample of 100 sites is needed. If there are 10,001-100,000 people served, then a sample from 60 sites is needed. For systems serving fewer than 10,000 sizes of 40, 20, 10 and 5 are defined - see <a href="https://www.ecfr.gov/cgi-bin/text-idx?SID=531617f923c3de2cbf5d12ae4663f56d&mc=true&node=sp40.23.141.i&rgn=div6#se40.25.141_186">§141.86(c)</a> of the rule for details. Of interest for this blog post is that action needs to be taken, if too many of the samples are above a given threshold of 15 part per billion (ppb):</p>
Figure: Excerpt of <a href="https://www.ecfr.gov/cgi-bin/text-idx?SID=531617f923c3de2cbf5d12ae4663f56d&mc=true&node=sp40.23.141.i&rgn=div6#se40.25.141_180">CFR 40 §141.80</a> with the quantile calculation highlighted in yellow.
<p>
</p>
<p>We note the explicit duality in the CFR between the in more than 10% and the 90% quantile in the text. However, we also note that it is not clear how to proceed, if the number calculated in (c)(3)(ii) is <strong>not an integer</strong>. This is not a problem per se, because the CFR itself only operates with samples sizes 100, 60, 40, 20, 10 and 5, where 0.9 times the sample size always gives an integer number. But if one for some reason does not obtain exactly the required number this quickly can become an issue as we shall see below.</p>
<h2>The Flint Data</h2>
<p>The data of the spring 2015 Flint water supply monitoring conducted by the Michigan Department of Environmental Quality are presented in the figure on p. 20 of <span>Langkjær-Bain (2017)</span>. Alternatively, they can also be taken directly from the Blog entry <a href="https://blogs.sas.com/content/iml/2017/05/17/quantiles-flint-water-crisis.html">Quantiles and the Flint water crisis</a> by <a href="https://blogs.sas.com/content/author/rickwicklin/">Rick Wicklin</a>.</p>
<div><pre><code>##Read flint water monitoring data (the column lead is the measurement)
flint &lt;-<span> </span><span>read.csv</span>(<span>file=</span><span>file.path</span>(filePath,<span>&quot;flint.csv&quot;</span>))
##Sort the measured lead values in ascending order
lead  &lt;-<span> </span><span>sort</span>(flint$lead)
##Number of observations
n &lt;-<span> </span><span>length</span>(lead)</code></pre></div>
<p></p>
<p>The proportion of observations above the 15 ppb threshold is</p>
<div><pre><code><span>mean</span>(lead ><span> </span><span>15</span>)</code></pre></div>
<pre><code>## [1] 0.1126761</code></pre>
<p>In other words, the proportion 11.3% is above the 10% action threshold and, hence, something needs to be done. However, as <span>Langkjær-Bain (2017)</span> describes, the story is a little more complicated, because two of the values above 15 were removed with the argument that they originated from sites which are not covered by the sampling plan. Only private households at high risk, i.e. with lead pipelines, are supposed to be sampled. As one can read in the article the removal is highly controversial, in particular, because the proportion of critical observations falls below the 10% action threshold when these two values are removed. For this blog entry, we will, however, work with the full <span>\(n=71\)</span> sample and focus on the <strong>quantile aspect</strong> of the calculation.</p>
<h2>Percentages and Quantiles</h2>
<p>Let <span>\(n\)</span> denote the size of the selected sample, which in our case is <span>\(n=71\)</span>. If more than 10% of the sample is to be above 15 ppb, this means that <span>\(\lfloor 0.1\cdot n + 1\rfloor\)</span> of the samples need to be above 15 ppb, where <span>\(\lfloor y \rfloor\)</span> denotes the largest integer less than or equal to <span>\(y\)</span>. We shall denote this the <strong>number of critical samples</strong>. If we divide this number by <span>\(n\)</span> we get the actual proportion of critical samples needed before action. It is worthwhile noting the difference between this critical proportion and the 10% threshold illustrated by the sawtooth curve in the figure below. The explanation for these sawtooth step-spikes is the discreteness of the decision problem (i.e. <span>\(x\)</span> out of <span>\(n\)</span>).</p>
<p></p>
<p>Turning to the equivalent 90% quantile is above 15 ppm decision criterion of the CFR, one will need to determine the 90% quantile from the finite sample of lead concentration measurements. How to estimate quantiles with statistical software is discussed in the excellent survey of <span>Hyndman and Fan (1996)</span>. In their work they describe the nine different quantile estimators implemented in R's <code>quantile</code> function. The estimators are all based on the <a href="https://en.wikipedia.org/wiki/Order_statistic">order statistic</a> of the sample, i.e. let <span>\(x_{(1)} \leq x_{(2)} &lt; \cdots \leq x_{(n)}\)</span> denote the ordered lead concentration measurements. Then the simplest estimator for the <span>\(p\cdot 100\%\)</span> quantile is</p>
<p><span>\[
\hat{x}_p = \min_{k} \left\{\hat{F}(x_{(k)}) \geq p\right\} =
x_{(\lceil n \cdot p\rceil)},
\]</span></p>
<p>where <span>\(\hat{F}\)</span> is the <a href="https://en.wikipedia.org/wiki/Empirical_distribution_function">empirical cumulative distribution</a> function of the sample and <span>\(\lceil y \rceil\)</span> denotes the smallest integer greater than or equal to <span>\(y\)</span>. This method corresponds to R's <code>type=1</code>. For our application <span>\(p\)</span> would be 0.9 and the 90th percentil for the Flint data is thus</p>
<div><pre><code><span>c</span>(<span>&quot;manual.90%&quot;</span>=lead[<span>ceiling</span>(n*p)], <span>R=</span><span>quantile</span>(lead, <span>probs=</span>p, <span>type=</span><span>1</span>))</code></pre></div>
<pre><code>## manual.90%      R.90% 
##         18         18</code></pre>
<p>which is above the action threshold of 15 ppb. It is also important to note that when <span>\(x_{(\lceil n \cdot 0.9\rceil)} > 15\> \text{ppb}\)</span> then a total <span>\(n - \lceil n \cdot 0.9\rceil + 1\)</span> samples are above 15 ppm. In other words the proportion of samples above 15 ppm in this situation is <span>\[ \frac{n - \lceil n \cdot 0.9\rceil + 1}{n}.\]</span> To show the duality between the more than 10% critical samples and the 90% quantile being above 15 ppm we thus need to show that <span>\((n - \lceil n \cdot 0.9\rceil + 1)/n = (\lfloor 0.1\cdot n + 1\rfloor)/n\)</span>. This is possible since the following <a href="https://en.wikipedia.org/wiki/Floor_and_ceiling_functions">relations hold for the floor and ceiling functions</a>: <span>\[ \begin{align*}
- \lceil y \rceil &amp;= \lfloor -y \rfloor \quad \text{and} \\
n + \lfloor y \rfloor &amp;= \lfloor n + y \rfloor,
\end{align*}
\]</span> with <span>\(n\)</span> being an integer and <span>\(y\)</span> any real number. Thus, <span>\[
(n+1) - \lceil n \cdot 0.9\rceil = (n+1) + \lfloor -n \cdot 0.9\rfloor =
\lfloor (n+1) - n \cdot 0.9\rfloor = \lfloor 0.1n+1 \rfloor.
\]</span></p>
<p><strong>Important conclusion</strong>: We have thus shown that with the <code>type=1</code> quantile method we have the duality between having more than 10% critical samples and the 90th percentile of the measurements being above 15 ppm.</p>
<h3>Other Quantile Types</h3>
<p>Since <span>\(\hat{F}\)</span> has jumps of size <span>\(1/n\)</span> the actual value of <span>\(\hat{F}(\hat{x}_{p})\)</span> can end up being somewhat larger than the desired <span>\(p\)</span>. Therefore, <span>Hyndman and Fan (1996)</span> prefer estimators interpolating between two adjacent order statistics. Also because such estimators have a lower mean squared error in most cases <span>(Dielman, Lowry, and Pfaffenberger 1994)</span>. As an example of such an extended estimator, the <code>type=5</code> quantile estimator is defined by letting <span>\(h=p n + 1/2\)</span> and then computing: <span>\[
\hat{x}_p = x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h
\rfloor + 1} - x_{\lfloor h \rfloor}).
\]</span></p>
<p>Doing this either manually or using the <code>quantile</code> function one obtains:</p>
<div><pre><code>## Small function to manually compute the type=5 p*100th quantile
## for the sample x
manual_quantile_5 &lt;-<span> </span>function(x, p) {
  h &lt;-<span> </span><span>length</span>(x)*p +<span> </span><span>0.5</span>
  x &lt;-<span> </span><span>sort</span>(x)
  <span>return</span>(x[<span>floor</span>(h)] +<span> </span>(h -<span> </span><span>floor</span>(h))*<span> </span>(x[<span>floor</span>(h)+<span>1</span>] -<span> </span>x[<span>floor</span>(h)]))
}

<span>c</span>(<span>&quot;manual.90%&quot;</span>=<span>manual_quantile_5</span>(lead, <span>p=</span><span>0.9</span>), <span>R=</span><span>quantile</span>(lead, <span>probs=</span><span>0.9</span>,<span>type=</span><span>5</span>))</code></pre></div>
<pre><code>## manual.90%      R.90% 
##       18.8       18.8</code></pre>
<p>Instead of reading the above or using the R code one can also instead watch a more <a href="https://youtu.be/9pql00zr700">didactic whiteboard explanation</a> by Professor <a href="http://www.emich.edu/math/faculty/cgardiner.php">Christopher Gardiner</a> for <a href="http://michiganradio.org/post/video-how-dropping-two-flints-lead-test-numbers-changed-things-state">Michigan Radio</a> on how to calculate the 90% quantile using a <code>type=5</code> argument for the Flint sample. However, the important point of the above calculations is that this quantile type is <strong>of limited interest</strong>, because the Lead and Copper Rule implicitly defines that one has to use the <code>type=1</code> quantile. To make this point even more explicit, we use sampling with replacement from the Flint data to construct a dataset, where the 90% <code>type=5</code>-quantile is above 15 ppm, but the percentage of samples above the 15 ppm threshold is less than 10%.</p>
<div><pre><code>##Function to compute the proportion critical as well as the 90% quantile
##using type (type)-quantiles. Returns the quantile as well as the proportion
##above the 15 ppm threshold
prop_critical_and_q90 &lt;-<span> </span>function(x, <span>type=</span><span>5</span>) {
  q90 &lt;-<span> </span><span>as.numeric</span>(<span>quantile</span>(x, <span>type=</span>type,<span>probs=</span>p))
  prop &lt;-<span> </span><span>mean</span>(x><span>15</span>)
  <span>c</span>(<span>q90=</span>q90,<span>prop=</span> prop)
}

##Make 100 datasets by sampling with replacement
r &lt;-<span> </span><span>data.frame</span>(<span>seed=</span><span>seq_len</span>(<span>100</span>)) %>%<span> </span>rowwise %>%<span> </span><span>do</span>({
  <span>set.seed</span>(.$seed)
  newdata &lt;-<span> </span><span>sample</span>(lead, <span>replace=</span><span>TRUE</span>)
  <span>as.data.frame</span>(<span>seed=</span>.$seed, <span>t</span>(<span>prop_critical_and_q90</span>(newdata)))
})

##Check which datasets violate the duality between quantile and
##percentage above threshold assumption
r %&lt;>%<span> </span><span>mutate</span>(<span>violates_duality =</span>  q90 ><span> </span><span>15</span> &amp;<span> </span>prop &lt;<span> </span><span>0.1</span>)

##Do the stats for this dataset
(five &lt;-<span> </span>r %>%<span> </span><span>filter</span>(violates_duality) %>%<span> </span><span>slice</span>(<span>1</span>:<span>5</span>))</code></pre></div>
<pre><code>## # A tibble: 5 × 3
##     q90       prop violates_duality
##   &lt;dbl>      &lt;dbl>            &lt;lgl>
## 1  15.0 0.09859155             TRUE
## 2  15.0 0.09859155             TRUE
## 3  16.2 0.09859155             TRUE
## 4  15.0 0.09859155             TRUE
## 5  15.8 0.09859155             TRUE</code></pre>
<p>We note that some of the lines in the above output are artifacts of lacking numerical precision: the quantile is only above 15 due to numerical imprecision in the calculation of the <code>type=5</code> quantile:</p>
<div><pre><code><span>print</span>(five$q90, <span>digits=</span><span>20</span>)</code></pre></div>
<pre><code>## [1] 15.000000000000028422 15.000000000000028422 16.200000000000045475
## [4] 15.000000000000028422 15.800000000000039790</code></pre>
<p>This shows that regulatory business with <a href="https://en.wikipedia.org/wiki/Floating-point_arithmetic">floating point arithmetic</a> is tricky. As a step towards fixing the problem, one could redefine the greater and less than operators, respectively, to only compare up to numerical precision:</p>
<div><pre><code>##Function to do numerical safe greater than comparision
<span>&quot;%greater%&quot;</span> &lt;-<span> </span>function(x,y) {
  equal_up_to_numerical_precision &lt;-<span> </span><span>isTRUE</span>(<span>all.equal</span>(x,y))
  <span>return</span>( (x ><span> </span>y) &amp;<span> </span>!(equal_up_to_numerical_precision) )
}

##Function to do numerical safe less than  comparision
<span>&quot;%less%&quot;</span> &lt;-<span> </span>function(x,y) {
  equal_up_to_numerical_precision &lt;-<span> </span><span>isTRUE</span>(<span>all.equal</span>(x,y))
  <span>return</span>( (x &lt;<span> </span>y) &amp;<span> </span>!(equal_up_to_numerical_precision) )
}

##Add the new column, which does &lt; and > comparisons only up to
##numerical precision
r %&lt;>%<span> </span><span>mutate</span>(<span>violates_duality_numsafe =</span>  (q90 %greater%<span> </span><span>15</span>) &amp;<span> </span>(prop %less%<span> </span><span>0.1</span>))

##Show five violation candidates for this corrected dataset
(five &lt;-<span> </span>r %>%<span> </span><span>filter</span>(violates_duality_numsafe) %>%<span> </span><span>slice</span>(<span>1</span>:<span>5</span>))</code></pre></div>
<pre><code>## # A tibble: 5 × 4
##     q90       prop violates_duality violates_duality_numsafe
##   &lt;dbl>      &lt;dbl>            &lt;lgl>                    &lt;lgl>
## 1  16.2 0.09859155             TRUE                     TRUE
## 2  15.8 0.09859155             TRUE                     TRUE
## 3  15.8 0.09859155             TRUE                     TRUE
## 4  16.2 0.09859155             TRUE                     TRUE
## 5  16.2 0.09859155             TRUE                     TRUE</code></pre>
<h2>Discussion</h2>
<p>To summarize the findings: The type of quantile estimation used in practice matters. It is not clear what to do, if <span>\(0.9\cdot n\)</span> is not integer in the estimation of the 90% quantile under the Lead and Copper Rule. For the Flint example the 63'rd sorted value is 13 which is below threshold, whereas the 64'th value is 18 which is above the threshold. If we use <code>type=1</code> then <span>\(\lceil 71\cdot 0.9\rceil=64\)</span> would be the correct value to take and the 90% quantile of the sample would be estimated to be 18 ppb. This means that the 19 ppb vertical line in the figure of <span>Langkjær-Bain (2017)</span> is a little misleading, because this appears to be the rounded <code>type=5</code> quantile. For the setting with <span>\(n=71\)</span> samples, both estimators are although above the action threshold of 15 ppb, so in the particular Flint application it does not matter so much which method to take. However, in other settings this might very well make a difference! So <strong>be careful with the type argument of the quantile function</strong>. As an example, the nine different types of R's <code>quantile</code> function provide estimates for the 90% quantile in the range from 17.50 to 19.60 for the Flint data.</p>
<p>On another note, one can discuss if it is a good idea to rely on the <code>type=1</code> quantile estimator in the rule, because it is well known that this type does not have as good estimation properties as, e.g., <code>type=5</code>. However, <code>type=1</code> is simpler to compute, <strong>ensures duality</strong> with the intuitive critical proportion, and has the property that the obtained value is always one also occurring in the sample. The later thus avoids the issue of numerical instability.</p>
<p>Finally, the <a href="https://blogs.sas.com/content/iml/2017/05/17/quantiles-flint-water-crisis.html">blog post</a> by Rick Wicklin addresses quantile estimation from an even more statistical viewpoint by computing confidence intervals for the quantile - a topic, which has been <a href="http://staff.math.su.se/hoehle/blog/2016/10/23/quantileCI.html">previously treated theoretically</a> in this blog. Compliance to a given quantile threshold based on samples has also been treated in the entirely different context of digital elevation models <span>(Höhle and Höhle 2009)</span>. Typically, tests and the dual confidence intervals are in this regulation setting formulated in a reversed way, such that one needs to provide enough evidence to show that underlying 90th percentile is indeed below 15 ppm beyond reasonable doubt. An interesting question in this context is how large the sample needs to be in order to do this with a given certainty - see <span>Höhle and Höhle (2009)</span> for details. It is, however, worthwhile pointing out that the Lead and Copper Rule does not know about confidence intervals. Currently, <strong>estimation uncertainty</strong> is only treated implicitly by specifying sample size as a function of number of people served by the water system and then hard-thresholding the result at 15 ppm.</p>
<p><strong>On a Personal Note</strong>: If you want more details on the use of confidence intervals for quantiles, join my 5 minute <a href="https://user2017.sched.com/event/Axs8/better-confidence-intervals-for-quantiles?iframe=yes&w=100%25&sidebar=yes&bg=no">lightning talk</a> on 6th of July at the <a href="https://user2017.brussels/">useR!2017 conference</a> in Brussels.</p>
<br>Photo is copyright <a href="https://www.flickr.com/photos/hz536n/29032559971/">George Thomas</a>, available under a CC BY-NC-ND 2.0 license.
<p>
</p>
<h2>Acknowledgments</h2>
<p>Thanks goes to Brian Tarran, editor of the Significance Magazine, for providing additional information about the quantile computation of the <span>Langkjær-Bain (2017)</span> article and for pointing out the Gardiner video.</p>
<h2>References</h2>
<div>
<div>
<p>Dielman, T., C. Lowry, and R. Pfaffenberger. 1994. “A Comparison of Quantile Estimators.” Communications in Statistics - Simulation and Computation 23 (2): 355–71. doi:<a href="https://doi.org/10.1080/03610919408813175">10.1080/03610919408813175</a>.</p>
</div>
<div>
<p>Höhle, J., and M. Höhle. 2009. “Accuracy Assessment of Digital Elevation Models by Means of Robust Statistical Methods.” ISPRS Journal of Photogrammetry and Remote Sensing 64 (4): 398–406. doi:<a href="https://doi.org/10.1016/j.isprsjprs.2009.02.003">10.1016/j.isprsjprs.2009.02.003</a>.</p>
</div>
<div>
<p>Hyndman, R. J., and Y. Fan. 1996. “Sample Quantiles in Statistical Packages.” American Statistician 50 (4): 361–65. doi:<a href="https://doi.org/10.2307/2684934">10.2307/2684934</a>.</p>
</div>
<div>
<p>Langkjær-Bain, Robert. 2017. “The Murky Tale of Flint’s Deceptive Water Data.” Significance 14 (2): 16–21. doi:<a href="https://doi.org/10.1111/j.1740-9713.2017.01016.x">10.1111/j.1740-9713.2017.01016.x</a>.</p>
</div>
</div>