<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/machine-learning-results-in-r-one-plot-to-rule-them-all-part-2-regression-models/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/machine-learning-results-in-r-one-plot-to-rule-them-all-part-2-regression-models/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Given the number of people interested in my first post for visualizing <a rel="nofollow" href="https://datascienceplus.com/machine-learning-results-one-plot-to-rule-them-all/" target="_blank">Classification Models Results</a>, I’ve decided to create and share some new function to visualize and compare whole Linear Regression Models with one line of code. These plots will help us with our time invested in model selection and a general understanding of our results. </p>
<p>Where are we going with this post? Let’s take a quick look at the final output: a quick nice dashboard with everything you’d need to compare and evaluate if your regression model is looking good, compare with others, or get working on further improvements.<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_full-3.png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_full-3-1024x640.png?w=450&ssl=1" alt="" class="wp-image-15557" data-recalc-dims="1"></a></p>
<p>Interesting to say that, the exact same function <code>mplot_full</code> used before in the <a rel="nofollow" href="https://datascienceplus.com/machine-learning-results-one-plot-to-rule-them-all/" target="_blank">Part 1 – Classification Models</a> post, will work on Regressions too <code>lares::updateLares()</code>. There’s a simple validator which will automatically label the model as a regression if the ‘tag’ values have more than 6 unique values and as a classifier otherwise. </p>
<p>Special thanks to Shahzeb from Pennsylvania, who asked me to develop these scripts into my library for his personal projects. A round of applause to him for letting me share this nice work with the community as well. </p>
<p><b>NOTE:</b> If you wish to replicate the following plots, you can download the dummy data used for the examples <a rel="nofollow" href="http://bit.ly/2uSgiAn" target="_blank">here</a>.</p>
<h2>Install the ‘lares’ library</h2>
<p>First, I’d like to clarify that this is my <a rel="nofollow" href="https://github.com/laresbernardo/lares" target="_blank">personal library</a> which I use in my everyday tasks to simplify and boost my work. So, if there are no documentations or it’s not in CRAN, that’s why! Said this, it is time to start and dig in…</p>
<pre>
devtools::install_github(&quot;laresbernardo/lares&quot;)
</pre>
<h2>Regression results plot</h2>
<p>The most obvious plot to study for a linear regression model, you guessed it, is the regression itself. If we plot the predicted values vs the real values we can see how close they are to our reference line of 45° (intercept = 0, slope = 1). If we’d had a very sparse plot where we can see no clear tendency over that line, then we have a bad regression. On the other hand, if we have all our points over the line, I bet you gave the model your wished results! </p>
<p>Then, the Adjusted R2 on the plot gives us an easy parameter for us to compare models and how well did it fits our reference line. The nearer this value gets to 1, the better. Without getting too technical, if you add more and more useless variables to a model, this value will decrease; but, if you add useful variables, the Adjusted R-Squared will improve. </p>
<p>We also get the RMSE and MAE (Root-Mean Squared Error and Mean Absolute Error) for our regression’s results. MAE measures the average magnitude of the errors in a set of predictions, without considering their direction. On the other side we have RMSE, which is a quadratic scoring rule that also measures the average magnitude of the error. It’s the square root of the average of squared differences between prediction and actual observation. Both metrics can range from 0 to ∞ and are indifferent to the direction of errors. They are negatively-oriented scores, which means lower values are better.</p>
<p>Having a data.frame with the ‘label’ and ‘prediction’ values (real value and obtained from the model value) in our environment, we can start plotting:</p>
<pre>
lares::mplot_lineal(tag = results$label, 
                    score = results$pred,
                    subtitle = &quot;Salary Regression Model&quot;,
                    model_name = &quot;simple_model_02&quot;)
</pre>
<p>Which will give us (and save into our working directory if set to save = TRUE) the following plot:<br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_lineal-1.png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_lineal-1-1024x1024.png?w=450&ssl=1" alt="" class="wp-image-15567" data-recalc-dims="1"></a></p>
<h2>Errors plot</h2>
<p>If we achieved a good model, now we have to learn to deal with its errors. As I said in the other post, this is the easiest plot to explain to the C-level guys to understand the consequences of getting your model to production. For example, if we tell them that our regression has an Adjusted R2 value of 0.97 and a great p-value of 1e-14, probably, they’ll blink twice and give you an awkward face. But, if you tell them that you trained your model and when tested in an untouched-by-your-algorithm dataset you get only a 5% of error in 90% of the cases, that is much more worthy to sell (depending on the final goal)!</p>
<p>On the background, I calculate the error of each calculation, absolute and porcentual, and sort them from the smallest errors to the worst. Finally, I split them into n different buckets with the same amount of observations. As you can see, we can conclude how much of our test set have an error below a value or percentage with the first two plots. The third one is a density plot for the real porcentual error, with a 0 reference line, where we can see in which ranges our errors are most common.</p>
<pre>
lares::mplot_cuts_error(tag = results$label, 
                        score = results$pred,
                        title = &quot;Salary Regression Model&quot;,
                        model_name = &quot;simple_model_02&quot;)
