<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/the-bayesian-approach-to-ridge-regression/"></div></div><div><a data-url="https://www.r-bloggers.com/the-bayesian-approach-to-ridge-regression/" data-counturl="https://www.r-bloggers.com/the-bayesian-approach-to-ridge-regression/" data-count="horizontal" data-text="The Bayesian approach to ridge regression" 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.onthelambda.com/2016/10/30/the-bayesian-approach-to-ridge-regression/"> R – On the lambda</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In a <a target="_blank" rel="nofollow" href="http://www.onthelambda.com/2015/08/19/kickin-it-with-elastic-net-regression/">previous post</a>, we demonstrated that ridge regression (a form of regularized linear regression that attempts to shrink the beta coefficients toward zero) can be super-effective at combating overfitting and lead to a greatly more generalizable model. This approach to regularization used penalized maximum likelihood estimation (for which we used the amazing <code>glmnet</code> package). There is, however, another approach… an equivalent approach… but one that allows us greater flexibility in model construction and lends itself more easily to an intuitive interpretation of the uncertainty of our beta coefficient estimates. I’m speaking, of course, of the bayesian approach. </p>
<p>As it turns out, careful selection of the type and shape of our prior distributions with respect to the coefficients can mimic different types of frequentist linear model regularization. For ridge regression, we use normal priors of varying width.</p>
<p>Though it can be shown analytically that shifting the width of normal priors on the beta coefficients is equivalent to L2 penalized maximum likelihood estimation, the math is scary and hard to follow. In this post, we are going to be taking a computational approach to demonstrating the equivalence of the bayesian approach and ridge regression.</p>
<p>This post is going to be a part of a multi-post series investigating other bayesian approaches to linear model regularization including lasso regression facsimiles and hybrid approaches.</p>
<h3>mtcars</h3>
<p>We are going to be using the venerable <code>mtcars</code> dataset for this demonstration because (a) it’s multicollinearity and high number of potential predictors relative to its sample size lends itself fairly well to ridge regression, and (b) we used it in the <a target="_blank" rel="nofollow" href="http://www.onthelambda.com/2015/08/19/kickin-it-with-elastic-net-regression/">elastic net blog post</a> <img alt="🙂" class="wp-smiley" src="https://s.w.org/images/core/emoji/2/72x72/1f642.png"></p>
<p>Before, you lose interest… here! have a figure! An explanation will follow.</p>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/www.onthelambda.com/wp-content/uploads/2016/10/mtcars-loocv-mse.png"><img sizes="(max-width: 650px) 100vw, 650px" alt="mtcars-loocv-mse" class="wp-image-749" src="https://i2.wp.com/www.onthelambda.com/wp-content/uploads/2016/10/mtcars-loocv-mse-1024x884.png?resize=456%2C394" data-recalc-dims="1"></a></p>
<p>After scaling the predictor variables to be 0-centered and have a standard deviation of 1, I described a model predicting <code>mpg</code> using all available predictors and placed normal priors on the beta coefficients with a standard deviation for each value from 0.05 to 5 (by 0.025). To fit the model, instead of MCMC estimation via JAGS or Stan, I used quadratic approximation performed by the awesome <code>rethinking</code> package written by Richard McElreath written for his excellent book, <a target="_blank" rel="nofollow" href="http://xcelab.net/rm/statistical-rethinking/">Statistical Rethinking</a>. Quadratic approximation uses an optimization algorithm to find the maximum a priori (MAP) point of the posterior distribution and approximates the rest of the posterior with a normal distribution about the MAP estimate. I use this method chiefly because as long as it took to run these simulations using quadratic approximation, it would have taken many orders of magnitude longer to use MCMC. Various spot checks confirmed that the quadratic approximation was comparable to the posterior as told by Stan.</p>
<p>As you can see from the figure, as the prior on the coefficients gets tighter, the model performance (as measured by the leave-one-out cross-validated mean squared error) improves—at least until the priors become too strong to be influenced sufficiently by the evidence. The ribbon about the MSE is the 95% credible interval (using a normal likelihood). I know, I know… it’s pretty damn wide.</p>
<p>The dashed vertical line is at the prior width that minimizes the LOOCV MSE. The minimum MSE is, for all practical purposes, <i>identical</i> to that of the highest performing ridge regression model using <code>glmnet</code>. This is good.</p>
<p>Another really fun thing to do with the results is to visualize the movement of the beta coefficient estimates and different penalties. The figure below depicts this. Again, the dashed vertical line is the highest performing prior width.</p>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/www.onthelambda.com/wp-content/uploads/2016/10/mtcars-coef-shrinkage.png"><img sizes="(max-width: 650px) 100vw, 650px" alt="mtcars-coef-shrinkage" class="wp-image-750" src="https://i1.wp.com/www.onthelambda.com/wp-content/uploads/2016/10/mtcars-coef-shrinkage-1024x884.png?resize=456%2C394" data-recalc-dims="1"></a></p>
<p>One last thing: we’ve heretofore only demonstrated that the bayesian approach can perform as well as the L2 penalized MLE… but it’s conceivable that it achieves this by finding a completely different coefficient vector. The figure below shows the same figure as above but I overlaid the coefficient estimates (for each predictor) of the top-performing <code>glmnet</code> model. These are shown as the dashed colored horizontal lines.</p>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/www.onthelambda.com/wp-content/uploads/2016/10/mtcars-coef-shrinkage-net-overlay.png"><img sizes="(max-width: 650px) 100vw, 650px" alt="mtcars-coef-shrinkage-net-overlay" class="wp-image-747" src="https://i2.wp.com/www.onthelambda.com/wp-content/uploads/2016/10/mtcars-coef-shrinkage-net-overlay-1024x884.png?resize=456%2C394" data-recalc-dims="1"></a></p>
<p>These results are pretty exciting! (if you’re the type to not get invited to parties). Notice that, at the highest performing prior width, the coefficients of the bayesian approach and the <code>glmnet</code> approach are virtually identical.</p>
<p>Sooooo, not only did the bayesian variety produce an equivalently generalizable model (as evinced by equivalent cross-validated MSEs) but also yielded a vector of beta coefficient estimates nearly identical to those estimated by <code>glmnet</code>. This suggests that both the bayesian approach and <code>glmnet</code>‘s approach, using different methods, regularize the model via the same underlying mechanism.</p>
<p>A drawback of the bayesian approach is that its solution takes many orders of magnitude more time to arrive at. Two advantages of the Bayesian approach are (a) the ability to study the posterior distributions of the coefficient estimates and ease of interpretation that they allows, and (b) the enhanced flexibility in model design and the ease by which you can, for example, swap out likelihood functions or construct more complicated hierarchal models.</p>
<p>If you are even the least bit interested in this, I urge you to look <a target="_blank" rel="nofollow" href="https://github.com/tonyfischetti/bayesian-regularization/blob/master/l2-regularization/bayes-reg.R">at the code</a> (in <a target="_blank" rel="nofollow" href="https://github.com/tonyfischetti/bayesian-regularization">this git repository</a>) because (a) I worked really hard on it and, (b) it demonstrates cool use of meta-programming, parallelization, and progress bars… if I do say so myself <img alt="🙂" class="wp-smiley" src="https://s.w.org/images/core/emoji/2/72x72/1f642.png"></p>
<p>share this: <a title="Share on Facebook" data-provider="facebook" target="_blank" rel="nofollow" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.onthelambda.com%2F2016%2F10%2F30%2Fthe-bayesian-approach-to-ridge-regression%2F&t=The%20Bayesian%20approach%20to%20ridge%20regression&%23038;s=100&%23038;p&%23091;url&%23093;=http%3A%2F%2Fwww.onthelambda.com%2F2016%2F10%2F30%2Fthe-bayesian-approach-to-ridge-regression%2F&%23038;p&%23091;images&%23093;&%23091;0&%23093;=http%3A%2F%2Fwww.onthelambda.com%2Fwp-content%2Fuploads%2F2016%2F10%2Fmtcars-coef-shrinkage-net-overlay.png&%23038;p&%23091;title&%23093;=The%20Bayesian%20approach%20to%20ridge%20regression"><img title="Share on Facebook" alt="Facebook" src="https://i1.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/facebook.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share on Twitter" data-provider="twitter" target="_blank" rel="nofollow" href="https://twitter.com/intent/tweet?url=http%3A%2F%2Fwww.onthelambda.com%2F2016%2F10%2F30%2Fthe-bayesian-approach-to-ridge-regression%2F&text=Hey%20check%20this%20out"><img title="Share on Twitter" alt="twitter" src="https://i2.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/twitter.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share on Google+" data-provider="google_plus" target="_blank" rel="nofollow" href="https://plus.google.com/share?url=http%3A%2F%2Fwww.onthelambda.com%2F2016%2F10%2F30%2Fthe-bayesian-approach-to-ridge-regression%2F"><img title="Share on Google+" alt="google_plus" src="https://i0.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/google_plus.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share on Reddit" data-provider="reddit" target="_blank" rel="nofollow" href="http://www.reddit.com/submit?url=http%3A%2F%2Fwww.onthelambda.com%2F2016%2F10%2F30%2Fthe-bayesian-approach-to-ridge-regression%2F&title=The%20Bayesian%20approach%20to%20ridge%20regression"><img title="Share on Reddit" alt="reddit" src="https://i1.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/reddit.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Pin it with Pinterest" data-provider="pinterest" target="_blank" rel="nofollow" href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.onthelambda.com%2F2016%2F10%2F30%2Fthe-bayesian-approach-to-ridge-regression%2F&media=http%3A%2F%2Fwww.onthelambda.com%2Fwp-content%2Fuploads%2F2016%2F10%2Fmtcars-coef-shrinkage-net-overlay.png&%23038;description=The%20Bayesian%20approach%20to%20ridge%20regression"><img title="Pin it with Pinterest" alt="pinterest" src="https://i1.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/pinterest.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share on Linkedin" data-provider="linkedin" target="_blank" rel="nofollow" href="http://www.linkedin.com/shareArticle?mini=true&url=http%3A%2F%2Fwww.onthelambda.com%2F2016%2F10%2F30%2Fthe-bayesian-approach-to-ridge-regression%2F&%23038;title=The%20Bayesian%20approach%20to%20ridge%20regression"><img title="Share on Linkedin" alt="linkedin" src="https://i0.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/linkedin.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share on tumblr" data-provider="tumblr" target="_blank" rel="nofollow" href="http://tumblr.com/share?s=&v=3&%23038;t=The%20Bayesian%20approach%20to%20ridge%20regression&%23038;u=http%3A%2F%2Fwww.onthelambda.com%2F2016%2F10%2F30%2Fthe-bayesian-approach-to-ridge-regression%2F"><img title="Share on tumblr" alt="tumblr" src="https://i0.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/tumblr.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share by email" data-provider="mail" target="_blank" rel="nofollow" href="mailto:?subject=The%20Bayesian%20approach%20to%20ridge%20regression&body=Hey%20check%20this%20out:%20http%3A%2F%2Fwww.onthelambda.com%2F2016%2F10%2F30%2Fthe-bayesian-approach-to-ridge-regression%2F"><img title="Share by email" alt="mail" src="https://i1.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/mail.png?resize=16%2C16" data-recalc-dims="1"></a></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/the-bayesian-approach-to-ridge-regression/"></div></div><div><a data-url="https://www.r-bloggers.com/the-bayesian-approach-to-ridge-regression/" data-counturl="https://www.r-bloggers.com/the-bayesian-approach-to-ridge-regression/" data-count="vertical" data-text="The Bayesian approach to ridge regression" 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.onthelambda.com/2016/10/30/the-bayesian-approach-to-ridge-regression/"> R – On the lambda</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/qEKiiKQE0xo"></a></div>