<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/my-criticism-of-r-numeric-summary/"></div></div><div><a data-url="https://www.r-bloggers.com/my-criticism-of-r-numeric-summary/" data-counturl="https://www.r-bloggers.com/my-criticism-of-r-numeric-summary/" data-count="horizontal" data-text="My criticism of R numeric summary" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2016/08/my-criticism-of-r-numeric-summary/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>My criticism of <a target="_blank" href="https://cran.r-project.org/">R</a>‘s numeric <code>summary()</code> method is: it unfaithful to numeric arguments (due to bad default behavior) and frankly it should be considered unreliable. It is likely the way it is for historic and compatibility reasons, but in my opinion it does not currently represent a desirable set of tradeoffs. <code>summary()</code> likely represents good work by high-ability researchers, and the sharp edges are due to historically necessary trade-offs.</p>
<p><center><br>
<img title="e53d7f8067067a51029cde8260094ff5867b10ab6676b1d493c8dd8d23c4571b.jpg" alt="E53d7f8067067a51029cde8260094ff5867b10ab6676b1d493c8dd8d23c4571b" src="http://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2016/08/e53d7f8067067a51029cde8260094ff5867b10ab6676b1d493c8dd8d23c4571b.jpg?w=660" border="0" data-recalc-dims="1">
<p>The Big Lebowski, 1998.<br>
</center></p>
<p>Please read on for some context and my criticism.<span></span></p>
<h2>Introduction</h2>
<p>My group has been doing a lot more <a target="_blank" href="http://www.win-vector.com/site/service-offerings/training-overview/">professional training</a> lately. This is interesting because bright students really put a lot of interesting demands on how you organize and communicate. They want things that make sense (so they can learn them), that are powerful (so it is worth learning them), and that are <em>regular</em> (so they can compose them and move beyond what you are teaching). Students are less sympathetic to implementation history and unstated conventions, as new users tend not to benefit from them. Remember a new <code>R</code> student is still deciding if they want to use <code>R</code>, to them it is new so an instructor needs to defend <code>R</code>‘s current trade-offs (not its evolutionary path). We find it is best to point out both what is great in <code>R</code> and what isn’t great (versus skipping such, or worse trying to justify such portions).</p>
<p>Please keep this in mind when I demonstrate what goes wrong when one attempts to teach R’s <code>summary()</code> function to the laity.</p>
<h2>The Issue</h2>
<p>Suppose you had a list or vector of numbers in R. It would be useful to be able to produce and view some summaries or statistics about these numbers. The primary way to do this in R is to call the <code>summary()</code> method. Here is an example below:</p>
<p><code>
<pre>
numbers &lt;- 1:7
print(numbers)
##  [1]  1  2  3  4  5  6  7

summary(numbers)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     1.0     2.5     4.0     4.0     5.5     7.0 
</pre>
<p></p>
<p>From the names attached to the results you can get the meanings and move on. But the whole time you are hoping none of your students call <code>summary()</code> on a single number. Because if the do, they have a <em>very good</em> chance of seeing <code>summary()</code> fail. And now you have broken trust in <code>R</code>.</p>
<p>Let’s tack into the wind and demonstrate the failure:</p>
<p><code>
<pre>
summary(15555)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   15560   15560   15560   15560   15560   15560 
</pre>
<p></p>
<p><code>summary()</code> is claiming the minimum value from the set of numbers <code>c(15555)</code> is <code>15560</code>. Now this is a deliberately trivial example where we can see what is going on (it sure looks like presentation rounding). To make matters worse, this isn’t just confusion generated during presentation- the actual values are wrong.</p>
<p><code>
<pre>
str(summary(15555))
## Classes 'summaryDefault', 'table'  Named num [1:6] 15560 15560 15560 15560 15560 ...
##   ..- attr(*, &quot;names&quot;)= chr [1:6] &quot;Min.&quot; &quot;1st Qu.&quot; &quot;Median&quot; &quot;Mean&quot; ...