</pre>
<p>Which gives us these three plots:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_ncuts_error-1.png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_ncuts_error-1-1024x1024.png?w=450&ssl=1" alt="" class="wp-image-15480" data-recalc-dims="1"></a></p>
<h2>Distribution plot</h2>
<p>This is a simple comparison between the real values and the predicted values. The more similar this two curves are, the better. I give a smaller size to this plot because it gives us an idea of the whole picture more than a specific metric.</p>
<pre>
lares::mplot_density(tag = results$label, 
                     score = results$pred,
                     subtitle = &quot;Salary Regression Model&quot;,
                     model_name = &quot;simple_model_02&quot;)
</pre>
<p>Plots:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_distribution.png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_distribution-1024x1024.png?w=450&ssl=1" alt="" class="wp-image-15426" data-recalc-dims="1"></a></p>
<h2>Final All-In Plot</h2>
<p>We do not have to plot non of the above functions to get this result (that’s the whole point of this post). We need to provide only two values: tag for real values and score for our model’s prediction or regression. The rest of the parameters may not be used. </p>
<pre>
lares::mplot_full(tag = results$label, 
                  score = results$pred,
                  splits = 10,
                  subtitle = &quot;Salary Regression Model&quot;,
                  model_name = &quot;simple_model_02&quot;,
                  save = T)
</pre>
<p><a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_full-3.png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_full-3-1024x640.png?w=450&ssl=1" alt="" class="wp-image-15557" data-recalc-dims="1"></a></p>
<p>These functions were adapted into my past <code>mplot</code> series functions from the <code>lares</code> library, merging some of them so they plot automatically, whether we have a categorical or a numerical input. Remember, if your tag values have more than 6 unique values, it’ll assume you are studying a regression.</p>
<h3>BONUS: Splits by quantiles</h3>
<p>One of my favourite functions to study our model’s quality is the following. What it shows is the result of arranging all scores or predicted values in sorted quantiles, from worst to best, and see how the classification goes compared to our test set. Being a regression model, we won’t always need this plot but I though it might be useful for some cases. More detailed explanation in <a rel="nofollow" href="https://datascienceplus.com/machine-learning-results-one-plot-to-rule-them-all/" target="_blank">this other post</a>.</p>
<pre>
lares::mplot_splits(tag = results$label, 
                    score = results$pred,
                    split = 8)
</pre>
<p>Which will give us something like this:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/lares-splits.png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/lares-splits-1024x768.png?w=450&ssl=1" alt="" class="wp-image-15535" data-recalc-dims="1"></a></p>
<p>Hope this post was as useful as the <a rel="nofollow" href="https://datascienceplus.com/machine-learning-results-one-plot-to-rule-them-all/" target="_blank">other one</a>. If anyone wishes to contact me, don’t hesitate to add me on <a rel="nofollow" href="https://www.linkedin.com/in/laresbernardo/" target="_blank">Linkedin</a>, get <a rel="nofollow" href="https://datascienceplus.com/author/bernardo-lares/" target="_blank">in touch with me</a>, and/or comment below. </p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/story-of-pairs-ggpairs-and-the-linear-regression/" title="Permanent Link toStory of pairs, ggpairs, and the linear regression" target="_blank">Story of pairs, ggpairs, and the linear regression</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/extract-fred-data-for-ols-regression-analysis-a-complete-tutorial/" title="Permanent Link toExtract FRED Data for OLS Regression Analysis: A Complete R Tutorial" target="_blank">Extract FRED Data for OLS Regression Analysis: A Complete R Tutorial</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/mnist-for-machine-learning-beginners-with-softmax-regression/" title="Permanent Link toMNIST For Machine Learning Beginners With Softmax Regression" target="_blank">MNIST For Machine Learning Beginners With Softmax Regression</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/time-dependent-roc-for-survival-prediction-models-in-r/" title="Permanent Link toTime-dependent ROC for Survival Prediction Models in R" target="_blank">Time-dependent ROC for Survival Prediction Models in R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/xplain-r-package-for-interpretations-and-explanations-of-statistical-results/" title="Permanent Link toxplain – R package for interpretations and explanations of statistical results" target="_blank">xplain – R package for interpretations and explanations of statistical results</a></li>
</ol>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/machine-learning-results-in-r-one-plot-to-rule-them-all-part-2-regression-models/"> R Programming – DataScience+</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/QLDgYc5HxSM" height="1"></a></div>