<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/r-code-to-accompany-real-world-machine-learning-chapter-6-making-predictions/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/DataProne-R/~3/qEMNHj-qbKw/rwml-R-chapter-6-nyc-taxi-predictions.html"> data prone - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Abstract</h2>
<p>In the latest update to the rwml-R Github repo, R code is provided to complete the analysis of New York City taxi data from Chapter 6 of the book “Real-World Machine Learning” by Henrik Brink, Joseph W. Richards, and Mark Fetherolf. Examples given include exploratory data analysis with <code>ggplot2</code> to identify and remove bad data; scaling numerical data to remove potential bias; development of machine learning models with <code>caret</code>; plotting of receiver operating characteristic (ROC) curves to evaluate model performance; and tabulation and display of feature importance using <code>caret</code> and <code>gridExtra</code>.</p>
<h2>Data import and EDA for NYC taxi example</h2>
<p>As mentioned in <a target="_blank" rel="nofollow" href="https://padamson.github.io/r/ggplot2/dplyr/fread/2017/04/22/rwml-R-chapter-6-nyc-taxi-eda.html">my previous post</a>,<br>
<a target="_blank" rel="nofollow" href="https://padamson.github.io/rwml-R/Chapter6.html">the complete code-through for Chapter 6</a> includes<br>
examples of getting the data and performing exploratory data analysis (EDA).</p>
<h2>Omitting bad data</h2>
<p>A plot of the <code>tip_amount</code> data reveals a large number of rides that<br>
result in no tip. Further analysis shows that these “no tip” rides are<br>
associated with cash payers. In order to develop a model to predict<br>
tipping behavior based on other factors, we must remove the data for<br>
which <code>payment_type == &quot;CSH&quot;</code>. This is done easily with the <code>filter</code><br>
function in <code>dplyr</code>. The remaining data is then plotted in a<br>
histogram with <code>ggplot2</code>.</p>
<figure>
<pre><code data-lang="r"><span>dataJoined</span><span> </span><span>&lt;-</span><span> </span><span>dataJoined</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>payment_type</span><span> </span><span>!=</span><span> </span><span>&quot;CSH&quot;</span><span>)</span><span>
</span><span>p</span><span>9</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>dataJoined</span><span>,</span><span> </span><span>aes</span><span>(</span><span>tip_amount</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_histogram</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-1</span><span>,</span><span> </span><span>16.0</span><span>),</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Tip amount (USD)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Count&quot;</span><span>)</span><span>
</span><span>p</span><span>9</span></code></pre>
</figure>
<p><img alt="The distribution of tip amounts when omitting cash payments" src="https://i2.wp.com/padamson.github.io/assets/figure6.6-1.png?w=456" data-recalc-dims="1"></p>
<h2>Model development and evaluation</h2>
<p>After we have explored the data and identified the specific prediction<br>
that we would like to make (Will non-cash rides result in a tip?), we<br>
move onto model development. The effectiveness of the machine learning models<br>
are evaluated by plotting the ROC curves and computing the Area Under the<br>
Curve (AUC) using code developed for <a target="_blank" rel="nofollow" href="https://padamson.github.io/rwml-R/Chapter4.html">Chapter 4</a> (with slight<br>
modifications for our purposes here).</p>
<p>For each model, we follow our typical <code>caret</code> workflow of splitting the data<br>
into training and test data sets with <code>createDataPartition</code>, setting up<br>
control parameters with the <code>trainControl</code> function, training the model with<br>
<code>train</code>, and making predictions with <code>predict</code>. The variable importance is<br>
calculated with the <code>varImp</code> function of <code>caret</code>. We start with a simple<br>
logistic regression model with the numerical data only. We then systematically<br>
improve prediction results by moving to a random forest model, adding<br>
categorical variables, and engineering date-time features. The final<br>
ROC curve and table of variable importances are shown below.</p>
<p><img alt="The ROC curve and feature importance list for the random forest model, including all categorical features and additional date-time features" src="https://i0.wp.com/padamson.github.io/assets/figure6.11b-1.png?w=456" data-recalc-dims="1"></p>
<h2>Feedback welcome</h2>
<p>If you have any feedback on the <a target="_blank" rel="nofollow" href="https://github.com/padamson/rwml-R">rwml-R</a> project, please<br>
leave a comment below or use the Tweet button.<br>
As with any of my projects, feel free to <a target="_blank" rel="nofollow" href="https://github.com/padamson/rwml-R/fork">fork the rwml-R repo</a><br>
and submit a pull request if you wish to contribute.<br>
For convenience, I’ve created a <a target="_blank" rel="nofollow" href="https://padamson.github.io/rwml-R/">project page for rwml-R</a> with<br>
the generated HTML files from <code>knitr</code>, including a page with<br>
<a target="_blank" rel="nofollow" href="https://padamson.github.io/rwml-R/Chapter6.html">all of the examples from chapter 6</a>.</p>
<p><a data-style="mega" target="_blank" rel="nofollow" href="https://github.com/padamson/rwml-R/archive/master.zip" data-icon="octicon-cloud-download">Download</a><br>
<a data-style="mega" data-count-href="http://feedproxy.google.com/padamson/rwml-R/network" data-count-aria-label="# forks on GitHub" target="_blank" data-count-api="/repos/padamson/rwml-R#forks_count" rel="nofollow" href="https://github.com/padamson/rwml-R/fork" data-icon="octicon-repo-forked">Fork</a></p>
<p><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/DataProne-R/~4/qEMNHj-qbKw"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://feedproxy.google.com/~r/DataProne-R/~3/qEMNHj-qbKw/rwml-R-chapter-6-nyc-taxi-predictions.html"> data prone - R</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/x6dAxLhwd5E"></a></div>