<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/visual-contrast-of-two-robust-regression-methods/"></div></div><div><a data-url="http://www.r-bloggers.com/visual-contrast-of-two-robust-regression-methods/" data-counturl="http://www.r-bloggers.com/visual-contrast-of-two-robust-regression-methods/" data-count="horizontal" data-text="Visual contrast of two robust regression methods" 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://ellisp.github.io/blog/2016/05/22/robust-regression"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Robust regression</h2>
<p>For training purposes, I was looking for a way to illustrate some of the different properties of two different <a target="_blank" href="https://en.wikipedia.org/wiki/Robust_regression">robust estimation methods</a> for linear regression models. The two methods I’m looking at are:</p>
<ul>
<li><a target="_blank" href="https://en.wikipedia.org/wiki/Least_trimmed_squares">least trimmed squares</a>, implemented as the default option in <code>lqs()</code></li>
<li>a <a target="_blank" href="https://en.wikipedia.org/wiki/M-estimator">Huber M-estimator</a>, implemented as the default option in <code>rlm()</code></li>
</ul>
<p>Both functions are in Venables and Ripley’s <code>MASS</code> R package which comes with the standard distribution of R.</p>
<p>These methods are alternatives to ordinary least squares that can provide estimates with superior qualities when the classical assumptions of linear regression aren’t met. M-estimators are effective in dealing with various breakdowns in the classical linear model assumptions, while still giving excellent results if the assumptions happen to be valid after all. Least trimmed squares is very resistant to outliers, at a cost to efficiency. Unfortunately, neither of these (or any other) robust estimation methods is the best in all situations, although they generally out-perform ordinary least squares in most real-life situations (my own, non-systematic observation).</p>
<h2>New Zealand Income Survey 2011</h2>
<p>To explore this I use the simulated unit record file from the New Zealand Income Survey 2011 that I’ve used in a lot of blog posts in the past year. My <a target="_blank" href="http://ellisp.github.io/blog/2015/08/15/importing-nzis-surf/">first post on this dataset</a> sets out how to import this data and tidy it up into a database. The data shows weekly individual income from all sources for New Zealanders aged over 15, as well as hours worked and a range of demographic information I won’t be using today. For looking at robust regression estimation methods, I’m going to pretend that I only ever have a sample of thirty observations from this dataset.</p>
<p>To illustrate the data, here’s an animation showing the full dataset (in grey) and repeated different samples of thirty data points, as well as lines representing linear models fit to those small samples with ordinary least squares (<code>lm</code>) and the two robust regression methods I’m investigating today.<br>
<img alt="animation" src="http://i1.wp.com/ellisp.github.io/img/0041-rtm-lqs.gif?w=456" data-recalc-dims="1"></p>
<p>Some characteristics of this data that make it a useful illustration for robust regression include:</p>
<ul>
<li>It’s reasonable to postulate the underlying relationship between hours worked and income as linear for much of the population. So a linear model on the original scale is likely to be appropriate.</li>
<li>However, the population seems likely to be made up of a number of diverse groups, making assumptions of homogeneity implausible. For example, a large number of people have incomes, some of them quite substantial, despite working zero hours. General economic and social knowledge suggests that divisions into wage-earners, profit-receivers, and social security receivers probably constitute three quite different populations, while still being a significant simplification of the actual heterogeneity.</li>
<li>On the original scale the data are expected to show <a target="_blank" href="https://en.wikipedia.org/wiki/Heteroscedasticity">heteroscedasticity</a> (in this case, higher variance for higher expected income), making classical inference invalid</li>
<li>There are a number of outliers on both the x and y axes; and a cluster of individuals with negative income that appear to form yet another group in the mixture.</li>
</ul>
<h2>Comparing robust methods</h2>
<p>To reflect one of the key features of the data which is the unusual distribution of income when working hours are zero, the models fit in this post are all of the structure:</p>
<p><code>income ~ hours + (hours == 0)</code></p>
<p>The graphics show just the relationship between income and hours for when a non-zero amount of hours is worked. In effect, (just for the graphics), this is like fitting the model after removing all the individuals that performed zero hours of work in the surveyed week.</p>
<p>The animation shown above was the germ idea for this post and already illustrates the key points. In particular, it shows how the randomness associated with a small sample of thirty points leads to a different result for any particular sample; and once you have that sample, different estimation methods can give you materially differing results. In some frames of the animation, we see a sample of thirty where slopes from the different estimation methods point in different directions, suggesting that if that particular small sample was the only one available, one might conclude that working more hours leads to decreased income.</p>
<p>The animation is nice for reminding us of the randomness associated with small samples, but actually isn’t a particularly effective method of looking at how the different robust estimation methods are doing. Here’s a better way of doing that; a plot of the actual regression lines fit to 10,000 different samples of thirty. Each line is semi-transparent, to give a better visual impression of where the bulk of fit lines end up:</p>
<p><img alt="comparison" src="http://i0.wp.com/ellisp.github.io/img/0041-compare-lqs-rlm.png?w=456" data-recalc-dims="1"></p>
<p>This makes it graphically clear that the <code>lqs</code> method (least trimmed squares) gives a noticeably wider range of results than does the Huber M-estimator. This is an expected result; least trimmed squares is highly resistant to outliers, but this comes at the cost of <a target="_blank" href="https://en.wikipedia.org/wiki/Sufficient_statistic">sufficiency</a> (effectively, a significant part of the data has very little impact on the slope, so the information contained in the data is not utilised to the full).</p>
<p>Here’s another way of looking at that; the density plots of the slopes of the three estimation methods (two robust methods plus ordinary least squares fitted with <code>lm</code>). I am using slope because it’s the most likely parameter of interest – it represents the marginal increase in income from an extra hour of work.</p>
<p><img alt="slopes" src="http://ellisp.github.io/img/0041-slope-densities.svg"></p>
<p>The density plot shows that the estimates from <code>lqs</code> tend to be lower than for either <code>rlm</code> or <code>lm</code>. This is to be expected, in a way related to the reason that trimmed mean or median income is usually less than mean income. An estimation method that knocks out of the data the most extreme points (as done by least trimmed squares) will give differing results for samples drawn from skewed distributions.</p>
<p>Here are the mean, median and standard deviation of the slopes estimated by the three methods for the 10,000 samples of thirty:</p>
<div>
<pre><code data-lang="r"><a name="True-1"></a>method  mean median    sd
<a name="True-2"></a>    lm  <span>21.3</span>   <span>21.9</span>  <span>18.9</span>
<a name="True-3"></a>   rlm  <span>20.5</span>   <span>20.7</span>  <span>10.9</span>
<a name="True-4"></a>   lqs  <span>17.7</span>   <span>18.2</span>  <span>23.7</span></code></pre>
</div>
<p>We see that <code>lm</code> and <code>rlm</code> on average give similar results, but <code>rlm</code> is much more stable, with a noticeably lower variance in results (shown by the standard deviation). <code>lqs</code> on the other hand is actually quite unstable, for this small sample size.</p>
<h2>Preliminary thoughts on robust regression estimators</h2>
<p>This post uses Venables and Ripley’s <code>MASS</code> package and the accompanying book <a target="_blank" href="http://www.springer.com/us/book/9780387954578"><em>Modern Applied Statistics with S</em></a>. Their own discussion of the issue implies (to my reading) the preferred use of the MM-estimator proposed by Yohai, Stahel &amp; Zamar in 1991, which retains the outlier-resistance of the S-estimator methods related to trimmed least squares, and the greater efficiency of M-estimators. MM-estimators are available via <code>rlm()</code> with the <code>method = 'MM'</code> argument.</p>
<p>Another book that has been influential for me and I thoroughly recommend is Wilcox’ <a target="_blank" href="http://www.amazon.com/Modern-Statistics-Social-Behavioral-Sciences/dp/1439834563"><em>Modern Statistics for the Social and Behavioral Sciences</em></a>. His comments on the matter are so pertinent that I am going to quote two paragraphs at length:</p>
<h3>Comments on Choosing a Regression Estimator (Wilcox)</h3>
<p>“Many regression estimators have been proposed that can <strong>have substantial advantages over the ordinary least squares estimator. But no single estimator dominates</strong> and there is the practical issue that the choice of estimator can make a practical difference in the conclusions reached. One point worth stressing is that even when an estimator has a high breakdown point, only two outliers, properly placed, can have a large impact on some of the robust estimates of the slopes…. Estimators that seem to be particularly good at avoiding this problem are the Least Trimmed Squares, Least Trimmed Absolute Value and OP regression estimators. Among these three estimators, the OP estimator seems to have an advantage in terms of achieving a relatively low standard error and so has the potential of relatively high power. A negative feature of the OP estimator is that with large sample sizes, execution time might be an issue…</p>
<p>“In terms of achieving a relatively small standard error when dealing with heteroscedasticity, certain robust regression estimators can offer a distinct advantage compared to the least squares estimator…. Ones that perform well include Theil-Sen, the OP regression estimator and the TSTS estimator. The MM-estimator does not perform well for the situations considered (ie heteroscedasticity)… but perhaps there are situations where it offers a practical advantage.”</p>
<p>It should be noted that earlier in the chapter Wilcox pointed out that the MM-estimator “enjoys excellent theoretical properties … but its standard error can be relatively large when there is heteroscedasticity.”</p>
<p>The “OP” estimator is a method of outlier detection and removal followed by <a target="_blank" href="https://en.wikipedia.org/wiki/Theil%E2%80%93Sen_estimator">Theil-Sen estimation</a>, a method that takes the median slope between all the pairs of sample points. Topic for a future post.</p>
<h2>Code</h2>
<p>Here’s the R code behind all this. It won’t be immediately reproducible. Prior work is needed to establish the database as described in my earlier post; and the animation sequence is dependent on a particular folder structure.</p>
<div>
<pre><code data-lang="r"><a name="True-1"></a><span>#===================setup=======================</span>
<a name="True-2"></a><span>library</span><span>(</span>showtext<span>)</span>
<a name="True-3"></a><span>library</span><span>(</span>RMySQL<span>)</span>
<a name="True-4"></a><span>library</span><span>(</span>MASS<span>)</span> 
<a name="True-5"></a><span>library</span><span>(</span>dplyr<span>)</span>
<a name="True-6"></a><span>library</span><span>(</span>tidyr<span>)</span>
<a name="True-7"></a><span>library</span><span>(</span>stringr<span>)</span>
<a name="True-8"></a><span>library</span><span>(</span>ggplot2<span>)</span>
<a name="True-9"></a><span>library</span><span>(</span>scales<span>)</span>
<a name="True-10"></a><span>library</span><span>(</span>directlabels<span>)</span>
<a name="True-11"></a>
<a name="True-12"></a><span>#================download and transform data==============</span>
<a name="True-13"></a><span># See http://ellisp.github.io/blog/2015/08/15/importing-nzis-surf/ for</span>
<a name="True-14"></a><span># instructions on setting up the database</span>
<a name="True-15"></a>
<a name="True-16"></a>sql <span>&lt;-</span> <span>&quot;SELECT hours, income FROM vw_mainheader&quot;</span>
<a name="True-17"></a>PlayPen <span>&lt;-</span> dbConnect<span>(</span>RMySQL<span>::</span>MySQL<span>(),</span> username <span>=</span> <span>&quot;analyst&quot;</span><span>,</span> dbname <span>=</span> <span>&quot;nzis11&quot;</span><span>)</span>
<a name="True-18"></a>nzis <span>&lt;-</span> dbGetQuery<span>(</span>PlayPen<span>,</span> sql<span>)</span> 
<a name="True-19"></a>dbDisconnect<span>(</span>PlayPen<span>)</span>
<a name="True-20"></a>
<a name="True-21"></a><span># An early version of the analysis used a cube root transform; this was dropped</span>
<a name="True-22"></a><span># as it reduces interpretability and is not really germane to the illustration</span>
<a name="True-23"></a><span># of robust regression methods</span>
<a name="True-24"></a><span># cuberoot &lt;- function(x){</span>
<a name="True-25"></a><span># sign(x) * abs(x)^(1/3)</span>
<a name="True-26"></a><span># }</span>
<a name="True-27"></a><span># nzis &lt;- nzis %>%</span>
<a name="True-28"></a><span># mutate(income2 = cuberoot(income),</span>
<a name="True-29"></a><span># hours2 = cuberoot(hours))</span>
<a name="True-30"></a>
<a name="True-31"></a><span># create the variables to be used for x and y (as finally used, this</span>
<a name="True-32"></a><span># is just income and hours on their original scale):</span>
<a name="True-33"></a>nzis <span>&lt;-</span> nzis <span>%>%</span>
<a name="True-34"></a>   mutate<span>(</span>income2 <span>=</span> income<span>,</span>
<a name="True-35"></a>          hours2 <span>=</span> hours<span>)</span>
<a name="True-36"></a>
<a name="True-37"></a><span>#==================simulations of many samples and estimated models==============</span>
<a name="True-38"></a><span># create empty lists to hold the samples and the model results</span>
<a name="True-39"></a>points_samp <span>&lt;-</span> <span>list</span><span>()</span>
<a name="True-40"></a>mod_lm <span>&lt;-</span> <span>list</span><span>()</span>
<a name="True-41"></a>mod_rlm <span>&lt;-</span> <span>list</span><span>()</span>
<a name="True-42"></a>mod_lqs <span>&lt;-</span> <span>list</span><span>()</span>
<a name="True-43"></a>
<a name="True-44"></a><span># create the samples, estimate the models and store the results</span>
<a name="True-45"></a>reps <span>&lt;-</span> <span>10000</span>
<a name="True-46"></a><span>for</span> <span>(</span>i <span>in</span> <span>1</span><span>:</span>reps<span>){</span>
<a name="True-47"></a>   nzis_samp <span>&lt;-</span> nzis<span>[</span><span>sample</span><span>(</span><span>1</span><span>:</span><span>nrow</span><span>(</span>nzis<span>),</span> <span>30</span><span>),</span> <span>]</span>   
<a name="True-48"></a>   points_samp<span>[[</span>i<span>]]</span> <span>&lt;-</span> nzis_samp
<a name="True-49"></a>   mod_lm<span>[[</span>i<span>]]</span> <span>&lt;-</span> lm<span>(</span>income2 <span>~</span> hours2 <span>+</span> <span>(</span>hours2 <span>==</span> <span>0</span><span>),</span> data <span>=</span> nzis_samp<span>)</span>
<a name="True-50"></a>   mod_rlm<span>[[</span>i<span>]]</span> <span>&lt;-</span> rlm<span>(</span>income2 <span>~</span> hours2 <span>+</span> <span>(</span>hours2 <span>==</span> <span>0</span><span>),</span> data <span>=</span> nzis_samp<span>)</span>
<a name="True-51"></a>   mod_lqs<span>[[</span>i<span>]]</span> <span>&lt;-</span> lqs<span>(</span>income2 <span>~</span> hours2 <span>+</span> <span>(</span>hours2 <span>==</span> <span>0</span><span>),</span> data <span>=</span> nzis_samp<span>)</span>
<a name="True-52"></a>   
<a name="True-53"></a><span>}</span>
<a name="True-54"></a>
<a name="True-55"></a><span>#======================reporting results==============</span>
<a name="True-56"></a>
<a name="True-57"></a><span>#------------setup-----------------</span>
<a name="True-58"></a><span># Fonts:</span>
<a name="True-59"></a>font.add.google<span>(</span><span>&quot;Poppins&quot;</span><span>,</span> <span>&quot;myfont&quot;</span><span>)</span>
<a name="True-60"></a>showtext.auto<span>()</span>
<a name="True-61"></a>theme_set<span>(</span>theme_light<span>(</span>base_family <span>=</span> <span>&quot;myfont&quot;</span><span>))</span>
<a name="True-62"></a>
<a name="True-63"></a>
<a name="True-64"></a>
<a name="True-65"></a><span># define a generic plot function that does the background pale grey</span>
<a name="True-66"></a><span># plot of the full dataset and draws the axes:</span>
<a name="True-67"></a>draw_plot <span>&lt;-</span> <span>function</span><span>(){</span>
<a name="True-68"></a>   <span>with</span><span>(</span>nzis<span>,</span> plot<span>(</span>hours2<span>,</span> income2<span>,</span> cex <span>=</span> <span>0.5</span><span>,</span> col <span>=</span> <span>&quot;grey80&quot;</span><span>,</span> bty <span>=</span> <span>&quot;l&quot;</span><span>,</span>
<a name="True-69"></a>                   xlab <span>=</span> <span>&quot;Hours worked&quot;</span><span>,</span> 
<a name="True-70"></a>                   ylab <span>=</span> <span>&quot;Weekly income&quot;</span><span>,</span>
<a name="True-71"></a>                   axes <span>=</span> <span>FALSE</span><span>))</span>
<a name="True-72"></a>   
<a name="True-73"></a>   axis<span>(</span><span>2</span><span>,</span> at <span>=</span> axTicks<span>(</span><span>2</span><span>),</span> labels <span>=</span> <span>paste0</span><span>(</span><span>&quot;$&quot;</span><span>,</span> str_trim<span>(</span><span>format</span><span>(</span>
<a name="True-74"></a>      axTicks<span>(</span><span>2</span><span>),</span> big.mark <span>=</span> <span>&quot;,&quot;</span><span>))))</span>
<a name="True-75"></a>   axis<span>(</span><span>1</span><span>,</span> at <span>=</span> axTicks<span>(</span><span>1</span><span>),</span> labels <span>=</span> axTicks<span>(</span><span>1</span><span>))</span>
<a name="True-76"></a><span>}</span>
<a name="True-77"></a>
<a name="True-78"></a>
<a name="True-79"></a>
<a name="True-80"></a><span>#-----------animation-------------</span>
<a name="True-81"></a><span># This code is dependent on the particular folder structure. File locations</span>
<a name="True-82"></a><span># will need to be changed if people want to reproduce the analysis.</span>
<a name="True-83"></a><span>for</span><span>(</span>i <span>in</span> <span>1</span><span>:</span><span>100</span><span>){</span>
<a name="True-84"></a>   
<a name="True-85"></a>   png<span>(</span><span>paste0</span><span>(</span><span>&quot;_output/0041-rlm-lqs/&quot;</span><span>,</span> i <span>+</span> <span>20000</span><span>,</span> <span>&quot;.png&quot;</span><span>),</span>
<a name="True-86"></a>       <span>650</span><span>,</span> <span>650</span><span>,</span> res <span>=</span> <span>100</span><span>)</span>
<a name="True-87"></a>   par<span>(</span>family <span>=</span> <span>&quot;myfont&quot;</span><span>)</span>
<a name="True-88"></a>   draw_plot<span>()</span>
<a name="True-89"></a>   title<span>(</span>main <span>=</span> <span>&quot;Comparison of different estimation methodsnfor linear models on a subset of income data&quot;</span><span>)</span>
<a name="True-90"></a>   <span>with</span><span>(</span>points_samp<span>[[</span>i<span>]],</span> points<span>(</span>hours2<span>,</span> income2<span>,</span> cex <span>=</span> <span>2</span><span>))</span>
<a name="True-91"></a>   abline<span>(</span>mod_lm<span>[[</span>i<span>]],</span> col <span>=</span> <span>&quot;black&quot;</span><span>)</span>
<a name="True-92"></a>   abline<span>(</span>mod_rlm<span>[[</span>i<span>]],</span> col <span>=</span> <span>&quot;blue&quot;</span><span>)</span>
<a name="True-93"></a>   abline<span>(</span>mod_lqs<span>[[</span>i<span>]],</span> col <span>=</span> <span>&quot;red&quot;</span><span>)</span>
<a name="True-94"></a>   
<a name="True-95"></a>   legend<span>(</span><span>&quot;topright&quot;</span><span>,</span> legend <span>=</span> <span>c</span><span>(</span><span>&quot;lm&quot;</span><span>,</span> <span>&quot;rlm&quot;</span><span>,</span> <span>&quot;lts&quot;</span><span>),</span> lty <span>=</span> <span>1</span><span>,</span> 
<a name="True-96"></a>          col <span>=</span> <span>c</span><span>(</span><span>&quot;black&quot;</span><span>,</span> <span>&quot;blue&quot;</span><span>,</span> <span>&quot;red&quot;</span><span>),</span> text.col <span>=</span> <span>c</span><span>(</span><span>&quot;black&quot;</span><span>,</span> <span>&quot;blue&quot;</span><span>,</span> <span>&quot;red&quot;</span><span>),</span>
<a name="True-97"></a>          bty <span>=</span> <span>&quot;n&quot;</span><span>)</span>
<a name="True-98"></a>   
<a name="True-99"></a>   legend<span>(</span><span>&quot;bottomright&quot;</span><span>,</span> legend <span>=</span> <span>c</span><span>(</span><span>&quot;Full data&quot;</span><span>,</span> <span>&quot;Sampled data&quot;</span><span>),</span>
<a name="True-100"></a>          pch <span>=</span> <span>1</span><span>,</span> col <span>=</span> <span>c</span><span>(</span><span>&quot;grey50&quot;</span><span>,</span> <span>&quot;black&quot;</span><span>),</span> pt.cex <span>=</span> <span>1</span><span>:</span><span>2</span><span>,</span> 
<a name="True-101"></a>          bty <span>=</span> <span>&quot;n&quot;</span><span>,</span> text.col <span>=</span> <span>c</span><span>(</span><span>&quot;grey50&quot;</span><span>,</span> <span>&quot;black&quot;</span><span>))</span>
<a name="True-102"></a>   
<a name="True-103"></a>   dev.off<span>()</span>
<a name="True-104"></a><span>}</span>
<a name="True-105"></a>
<a name="True-106"></a><span># next sequence turns the various static frames into an animated GIF</span>
<a name="True-107"></a><span># and depends on the actual location of ImageMagick:</span>
<a name="True-108"></a>projdir <span>&lt;-</span> <span>setwd</span><span>(</span><span>&quot;_output/0041-rlm-lqs/&quot;</span><span>)</span>
<a name="True-109"></a><span>system</span><span>(</span><span>'&quot;C:\Program Files\ImageMagick-6.9.1-Q16\convert&quot; -loop 0 -delay 65 *.png &quot;../../..http://ellisp.github.io/img/0041-rtm-lqs.gif&quot;'</span><span>)</span> 
<a name="True-110"></a><span>setwd</span><span>(</span>projdir<span>)</span>
<a name="True-111"></a>
<a name="True-112"></a>
<a name="True-113"></a><span>#--------------visual static comparison-----------------</span>
<a name="True-114"></a>par<span>(</span>mfrow <span>=</span> <span>c</span><span>(</span><span>1</span><span>,</span> <span>2</span><span>),</span> family <span>=</span> <span>&quot;myfont&quot;</span><span>)</span>
<a name="True-115"></a>
<a name="True-116"></a><span># draw graph for lqs</span>
<a name="True-117"></a>draw_plot<span>()</span>
<a name="True-118"></a>title<span>(</span>main <span>=</span> <span>&quot;Least trimmed squares&quot;</span><span>)</span>
<a name="True-119"></a>
<a name="True-120"></a>
<a name="True-121"></a><span>for</span><span>(</span>i <span>in</span> <span>1</span><span>:</span>reps<span>){</span>
<a name="True-122"></a>   abline<span>(</span>mod_lqs<span>[[</span>i<span>]],</span> col <span>=</span> adjustcolor<span>(</span><span>&quot;red&quot;</span><span>,</span> alpha.f <span>=</span> <span>0.02</span><span>))</span>
<a name="True-123"></a><span>}</span>
<a name="True-124"></a>   
<a name="True-125"></a><span># draw graph for rlm</span>
<a name="True-126"></a>draw_plot<span>()</span>
<a name="True-127"></a>title<span>(</span>main <span>=</span> <span>&quot;M-estimator&quot;</span><span>)</span>
<a name="True-128"></a><span>for</span><span>(</span>i <span>in</span> <span>1</span><span>:</span>reps<span>){</span>
<a name="True-129"></a>   abline<span>(</span>mod_rlm<span>[[</span>i<span>]],</span> col <span>=</span> adjustcolor<span>(</span><span>&quot;blue&quot;</span><span>,</span> alpha.f <span>=</span> <span>0.02</span><span>))</span>
<a name="True-130"></a><span>}</span>
<a name="True-131"></a>
<a name="True-132"></a>
<a name="True-133"></a><span>#-----------------compare estimated slopes-------------------</span>
<a name="True-134"></a>slopes <span>&lt;-</span> <span>data.frame</span><span>(</span>
<a name="True-135"></a>   lm <span>=</span> <span>unlist</span><span>(</span><span>lapply</span><span>(</span>mod_lm<span>,</span> <span>function</span><span>(</span>x<span>){</span>coef<span>(</span>x<span>)[</span><span>2</span><span>]})),</span>
<a name="True-136"></a>   rlm <span>=</span> <span>unlist</span><span>(</span><span>lapply</span><span>(</span>mod_rlm<span>,</span> <span>function</span><span>(</span>x<span>){</span>coef<span>(</span>x<span>)[</span><span>2</span><span>]})),</span>
<a name="True-137"></a>   lqs <span>=</span> <span>unlist</span><span>(</span><span>lapply</span><span>(</span>mod_lqs<span>,</span> <span>function</span><span>(</span>x<span>){</span>coef<span>(</span>x<span>)[</span><span>2</span><span>]}))</span>
<a name="True-138"></a><span>)</span> <span>%>%</span>
<a name="True-139"></a>   gather<span>(</span>method<span>,</span> value<span>)</span>
<a name="True-140"></a>
<a name="True-141"></a><span># summary table:</span>
<a name="True-142"></a>slopes <span>%>%</span>
<a name="True-143"></a>   mutate<span>(</span>method <span>=</span> <span>factor</span><span>(</span>method<span>,</span> levels <span>=</span> <span>c</span><span>(</span><span>&quot;lm&quot;</span><span>,</span> <span>&quot;rlm&quot;</span><span>,</span> <span>&quot;lqs&quot;</span><span>)))</span> <span>%>%</span>
<a name="True-144"></a>   group_by<span>(</span>method<span>)</span> <span>%>%</span>
<a name="True-145"></a>   summarise<span>(</span>
<a name="True-146"></a>      mean <span>=</span> <span>round</span><span>(</span><span>mean</span><span>(</span>value<span>),</span> <span>1</span><span>),</span>
<a name="True-147"></a>      median <span>=</span> <span>round</span><span>(</span>median<span>(</span>value<span>),</span> <span>1</span><span>),</span>
<a name="True-148"></a>      sd <span>=</span> <span>round</span><span>(</span>sd<span>(</span>value<span>),</span> <span>1</span><span>)</span>
<a name="True-149"></a>   <span>)</span>
<a name="True-150"></a>
<a name="True-151"></a>   
<a name="True-152"></a><span># density plot, taking care to use same colours for each method</span>
<a name="True-153"></a><span># as in previous graphics:</span>
<a name="True-154"></a>colours <span>&lt;-</span> <span>c</span><span>(</span><span>&quot;lm&quot;</span> <span>=</span> <span>&quot;black&quot;</span><span>,</span> <span>&quot;rlm&quot;</span> <span>=</span> <span>&quot;blue&quot;</span><span>,</span> <span>&quot;lqs&quot;</span> <span>=</span> <span>&quot;red&quot;</span><span>)</span>
<a name="True-155"></a><span>print</span><span>(</span>direct.label<span>(</span>
<a name="True-156"></a>ggplot<span>(</span>slopes<span>,</span> aes<span>(</span>x <span>=</span> value<span>,</span> fill <span>=</span> method<span>,</span> colour <span>=</span> method<span>))</span> <span>+</span>
<a name="True-157"></a>   geom_density<span>(</span>alpha <span>=</span> <span>0.3</span><span>)</span> <span>+</span>
<a name="True-158"></a>   coord_cartesian<span>(</span>xlim <span>=</span> <span>c</span><span>(</span><span>-25</span><span>,</span> <span>75</span><span>))</span> <span>+</span>
<a name="True-159"></a>   scale_x_continuous<span>(</span><span>&quot;Estimated marginal return of extra hour of work&quot;</span><span>,</span>
<a name="True-160"></a>                      label <span>=</span> dollar<span>)</span> <span>+</span>
<a name="True-161"></a>   labs<span>(</span>y <span>=</span> <span>&quot;Density of estimates fromnsamples of 30 each&quot;</span><span>)</span> <span>+</span>
<a name="True-162"></a>   scale_fill_manual<span>(</span>values <span>=</span> colours<span>)</span> <span>+</span>
<a name="True-163"></a>   scale_colour_manual<span>(</span>values <span>=</span> colours<span>)</span>
<a name="True-164"></a><span>))</span></code></pre>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/visual-contrast-of-two-robust-regression-methods/"></div></div><div><a data-url="http://www.r-bloggers.com/visual-contrast-of-two-robust-regression-methods/" data-counturl="http://www.r-bloggers.com/visual-contrast-of-two-robust-regression-methods/" data-count="vertical" data-text="Visual contrast of two robust regression methods" 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://ellisp.github.io/blog/2016/05/22/robust-regression"> Peter's stats stuff - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Fq3oDR83fXw"></a></div>