<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-5-event-modeling/"></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/oS9G7Tk8ETQ/rwml-R-chapter-5-event-model.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>The rwml-R Github repo is updated with R code for the event modeling examples from Chapter 5 of the book “Real-World Machine Learning” by Henrik Brink, Joseph W. Richards, and Mark Fetherolf. Examples given include reading large data files with the <code>fread</code> function from <code>data.table</code>, optimization of model parameters with <code>caret</code>, computing and plotting ROC curves with <code>ggplot2</code>, engineering date-time features, and determining the relative importance of features with the <code>varImp</code> function in <code>caret</code>.</p>
<h2>Event-Modeling Data</h2>
<p>The data for the event modeling examples in chapter 5 of the book is from the<br>
<a target="_blank" rel="nofollow" href="https://www.kaggle.com/c/event-recommendation-engine-challenge">Kaggle Event Recommendation Engine Challenge</a>.<br>
The rules of the challenge prohibit redristribution of the data, so the reader must<br>
download the data from <a target="_blank" rel="nofollow" href="https://www.kaggle.com/">Kaggle</a>.</p>
<h2>Using fread</h2>
<p>In order to work through the examples, features from the rather large<br>
<code>event.csv</code> file are processed several times. To save time, an alternative to<br>
the <code>read.csv</code> function is needed. This is where the <code>fread</code> function from the<br>
<code>data.table</code> library comes in. It is similar to <code>read.table</code> but<br>
faster and more convenient. On my MacBook Pro, it took only seven seconds to read<br>
the <code>event_id</code>, <code>lat</code>, and <code>lng</code> features from the >1GB <code>events.csv</code> data file.</p>
<figure>
<pre><code data-lang="r"><span>events</span><span> </span><span>&lt;-</span><span> </span><span>fread</span><span>(</span><span>file.path</span><span>(</span><span>dataDir</span><span>,</span><span> </span><span>&quot;events.csv&quot;</span><span>),</span><span>
</span><span>sep</span><span>=</span><span>&quot;,&quot;</span><span>,</span><span>
</span><span>colClasses</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;integer64&quot;</span><span>,</span><span>
</span><span>rep</span><span>(</span><span>&quot;NULL&quot;</span><span>,</span><span>6</span><span>),</span><span>
</span><span>&quot;numeric&quot;</span><span>,</span><span>
</span><span>&quot;numeric&quot;</span><span>,</span><span>
</span><span>rep</span><span>(</span><span>&quot;NULL&quot;</span><span>,</span><span>101</span><span>)))</span></code></pre>
</figure>
<h2>Initial cross-validated ROC curve and AUC metric</h2>
<p>Once a training data set is built with features from the <code>events.csv</code>,<br>
<code>train.csv</code>, and <code>users.csv</code> files, the <code>caret</code> <code>train</code> function is used<br>
to train a random forsest model<br>
evaluated using 10-fold cross-validation with the<br>
receiver operating characteristic (ROC) curve as the metric.<br>
The ROC curve and area under the curve (AUC) for the model (when applied<br>
to held-out test data from the training data set) are shown in the figure<br>
below. An AUC of 0.86 is achieved with the initial set of six features.</p>
<p><img alt="Cross-validated ROC curve and AUC metric" src="https://i1.wp.com/padamson.github.io/assets/figure5.3-1.png?w=456" data-recalc-dims="1"></p>
<p><a data-url="https://padamson.github.io/r/ggplot2/dplyr/caret/randomforest/fread/roc/auc/importance/crossvalidation/featureengineering/2017/04/08/rwml-R-chapter-5-event-model.html" data-count="true" data-text="R code to accompany Real-World Machine Learning (Chapter 5): Event Modeling" target="_blank" data-image="http://padamson.github.io/assets/figure5.3-1.png" rel="nofollow" href="http://feedproxy.google.com/share">Twitshot</a><br>
</p>
<h2>Inclusion of date-time and bag-of-words features lead to over-fitting</h2>
<p>Ten date-time features (such as ‘‘week of the year’’ and ‘‘day of the week’’)<br>
are extracted from the timestamp column of the <code>events.csv</code> file.<br>
When added to the model, the AUC actually decreases slightly,<br>
indicating over-fitting. The AUC decreases even more when available<br>
‘‘bag-of-words’’ data is included in the model.</p>
<h2>Feature importance</h2>
<p>The <code>varImp</code> function from the <code>caret</code> package computes the<br>
relative importance of each variable for objects produced by the <code>train</code><br>
method. A plot of the relative importance for the top seven variables<br>
in the final random forest model is shown below.</p>
<p><img alt="The random forest feature importances for the top seven features in the event-recommendation model" src="https://i2.wp.com/padamson.github.io/assets/rf.importance-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/Chapter5.html">all of the event-modeling examples from chapter 5</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/oS9G7Tk8ETQ"></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/oS9G7Tk8ETQ/rwml-R-chapter-5-event-model.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/fyf6KUTrDeU"></a></div>