<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/how-to-use-data-analysis-for-machine-learning-part-2/"></div></div><div><a data-url="http://www.r-bloggers.com/how-to-use-data-analysis-for-machine-learning-part-2/" data-counturl="http://www.r-bloggers.com/how-to-use-data-analysis-for-machine-learning-part-2/" data-count="horizontal" data-text="How to use data analysis for machine learning, part 2" 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.sharpsightlabs.com/data-analysis-machine-learning-example-part2/"> r-bloggers – SHARP SIGHT LABS</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In part 1, we went over how to use <a href="http://www.sharpsightlabs.com/data-analysis-machine-learning-example-part1/">data visualization and data analysis prior to machine learning</a>. For example, we discussed how to visualize the data to identify potential issues in the dataset, examine the variable distributions, etc.</p>
<p>In this blog post, we’ll continue by building a very simple model and using data visualization to examine that model.</p>
<p>Just a quick reminder: as I noted in part 1, we’re working with a very simple model. This is deliberately a “toy” model, which allows us to focus on the visualization/analysis aspect of the task without the added level of complexity that we’d inject by using a more advanced machine learning algorithm. </p>
<p>Having said that, let’s dive in.</p>
<h2>Build a simple linear regression model</h2>
<p>Having <a href="http://www.sharpsightlabs.com/data-analysis-machine-learning-example-part1/">explored our data in part 1</a>, let’s continue our work by building a basic linear regression model.</p>
<p>We’ll build a simple linear regression model by regressing  onto . Effectively, we’re going to fit a straight line on top of the following data:</p>
<p><img alt="1_data-analysis-for-ML2_scatter_medv-vs-rm_2016-06-21" class="wp-image-2667" src="http://i2.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/06/1_data-analysis-for-ML2_scatter_medv-vs-rm_2016-06-21.png?resize=456%2C427" data-recalc-dims="1"></p>
<p>Let’s create the linear regression model using the  function:</p>
<pre class="wp-code-highlight">
#-----------
# GET DATA
#-----------

data(Boston, package = &quot;MASS&quot;)

head(Boston)

#------------------------
# BUILD MODEL USING lm()
#------------------------

lm.boston_simple &lt;- lm(medv ~ rm
                       ,data = Boston
                       )
</pre>
<p> </p>
<h2>Plot the model</h2>
<p>Now that we’ve built the model, let’s quickly plot it.</p>
<p>To do this, we’re going to:</p>
<p>1. Extract the coefficients from our model object using the function. Note here that I’m using the prefix ““. This prefix is meaningless syntactically, but I use prefixes like these to help keep my objects organized. It’s slightly unconventional, but it absolutely helps with ‘bookkeeping’ and keeping track of objects.</p>
<p>2. Plot the data and model using ggplot<br>
Here, we’ll just plot the data points using , and then add the regression line as <a href="https://rpubs.com/hadley/ggplot2-layers">an additional layer</a> using .</p>
<p>Keep in mind that in this case, we can plot the model because we have only two variables (one predictor and one target). That is, because we only have two variables to visualize, we can visualize it with a scatterplot. However, in many cases, you’ll build models with <i>many predictors</i> and there won’t be a way to directly visualize the model in many dimensions.</p>
<pre class="wp-code-highlight">
#-------------------------------
# PLOT LINEAR MODEL ON SCATTER
#-------------------------------


# EXTRACT MODEL COEFFICIENTS

coef.lm_icept &lt;- coef(lm.boston_simple)[1]
coef.lm_slope &lt;- coef(lm.boston_simple)[2]


# PLOT MODEL AND POINTS

require(ggplot2)
ggplot() +
  geom_point(data = Boston, aes(x = rm, y = medv)) +
  geom_abline(intercept = coef.lm_icept, slope = coef.lm_slope, color = &quot;red&quot;)

</pre>
<p> </p>
<p><img alt="2_data-analysis-for-ML2_regression_medv-vs-rm_2016-06-21" class="wp-image-2668" src="http://i1.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/06/2_data-analysis-for-ML2_regression_medv-vs-rm_2016-06-21.png?resize=456%2C426" data-recalc-dims="1"></p>
<p>Visually, this model looks like an OK fit. We can see that the regression line cuts through the bulk of the points, but there are several points that are far away from the regression line. That is, there are quite a few large residuals.</p>
<h2>Residual plots and diagnostics</h2>
<p>Now that we’ve looked at a plot of the model against the data, let’s use some additional visualization techniques to evaluate the model. To do this, we’ll use residual plots.</p>
<p>Residual plots can be very useful for evaluating how well the model fits the data, as noted by Kuhn and Johnson in their excellent book, <a href="http://amzn.com/dp/B00K15TZU0">Applied Predictive Modeling</a>:</p>
<blockquote><p>Visualizations of the model fit, particularly residual plots, are critical to understanding whether the model is fit for purpose.”</p>
</blockquote>
<p>One thing that we can evaluate with residual plots is whether or not there is a linear relationship between the predictor(s) and the response. For linear regression, we’re making a strong assumption about the relationship between the target and the predictors. We’re assuming that there’s a linear relationship. In the case of simple linear regression, we’re assuming a relationship of the form <img title="Rendered by QuickLaTeX.com" alt="y=\beta_0 + \beta_1x" src="http://i1.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-7078b08399c5f2c196f38be02dbac8b2_l3.png?resize=100%2C16" data-recalc-dims="1"> (although in the case of multiple regression, we assume a hyperplane instead of a line).</p>
<p>This is a strong assumption that doesn’t always hold. </p>
<p>How do we know? How do we verify that the relationship is linear?</p>
<h3>Plot: fitted values vs. residuals</h3>
<p>Residual plots are an excellent diagnostic tool for validating this assumption. A plot of the predicted value vs the residual can help us identify a possible non-linear relationship. If we find a non-linear relationship, this is an indication that the linear model is not a good fit.</p>
<pre class="wp-code-highlight">
#------------------------------
# SCATTERPLOT: Fitted vs Resid
#------------------------------

