<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
<a href="https://www.kaggle.com/c/event-recommendation-engine-challenge">Kaggle Event Recommendation Engine Challenge</a>. The rules of the challenge prohibit redristribution of the data, so the reader must download the data from <a 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 <code>event.csv</code> file are processed several times. To save time, an alternative to the <code>read.csv</code> function is needed. This is where the <code>fread</code> function from the <code>data.table</code> library comes in. It is similar to <code>read.table</code> but faster and more convenient. On my MacBook Pro, it took only seven seconds to read
the <code>event_id</code>, <code>lat</code>, and <code>lng</code> features from the >1GB <code>events.csv</code> data file.</p>
<pre><code><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><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>, <code>train.csv</code>, and <code>users.csv</code> files, the <code>caret</code> <code>train</code> function is used
to train a random forsest model evaluated using 10-fold cross-validation with the receiver operating characteristic (ROC) curve as the metric.
The ROC curve and area under the curve (AUC) for the model (when applied
to held-out test data from the training data set) are shown in the figure
below. An AUC of 0.86 is achieved with the initial set of six features.</p>
<p></p>
<p><a target="_blank" href="http://padamson.github.io/share">Twitshot</a>
</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’’)
are extracted from the timestamp column of the <code>events.csv</code> file. When added to the model, the AUC actually decreases slightly, indicating over-fitting. The AUC decreases even more when available ‘‘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 relative importance of each variable for objects produced by the <code>train</code>
method. A plot of the relative importance for the top seven variables
in the final random forest model is shown below.</p>
<p></p>
<h2>Feedback welcome</h2>
<p>If you have any feedback on the <a href="https://github.com/padamson/rwml-R">rwml-R</a> project, please
leave a comment below or use the Tweet button.
As with any of my projects, feel free to <a href="https://github.com/padamson/rwml-R/fork">fork the rwml-R repo</a>
and submit a pull request if you wish to contribute.
For convenience, I’ve created a <a href="https://padamson.github.io/rwml-R/">project page for rwml-R</a> with the generated HTML files from <code>knitr</code>, including a page with <a href="https://padamson.github.io/rwml-R/Chapter5.html">all of the event-modeling examples from chapter 5</a>.</p>
<p><a href="https://github.com/padamson/rwml-R/archive/master.zip">Download</a>
<a href="https://github.com/padamson/rwml-R/fork">Fork</a></p>