summary(15555)[['Min.']] == min(15555)
## [1] FALSE
</pre>
<p></p>
<p>It may seem silly to expect the slots from a <code>summary()</code> call on a vector would be used in calculation (when we have direct functions such as <code>quantile()</code> and <code>mean()</code> for getting the same results), but using values from summaries of models is standard practice in R. The trivial linear model summary <code>summary(lm(y~0,data.frame(y=15555)))</code> shows rounded results (though it appears to hold accurate results, and only round during presentation; use <code>unclass()</code> to inspect the actual values).</p>
<h2>Why it Matters</h2>
<p>This <em>is</em> in fact a problem. You can say this is a consequence of the “default settings of <code>summary()</code>” and it is my fault for not changing those settings. But frankly it is quite fair to expect the default settings to be safe and sane. </p>
<p>Let us also appeal to authority:</p>
<blockquote><p>The many computational steps between original data source and displayed results must all be truthful, or the effect of the analysis may be worthless, if not pernicious. This places an obligation on all creators of software to program in such a way that the computations can be understood and trusted. This obligation I label the <em>Prime Directive</em>.</p>
<p>John Chambers, <em>Software for Data Analysis: Programming with R</em>, Springer 2008.
</blockquote>
<p>The point is you are delegating work to your system. If it needlessly fails (no matter how trivially) when observed, how can you trust it when unobserved? John Chambers’ point is that trust is very expensive to build up, so you really don’t want to squander it.</p>
<p>I used to try to “lecture this away” as just being “rounding in the presentation for neatness.” But this runs into two objections:</p>
<ul>
<li>Why doesn’t the presentation hint at this by switching to scientific notation such as <code>1.556e+4</code>?</li>
<li>If <code>summary()</code> “is just presentation” wouldn’t it be a string?</li>
</ul>
<p>We are losing substitutability. We would love to be able to say to students that “<code>summary()</code> is a convenient shorthand and you can treat the following as equivalent”:</p>
<ul>
<li><code>summary(x)[['Min.']] == min(x)</code></li>
<li><code>summary(x)[['1st Qu.']] == quantile(x,0.25)</code></li>
<li><code>summary(x)[['Median']] == median(x)</code></li>
<li><code>summary(x)[['Mean']] == mean(x)</code></li>
<li><code>summary(x)[['3rd Qu.']] == quantile(x,0.75)</code></li>
<li><code>summary(x)[['Max.']] == max(x)</code></li>
</ul>
<p>But the above isn’t always the case. What we would like is for <code>summary()</code> to contain these values and get pretty printing by using the S3 or S4 object system to override the <code>print()</code> method. It is quite likely <code>summary()</code> predates these object systems, so achieved pretty printing through rounding of values.</p>
<h2>What is going on?</h2>
<p>We can take a look at the actual code and see what is happening. We are looking for a reason, not an excuse.</p>
<p>From <code>help(summary)</code> we see summary takes a <code>digits</code> option with default value <code>digits = max(3, getOption(&quot;digits&quot;)-3)</code> (lets not even get into why setting <code>digits</code> directly does one thing and the system default is shifted by <code>3</code>). <code>getOption(&quot;digits&quot;)</code> returns <code>7</code> on my machine so we see we are asking for four digit rounding, which is consistent with what we saw. Digging through the dispatch rules we can eventually determine that for a numeric vector <code>summary()</code> eventually calls <code>summary.default()</code>. By calling <code>print(summary.default)</code> we can look at the code. The offending snippet is:</p>
<p><code>
<pre>
        qq &lt;- stats::quantile(object)
        qq &lt;- signif(c(qq[1L:3L], mean(object), qq[4L:5L]), digits)
</pre>
<p></p>
<p>After computing the quantiles summary then calls <code>signif()</code> to round the results. <code>R</code> isn’t inaccurate, it just went out of its way to round the results.</p>
<h2>Why is this whiny rant so long?</h2>
<p>One reason this article is long is the behavior we are describing breaks expectations. So we end up having to document what is actually going on (a laborious process) instead of being able to rely on shared educated expectations. The whining is where actualities and expectations diverge.</p>
<h2>Conclusion</h2>
<p><code>summary()</code> attempts to achieve neatness and legibility. This is a laudable goal, if achievable. Numeric analysis is not so simple that rounding could safely achieve such a goal.</p>
<p>It is well known that rounding is not a safe or faithful operation (it loses information, and can be catastrophic if naively applied in many stages of a complex calculation). Because it is obvious rounding is dangerous, sophisticated students are surprised that it defaults to “on” in common calculations without indication or warning (such as moving to scientific notation). <code>summary()</code> compounds this error by returning rounded values (instead of rounding only at <code>print</code>/presentation). As <code>summary()</code> is often a first view of data (along with <code>print()</code>) we encounter confusing inconsistent situations where un-rounded values (presentation of original data) and rounded values are compared.</p>
<p>Of course, we can (and should) teach students to call <code>mean(x)</code> and <code>quantile(x)</code> rather than <code>summary(x)</code> when they want to reuse the summary statistics. But then we have to explain <em>why</em>. After seeing something like this it becomes an unfortunate additional teaching goal to convince students that more of <code>R</code> doesn’t behave like <code>summary()</code>.</p>

<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/my-criticism-of-r-numeric-summary/"></div></div><div><a data-url="https://www.r-bloggers.com/my-criticism-of-r-numeric-summary/" data-counturl="https://www.r-bloggers.com/my-criticism-of-r-numeric-summary/" data-count="vertical" data-text="My criticism of R numeric summary" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>

<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.win-vector.com/blog/2016/08/my-criticism-of-r-numeric-summary/"> R – Win-Vector Blog</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ufEKCtn4IaM"></a></div></code></code></code></code>