df.lm.boston_simple &lt;- fortify(lm.boston_simple)
head(df.lm.boston_simple)

# WITH LOESS SMOOTHE
ggplot(data = df.lm.boston_simple, aes(x = .fitted, y = .resid)) +
  geom_point() + 
  stat_smooth(se = FALSE) +
  labs(x = &quot;fitted&quot;, y = &quot;residual&quot;)

</pre>
<p> </p>
<p><img alt="3_data-analysis-for-ML2_fitted-vs-residuals_2016-06-21" class="wp-image-2669" src="http://i0.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/06/3_data-analysis-for-ML2_fitted-vs-residuals_2016-06-21.png?resize=456%2C426" data-recalc-dims="1"></p>
<p>If the relationship between the predictor and the response is actually linear, then the plot of fitted values vs residuals should look random, without any detectable pattern.</p>
<p>The data in this chart do not look random. There’s a noticeable, non-linear pattern in the plot. This is a bit of a cause for concern and an indication that we may want to modify our model with additional predictors, transformations of our predictors, etc.</p>
<h3>Plot: histogram of the residuals</h3>
<p>We can also look at the histogram of the residuals. When we use a linear regression model, we want the residuals to be normally distributed, so we can visually inspect the histogram to visually identify normality or non-normality.</p>
<pre class="wp-code-highlight">
#-----------------------------
# DIAGNOSTIC PLOT: 
#  Histogram of the Residuals
#-----------------------------

ggplot(data = df.lm.boston_simple, aes(x = .resid)) +
  geom_histogram()

</pre>
<p> </p>
<p><img alt="4_data-analysis-for-ML2_histogram-residuals_2016-06-21" class="wp-image-2670" src="http://i0.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/06/4_data-analysis-for-ML2_histogram-residuals_2016-06-21.png?resize=401%2C391" data-recalc-dims="1"></p>
<p>In this plot, we can see more clearly that the residuals are not normally distributed. Again, this is an indication that the model is not a great fit, and we may want to make some modifications.</p>
<h2>Other uses for visualization in machine learning</h2>
<p>As I mentioned in the beginning of the post, this is really a very simple example. Not only is simple linear regression perhaps the <i>simplest</i> example of machine learning, but in the interest of brevity, I’ve left out some other parts of the workflow where you might need visualization.</p>
<p>Having said that, I’ll give you a quick list of some additional ways that you might need visualization and analysis in ML, either for linear regression, or for more advanced techniques:</p>
<p>1. <b>Model tuning</b><br>
For some model types other than linear regression, you need to “tune” your model by selecting the best values for model parameters. When you perform model tuning, there are several visualization techniques that can be helpful for selecting the ideal parameters. <a href="https://flowingdata.com/2010/01/21/how-to-make-a-heatmap-a-quick-and-easy-solution/">Heat maps</a> can be particularly useful for this purpose.</p>
<p>2. <b>Learning curves</b><br>
To diagnose potential problems in your model and optimize its performance, you can <a href="https://www.youtube.com/watch?v=g4XluwGYPaA"> plot learning curves</a>.</p>
<p>3. <b>Presenting your results to business partners and management</b><br>
Even after you build your final model, if you’re in industry, you’ll have to communicate your results.</p>
<p>In many cases, your audience may have limited mathematical sophistication. Because of this, data visualization is frequently the best way to communicate the results of your model and explain how it works to your business patterns and management. </p>
<p>This is often overlooked in textbooks and tutorials, but in industry and business, it’s a huge part of your job. </p>
<h2>If you want to learn machine learning, learn data analysis first</h2>
<p>Between this post and the last blog post, you should have a basic idea of how you’ll use data visualization and analysis in the machine learning workflow.</p>
<p>As I’ve emphasized several times, data analysis and data visualization form the foundation of more advanced data science topics. If you want to learn applied machine learning, you <a href="http://www.sharpsightlabs.com/machine-learning-prerequisite-isnt-math/">need to have a strong foundation in analysis and visualization</a>.</p>
<p>If you’re ready to get really serious about mastering data analysis and visualization in R, I’m going to be launching a new training program very soon. It’ll enable you to rapidly master analysis and visualization. You’ll learn everything you need to know, from the ground up.</p>
<p>If you’re interested in the training program, sign up for our email list so you know when the program launches.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://www.sharpsightlabs.com/data-analysis-machine-learning-example-part2/">How to use data analysis for machine learning, part 2</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.sharpsightlabs.com/">SHARP SIGHT LABS</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="http://www.r-bloggers.com/how-to-use-data-analysis-for-machine-learning-part-2/"></div></div><div><a data-url="http://www.r-bloggers.com/how-to-use-data-analysis-for-machine-learning-part-2/" data-counturl="http://www.r-bloggers.com/how-to-use-data-analysis-for-machine-learning-part-2/" data-count="vertical" data-text="How to use data analysis for machine learning, part 2" 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.sharpsightlabs.com/data-analysis-machine-learning-example-part2/"> r-bloggers – SHARP SIGHT LABS</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/uO6bzwh4nfA"></a></div>