<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/moving-largish-data-from-r-to-h2o-spam-detection-with-enron-emails/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/02/18/svmlite"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Moving around sparse matrices of text data – the limitations of <code>as.h2o</code></h2>
<p>This post is the resolution of a challenge I first <a target="_blank" rel="nofollow" href="http://ellisp.github.io/blog/2016/12/31/sparse-bags">wrote about in late 2016</a>, moving large sparse data from an R environment onto an <a target="_blank" rel="nofollow" href="http://h2o.ai/">H2O cluster</a> for machine learning purposes. In that post, I experimented with functionality recently added by the H2O team to their supporting R package, the ability for <code>as.h2o()</code> to interpret a sparse <code>Matrix</code> object from R and convert it to an H2O frame. The <code>Matrix</code> and <code>as.h2o</code> method is ok for medium sized data but broke down on my hardware with a larger dataset – a bags of words from <a target="_blank" rel="nofollow" href="https://archive.ics.uci.edu/ml/machine-learning-databases/bag-of-words/">New York Times articles</a> with 300,000 rows and 102,000 columns. Cell entries are the number of times a particular word is used in the document represented by a row and are mostly empty, so my 12GB laptop has no problem managing the data in a sparse format like <code>Matrix</code> from the <code>Matrix</code> package or a simple triplet matrix from the <code>slam</code> package. I’m not sure what <code>as.h2o</code> does under the hood in converting from <code>Matrix</code> to an H2O frame, but it’s too much for my laptop.</p>
<p>My motivation for this is that I want to use R for convenient pre-processing of textual data using the <a target="_blank" rel="nofollow" href="http://tidytextmining.com/"><code>tidytext</code></a> approach; but H2O for high powered machine learning. <code>tidytext</code> makes it easy to create a sparse matrix with <code>cast_dtm</code> or <code>cast_sparse</code>, but uploading this to H2O can be a challenge.</p>
<h3>How to write from R into SVMLight format</h3>
<p>After some <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/41340086/how-to-cast-data-from-long-to-wide-format-in-h2o">to-and-fro on Stack Overflow</a>, the best advice was to export the sparse matrix from R into a SVMLight/LIBSVM format text file, then read it into H2O with <code>h2o.importFile(..., parse_type = &quot;SVMLight&quot;)</code>. This turned the problem from an difficult and possibly intractable memory managment challenge into a difficult and possibly intractable data formatting and file writing challenge – how to efficiently write files in SVMLight format.</p>
<p>SVMLight format combines a data matrix with some modelling information ie the response value of a model, or “label” as it is (slightly oddly, I think) often called in this world. Instead of a more conventional row-based sparse matrix format which might convey information in row-column-value triples, it uses label-column:value indicators. It looks like this:</p>
<div>
<pre><code>1 10:3.4 123:0.5 34567:0.231
0.2 22:1 456:0.3
</code></pre>
</div>
<p>That example is equivalent to two rows of a sparse matrix with at least 34,567 columns. The first row has <code>1</code> as the response value, <code>3.4</code> in the 10th column of explanatory variables, and 0.231 in the 34,567th column; the second row has 0.2 as the response value, <code>1</code> in the 22nd column, and so on.</p>
<p>Writing data from R into this format is a known problem discussed in <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/12112558/read-write-data-in-libsvm-format">this Q&amp;A on Stack Overflow</a>. Unfortunately, the top rated answer to that question, <code>e1071::write.svm</code> is reported as being slow, and also it is integrated into a workflow that requires you to first fit a Support Vector Machine model to the data, a step I wanted to avoid. That Q&amp;A led me to a GitHub repo by zygmuntz that had a (also slow) solution for writing dense matrices into SVMLight format, but that didn’t help me as my data were too large for R to hold in dense format. So I wrote my own version for taking simplet triplet matrices and writing SVMLight format. My first version depended on nested <code>paste</code> statements that were <code>apply</code>d to each row of the data and was still too slow at scale, but with the help of yet another <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/41477700/optimising-sapply-or-for-paste-to-efficiently-transform-sparse-triplet-m">Stack Overflow interaction</a> and some data table wizardry by @Roland this was able to reduce the expected time writing my 300,000 by 83,000 New York Times matrix (having removed stop words) from several centuries to two minutes.</p>
<p>I haven’t turned this into a package – it would seem better to find an existing package to add it to than create a package just for this one function, any ideas appreciated. The <a target="_blank" rel="nofollow" href="https://github.com/ellisp/r-libsvm-format-read-write/blob/master/R/write-sparse-triplets-svm.R">functions are available on GitHub</a> but in case I end up moving them, here they are in full. One function creates a big character vector; the second writes that to file. This means multiple copies of the data need to be held in R and hence creates memory limitations, but is much much faster than writing it one line at a time (seconds rather than years in my test cases).</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>data.table</span><span>)</span><span>
</span><span>library</span><span>(</span><span>slam</span><span>)</span><span>
</span><span># Convert a simple triplet matrix to svm format
</span><span>#' @author Peter Ellis
#' @return a character vector of length n = nrow(stm)
</span><span>calc_stm_svm</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>stm</span><span>,</span><span> </span><span>y</span><span>){</span><span>
</span><span># returns a character vector of length y ready for writing in svm format
</span><span> </span><span>if</span><span>(</span><span>!</span><span>&quot;simple_triplet_matrix&quot;</span><span> </span><span>%in%</span><span> </span><span>class</span><span>(</span><span>stm</span><span>)){</span><span>
</span><span>stop</span><span>(</span><span>&quot;stm must be a simple triple matrix&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span>if</span><span>(</span><span>!</span><span>is.vector</span><span>(</span><span>y</span><span>)</span><span> </span><span>|</span><span> </span><span>nrow</span><span>(</span><span>stm</span><span>)</span><span> </span><span>!=</span><span> </span><span>length</span><span>(</span><span>y</span><span>)){</span><span>
</span><span>stop</span><span>(</span><span>&quot;y should be a vector of length equal to number of rows of stm&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span>n</span><span> </span><span>&lt;-</span><span> </span><span>length</span><span>(</span><span>y</span><span>)</span><span>
</span><span># data.table solution thanks to @roland at http://stackoverflow.com/questions/41477700/optimising-sapply-or-for-paste-to-efficiently-transform-sparse-triplet-m/41478999#41478999
</span><span> </span><span>stm2</span><span> </span><span>&lt;-</span><span> </span><span>data.table</span><span>(</span><span>i</span><span> </span><span>=</span><span> </span><span>stm</span><span>$</span><span>i</span><span>,</span><span> </span><span>j</span><span> </span><span>=</span><span> </span><span>stm</span><span>$</span><span>j</span><span>,</span><span> </span><span>v</span><span> </span><span>=</span><span> </span><span>stm</span><span>$</span><span>v</span><span>)</span><span>
</span><span>res</span><span> </span><span>&lt;-</span><span> </span><span>stm2</span><span>[,</span><span> </span><span>.</span><span>(</span><span>i</span><span>,</span><span> </span><span>jv</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>j</span><span>,</span><span> </span><span>v</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;:&quot;</span><span>))</span><span>
</span><span>][</span><span>order</span><span>(</span><span>i</span><span>),</span><span> </span><span>.</span><span>(</span><span>res</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>jv</span><span>,</span><span> </span><span>collapse</span><span> </span><span>=</span><span> </span><span>&quot; &quot;</span><span>)),</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>i</span><span>][[</span><span>&quot;res&quot;</span><span>]]</span><span>
</span><span>out</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>y</span><span>,</span><span> </span><span>res</span><span>)</span><span>
</span><span>return</span><span>(</span><span>out</span><span>)</span><span>
</span><span>}</span><span>
</span><span>#' @param stm a simple triplet matrix (class exported slam) of features (ie explanatory variables)
#' @param y a vector of labels. If not provided, a dummy of 1s is provided
#' @param file file to write to.
#' @author Peter Ellis
</span><span>write_stm_svm</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>stm</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>1</span><span>,</span><span> </span><span>nrow</span><span>(</span><span>stm</span><span>)),</span><span> </span><span>file</span><span>){</span><span>
</span><span>out</span><span> </span><span>&lt;-</span><span> </span><span>calc_stm_svm</span><span>(</span><span>stm</span><span>,</span><span> </span><span>y</span><span>)</span><span> </span><span>writeLines</span><span>(</span><span>out</span><span>,</span><span> </span><span>con</span><span> </span><span>=</span><span> </span><span>file</span><span>)</span><span>
</span><span>}</span></code></pre>
</figure>
<h2>Example application – spam detection with the Enron emails</h2>
<p>Although I’d used the New York Times bags of words from the UCI machine learning dataset repository for testing the scaling up of this approach, I actually didn’t have anything I wanted to analyse that data for in H2O. So casting around for an example use case I decided on using the <a target="_blank" rel="nofollow" href="http://csmining.org/index.php/enron-spam-datasets.html">Enron email collection for spam detection</a>, first analysed in a <a target="_blank" rel="nofollow" href="http://www.aueb.gr/users/ion/docs/ceas2006_paper.pdf">2006 conference paper by V. Metsis, I. Androutsopoulos and G. Paliouras</a>. As well as providing one of the more sensational corporate scandals of recent times, the Enron case has blessed data scientists with one of the largest published sets of emails collected from their natural habitat.</p>
<p>The original authors classified the emails as spam or ham and saved these pre-processed data for future use and reproducibility. I’m not terribly knowledgeable (or interested) in spam detection, so please take the analysis below as a crude and naive example only.</p>
<h3>Data</h3>
<p>First the data need to be downloaded and unzipped. The files are stored as 6 Tape ARchive files</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidytext</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tm</span><span>)</span><span>
</span><span>library</span><span>(</span><span>testthat</span><span>)</span><span>
</span><span>library</span><span>(</span><span>data.table</span><span>)</span><span>
</span><span>library</span><span>(</span><span>h</span><span>2</span><span>o</span><span>)</span><span>
</span><span>library</span><span>(</span><span>stringr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span>#===========download files=============
# Files described at http://csmining.org/index.php/enron-spam-datasets.html
</span><span>baseurl</span><span> </span><span>&lt;-</span><span> </span><span>&quot;http://csmining.org/index.php/enron-spam-datasets.html?file=tl_files/Project_Datasets/Enron-Spam%20datasets/Preprocessed/enron&quot;</span><span>
</span><span>filenames</span><span> </span><span>&lt;-</span><span> </span><span>paste0</span><span>(</span><span>baseurl</span><span>,</span><span> </span><span>1</span><span>:</span><span>6</span><span>,</span><span> </span><span>&quot;.tar.tar&quot;</span><span>)</span><span>
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>6</span><span>){</span><span>
</span><span>message</span><span>(</span><span>&quot;Downloading &quot;</span><span>,</span><span> </span><span>i</span><span>)</span><span>
</span><span>dfile</span><span> </span><span>&lt;-</span><span> </span><span>paste0</span><span>(</span><span>&quot;enron&quot;</span><span>,</span><span> </span><span>i</span><span>,</span><span> </span><span>&quot;.tar.tar&quot;</span><span>)</span><span>
</span><span>download.file</span><span>(</span><span>filenames</span><span>[</span><span>i</span><span>],</span><span> </span><span>destfile</span><span> </span><span>=</span><span> </span><span>dfile</span><span>,</span><span> </span><span>mode</span><span> </span><span>=</span><span> </span><span>&quot;wb&quot;</span><span>)</span><span>
</span><span>message</span><span>(</span><span>&quot;Un-archiving &quot;</span><span>,</span><span> </span><span>i</span><span>)</span><span>
</span><span>untar</span><span>(</span><span>dfile</span><span>)</span><span>
</span><span>}</span></code></pre>
</figure>
<p>This creates six folders with the names <code>enron1</code>, <code>enron2</code> etc; each with a <code>spam</code> and a <code>ham</code> subfolder containing numerous text files. The files look like this example piece of ham (ie non-spam; a legitimate email), chosen at random:</p>
<div>
<pre><code>Subject: re : creditmanager net meeting
aidan ,
yes , this will work for us .
vince
&quot; aidan mc nulty &quot; on 12 / 16 / 99 08 : 36 : 14 am
to : vince j kaminski / hou / ect @ ect
cc :
subject : creditmanager net meeting
vincent , i cannot rearrange my schedule for tomorrow so i would like to
confirm that we will have a net - meeting of creditmanager on friday 7 th of
january at 9 . 30 your time .
regards
aidan mc nulty
212 981 7422
</code></pre>
</div>
<p>The pre-processing has removed duplicates, emails sent to themselves, some of the headers, etc.</p>
<p>Importing the data into R and making tidy data frames of documents and word counts is made easy by Silge and Robinson’s <code>tidytext</code> package which I never tire of saying is a game changer for convenient analysis of text by statisticians:</p>
<figure>
<pre><code data-lang="r"><span>#=============import to R================
# Adapting the example at http://tidytextmining.com/usenet.html
</span><span>folders</span><span> </span><span>&lt;-</span><span> </span><span>paste0</span><span>(</span><span>&quot;enron&quot;</span><span>,</span><span> </span><span>1</span><span>:</span><span>6</span><span>)</span><span>
</span><span>spam</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>dir</span><span>(</span><span>paste0</span><span>(</span><span>folders</span><span>,</span><span> </span><span>&quot;/spam&quot;</span><span>),</span><span> </span><span>full.names</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>text</span><span> </span><span>=</span><span> </span><span>map</span><span>(</span><span>file</span><span>,</span><span> </span><span>readLines</span><span>,</span><span> </span><span>encoding</span><span> </span><span>=</span><span> </span><span>&quot;Latin-1&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>transmute</span><span>(</span><span>id</span><span> </span><span>=</span><span> </span><span>basename</span><span>(</span><span>file</span><span>),</span><span> </span><span>text</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>unnest</span><span>(</span><span>text</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>SPAM</span><span> </span><span>=</span><span> </span><span>&quot;spam&quot;</span><span>)</span><span>
</span><span>ham</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>dir</span><span>(</span><span>paste0</span><span>(</span><span>folders</span><span>,</span><span> </span><span>&quot;/ham&quot;</span><span>),</span><span> </span><span>full.names</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>text</span><span> </span><span>=</span><span> </span><span>map</span><span>(</span><span>file</span><span>,</span><span> </span><span>readLines</span><span>,</span><span> </span><span>encoding</span><span> </span><span>=</span><span> </span><span>&quot;Latin-1&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>transmute</span><span>(</span><span>id</span><span> </span><span>=</span><span> </span><span>basename</span><span>(</span><span>file</span><span>),</span><span> </span><span>text</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>unnest</span><span>(</span><span>text</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>SPAM</span><span> </span><span>=</span><span> </span><span>&quot;ham&quot;</span><span>)</span><span>
</span><span>enron_raw</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>spam</span><span>,</span><span> </span><span>ham</span><span>)</span><span>
</span><span>#============error checks - failing!===================
# Check against the counts provided at http://csmining.org/index.php/data.html
# Turns out some 3013 spam messages have gone missing
# should be 20170 spam messages and 16545 ham messages:
# returns an error
</span><span>expect_equal</span><span>(</span><span>length</span><span>(</span><span>unique</span><span>(</span><span>enron_raw</span><span>$</span><span>id</span><span>)),</span><span> </span><span>20170</span><span> </span><span>+</span><span> </span><span>16545</span><span>)</span><span> </span><span>enron_raw</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>id</span><span>,</span><span> </span><span>SPAM</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>spam_count</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>SPAM</span><span> </span><span>==</span><span> </span><span>&quot;spam&quot;</span><span>),</span><span> </span><span>ham_count</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>SPAM</span><span> </span><span>==</span><span> </span><span>&quot;ham&quot;</span><span>))</span><span>
</span><span># For my purposes I decide not to worry about this.
</span><span>
</span><span>#=================further processing==================
</span><span>enron</span><span> </span><span>&lt;-</span><span> </span><span>enron_raw</span><span> </span><span>%>%</span><span>
</span><span># will just remove the &quot;Subject:&quot; and &quot;Subject :&quot; and treat subject words like any other
</span><span> </span><span>mutate</span><span>(</span><span>text</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;^Subject *: *&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>text</span><span>),</span><span>
</span><span>text</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;&lt;U.....>&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>text</span><span>,</span><span> </span><span>fixed</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>))</span><span> </span><span>enron_words</span><span> </span><span>&lt;-</span><span> </span><span>enron</span><span> </span><span>%>%</span><span>
</span><span>unnest_tokens</span><span>(</span><span>word</span><span>,</span><span> </span><span>text</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>-</span><span>SPAM</span><span>)</span></code></pre>
</figure>
<p>As well as basic word counts, I wanted to experiment with other characteristics of emails such as number of words, number and proportion of of stopwords (frequently used words like “and” and “the”). I create a traditional data frame with a row for each email, identified by <code>id</code>, and columns indicating whether it is SPAM and those other characteristics of interest.</p>
<figure>
<pre><code data-lang="r"><span># First I'm creating a summary, dense data frame with some numeric info on each document
</span><span>enron_sum1</span><span> </span><span>&lt;-</span><span> </span><span>enron</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>number_characters</span><span> </span><span>=</span><span> </span><span>nchar</span><span>(</span><span>text</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>id</span><span>,</span><span> </span><span>SPAM</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>number_characters</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>number_characters</span><span>))</span><span>
</span><span>enron_sum2</span><span> </span><span>&lt;-</span><span> </span><span>enron_words</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>id</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>number_words</span><span> </span><span>=</span><span> </span><span>length</span><span>(</span><span>word</span><span>))</span><span>
</span><span>enron_sum3</span><span> </span><span>&lt;-</span><span> </span><span>enron_words</span><span> </span><span>%>%</span><span>
</span><span>anti_join</span><span>(</span><span>stop_words</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;word&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>id</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>number_nonstop_words</span><span> </span><span>=</span><span> </span><span>length</span><span>(</span><span>word</span><span>))</span><span>
</span><span>enron_sum_total</span><span> </span><span>&lt;-</span><span> </span><span>enron_sum1</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>enron_sum2</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;id&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>enron_sum3</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;id&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>number_stop_words</span><span> </span><span>=</span><span> </span><span>number_words</span><span> </span><span>-</span><span> </span><span>number_nonstop_words</span><span>,</span><span>
</span><span>proportion_stop_words</span><span> </span><span>=</span><span> </span><span>number_stop_words</span><span> </span><span>/</span><span> </span><span>number_words</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>-</span><span>number_nonstop_words</span><span>)</span><span>
</span><span>enron_sum_total</span></code></pre>
</figure>
<div>
<pre><code>Source: local data frame [33,702 x 6]
Groups: id [33,702]

                                   id  SPAM number_characters number_words number_stop_words
                                &lt;chr> &lt;chr>             &lt;int>        &lt;int>             &lt;int>
1      0001.1999-12-10.farmer.ham.txt   ham                28            4                 0
2    0001.1999-12-10.kaminski.ham.txt   ham                24            4                 3
3        0001.2000-01-17.beck.ham.txt   ham              3486          559               248
4       0001.2000-06-06.lokay.ham.txt   ham              3603          536               207
5     0001.2001-02-07.kitchen.ham.txt   ham               322           48                18
6    0001.2001-04-02.williams.ham.txt   ham              1011          202               133
7      0002.1999-12-13.farmer.ham.txt   ham              4194          432               118
8     0002.2001-02-07.kitchen.ham.txt   ham               385           64                40
9  0002.2001-05-25.SA_and_HP.spam.txt  spam               990          170                80
10        0002.2003-12-18.GP.spam.txt  spam              1064          175                63
</code></pre>
</div>
<p>I next make my sparse matrix as a document term matrix (which is a special case of a simplet triplet matrix from the <code>slam</code> package), with a column for each word (having first limited myself to interesting words)</p>
<figure>
<pre><code data-lang="r"><span>used_words</span><span> </span><span>&lt;-</span><span> </span><span>enron_words</span><span> </span><span>%>%</span><span>
</span><span># knock out stop words:
</span><span> </span><span>anti_join</span><span>(</span><span>stop_words</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;word&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># knock out numerals, and words with only 2 or 1 letters:
</span><span> </span><span>mutate</span><span>(</span><span>word</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;[0-9]&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>word</span><span>),</span><span>
</span><span>wordlength</span><span> </span><span>=</span><span> </span><span>nchar</span><span>(</span><span>word</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>wordlength</span><span> </span><span>></span><span> </span><span>2</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>word</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>count</span><span> </span><span>=</span><span> </span><span>length</span><span>(</span><span>word</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span># knock out words used less than 10 times:
</span><span> </span><span>filter</span><span>(</span><span>count</span><span> </span><span>>=</span><span> </span><span>10</span><span>)</span><span>
</span><span>enron_dtm</span><span> </span><span>&lt;-</span><span> </span><span>enron_words</span><span> </span><span>%>%</span><span>
</span><span>right_join</span><span>(</span><span>used_words</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;word&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>cast_dtm</span><span>(</span><span>id</span><span>,</span><span> </span><span>word</span><span>,</span><span> </span><span>count</span><span>)</span><span> </span><span># we need a version of the dense data in the same order as the document-term-matrix, to do a sort of # manual join of the sparse matrix with the dense one later in H2O.
</span><span>rows</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>id</span><span> </span><span>=</span><span> </span><span>rownames</span><span>(</span><span>enron_dtm</span><span>))</span><span>
</span><span>enron_dense</span><span> </span><span>&lt;-</span><span> </span><span>left_join</span><span>(</span><span>rows</span><span>,</span><span> </span><span>enron_sum_total</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;id&quot;</span><span>)</span><span>
</span><span>expect_equal</span><span>(</span><span>nrow</span><span>(</span><span>enron_dtm</span><span>),</span><span> </span><span>nrow</span><span>(</span><span>enron_dense</span><span>))</span><span>
</span><span>expect_equal</span><span>(</span><span>rownames</span><span>(</span><span>enron_dtm</span><span>),</span><span> </span><span>enron_dense</span><span>$</span><span>id</span><span>)</span></code></pre>
</figure>
<p>Now we can load our two datasets onto an H2O cluster for analysis:</p>
<figure>
<pre><code data-lang="r"><span>#================import to h2o and join up there============
</span><span>h</span><span>2</span><span>o.init</span><span>(</span><span>nthreads</span><span> </span><span>=</span><span> </span><span>-1</span><span>,</span><span> </span><span>max_mem_size</span><span> </span><span>=</span><span> </span><span>&quot;8G&quot;</span><span>)</span><span>
</span><span># Load up the dense matrix with counts of stopwords etc:
</span><span>enron_dense_h2o</span><span> </span><span>&lt;-</span><span> </span><span>as.h2o</span><span>(</span><span>enron_dense</span><span>)</span><span>
</span><span># Load up the sparse matrix with columns for each word:
</span><span>thefile</span><span> </span><span>&lt;-</span><span> </span><span>tempfile</span><span>()</span><span>
</span><span>write_stm_svm</span><span>(</span><span>enron_dtm</span><span>,</span><span> </span><span>file</span><span> </span><span>=</span><span> </span><span>thefile</span><span>)</span><span>
</span><span>enron_sparse_h2o</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.uploadFile</span><span>(</span><span>thefile</span><span>,</span><span> </span><span>parse_type</span><span> </span><span>=</span><span> </span><span>&quot;SVMLight&quot;</span><span>)</span><span>
</span><span>unlink</span><span>(</span><span>thefile</span><span>)</span><span>
</span><span># Number of rows should be equal:
</span><span>expect_equal</span><span>(</span><span>nrow</span><span>(</span><span>enron_sparse_h2o</span><span>),</span><span> </span><span>nrow</span><span>(</span><span>enron_dtm</span><span>))</span><span>
</span><span># Number of columns should be 1 extra in H2O, dummy variable of labels (1) added by write_stm_svm:
</span><span>expect_equal</span><span>(</span><span>ncol</span><span>(</span><span>enron_sparse_h2o</span><span>),</span><span> </span><span>ncol</span><span>(</span><span>enron_dtm</span><span>)</span><span> </span><span>+</span><span> </span><span>1</span><span>)</span><span>
</span><span># First column should be the dummy labels = all one
</span><span>expect_equal</span><span>(</span><span>mean</span><span>(</span><span>enron_sparse_h2o</span><span>[</span><span> </span><span>,</span><span> </span><span>1</span><span>]),</span><span> </span><span>1</span><span>)</span><span>
</span><span>enron_fulldata</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.cbind</span><span>(</span><span>enron_sparse_h2o</span><span>,</span><span> </span><span>enron_dense_h2o</span><span>)</span><span>
</span><span>head</span><span>(</span><span>colnames</span><span>(</span><span>enron_fulldata</span><span>),</span><span> </span><span>10</span><span>)</span><span>
</span><span># Convert the target variable to a factor so h2o.glm and other modelling functions
# know what to do with it:
</span><span>enron_fulldata</span><span>[</span><span> </span><span>,</span><span> </span><span>&quot;SPAM&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>as.factor</span><span>(</span><span>enron_fulldata</span><span>[</span><span> </span><span>,</span><span> </span><span>&quot;SPAM&quot;</span><span>])</span></code></pre>
</figure>
<p>I now have an H2O frame with 33602 rows and 26592 columns; most of the columns representing words and the cells being counts; but some columns representing other variables such as number of stopwords.</p>
<h3>Analysis</h3>
<p>To give H2O a workout, I decided to fit four different types of models trying to understand which emails were ham and which spam:</p>
<ul>
<li>generalized linear model, with elastic net regularization to help cope with the large number of explanatory variables</li>
<li>random forest</li>
<li>gradient boosting machine</li>
<li>neural network</li>
</ul>
<p>I split the data into training, validation and testing subsets; with the idea that the validation set would be used for choosing tuning parameters, and the testing set used as a final comparison of the predictive power of the final models. As things turned out, I didn’t have patience to do much in the way of tuning. This probably counted against the latter three of my four models, because I’m pretty confident better performance would be possible with more careful choice of some of the meta parameters. Here’s the eventual results from my not-particularly-tuned models:</p>
<p><img alt="results" src="http://ellisp.github.io/img/0078-results.svg"></p>
<p>The humble generalized linear model (GLM) performs pretty well; outperformed clearly only by the neural network. The GLM has a big advantage in interpretability too. Here are the most important variables for the GLM in predicting spam (NEG means a higher count of the word means less likely to be spam)</p>
<div>
<pre><code>                   names coefficients sign       word
1                  C9729    1.1635213  NEG      enron
2                 C25535    0.6023054  NEG      vince
3                  C1996    0.5990230  NEG   attached
4                 C15413    0.4524011  NEG     louise
5                 C19891    0.3905246  NEG  questions
6                 C11478    0.2993239  NEG        gas
7  proportion_stop_words    0.2935112  NEG       &lt;NA>
8                 C12866    0.2774074  NEG  hourahead
9                  C7268    0.2600452  NEG      daren
10                C16257    0.2497282  NEG      meter
11                C12878    0.2439315  NEG    houston
12                C21441    0.2345008  NEG      sally
13                C16106    0.2179897  NEG    meeting
14                C12894    0.1965571  NEG        hpl
15                C16618    0.1909332  NEG     monday
16                C11270    0.1873195  NEG     friday
17                 C8553    0.1704185  NEG        doc
18                 C7386    0.1673093  NEG       deal
19                C21617    0.1636310  NEG   schedule
20                 C4185    0.1510748  NEG california
21                C12921    0.3695006  POS       http
22                C16624    0.2132104  POS      money
23                C22597    0.2034031  POS   software
24                C15074    0.1957970  POS       life
25                 C5394    0.1922659  POS      click
26                C17683    0.1915608  POS     online
27                C25462    0.1703109  POS     viagra
28                C16094    0.1605989  POS       meds
29                C21547    0.1583438  POS       save
30                 C9483    0.1498732  POS      email
</code></pre>
</div>
<p>So, who knew, emails containing the words “money”, “software”, “life”, “click”, “online”, “viagra” and “meds” are (or at least were in the time of Enron – things may have changed) more likely to be spam.</p>
<p>Here’s the code for the analysis all together:</p>
<figure>
<pre><code data-lang="r"><span>#=====================analysis in H2O=============
</span><span>
</span><span>#-----------prep------------------------
# names of the explanatory variables - all the columns in the sparse matrix (which are individual words)
# except the first one which is the dummy &quot;labels&quot; created just for the SVMLight format. And the
# four summary variables in the dense dataframe:
</span><span>xnames</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>colnames</span><span>(</span><span>enron_sparse_h2o</span><span>)[</span><span>-1</span><span>],</span><span> </span><span>&quot;number_characters&quot;</span><span>,</span><span> </span><span>&quot;number_words&quot;</span><span>,</span><span> </span><span>&quot;number_stop_words&quot;</span><span>,</span><span> </span><span>&quot;proportion_stop_words&quot;</span><span>)</span><span>
</span><span># A lookup table of the column names that refer to words to the words they actually mean.
# Useful when we look at variable performance down the track.
</span><span>wordcols</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>variable</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>enron_sparse_h2o</span><span>),</span><span>
</span><span>word</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>colnames</span><span>(</span><span>enron_dtm</span><span>))</span><span>
</span><span>)</span><span>
</span><span># The slower models (ie apart from glm) take ages for cross-validation so # we'll settle for single-split validation
</span><span>enron_split</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.splitFrame</span><span>(</span><span>enron_fulldata</span><span>,</span><span> </span><span>ratios</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.6</span><span>,</span><span> </span><span>0.2</span><span>))</span><span>
</span><span>dim</span><span>(</span><span>enron_split</span><span>[[</span><span>1</span><span>]])</span><span>
</span><span>#-------------------GLM---------------------
# Binomial GLM, with elastic net regularization. This is the minimal baseline sort of model.
# 200 seconds
</span><span>system.time</span><span>({</span><span>
</span><span>mod.glm</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.glm</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>xnames</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;SPAM&quot;</span><span>,</span><span> </span><span>training_frame</span><span> </span><span>=</span><span> </span><span>enron_split</span><span>[[</span><span>1</span><span>]],</span><span> </span><span>validation_frame</span><span> </span><span>=</span><span> </span><span>enron_split</span><span>[[</span><span>2</span><span>]],</span><span>
</span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;binomial&quot;</span><span>,</span><span>
</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>lambda_search</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>})</span><span>
</span><span>h</span><span>2</span><span>o.varimp</span><span>(</span><span>mod.glm</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>slice</span><span>(</span><span>1</span><span>:</span><span>30</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>wordcols</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;names&quot;</span><span> </span><span>=</span><span> </span><span>&quot;variable&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>sign</span><span>,</span><span> </span><span>desc</span><span>(</span><span>coefficients</span><span>))</span><span> </span><span>h</span><span>2</span><span>o.performance</span><span>(</span><span>mod.glm</span><span>,</span><span> </span><span>valid</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span># 131 / 6640
</span><span>
</span><span>#--------------------Random Forest-------------------
# with ntrees = 50, nfolds = 10, max_depth = 20, took 25 minutes to get # 5% through so I stopped it and went back to having a single validation frame.
# Can view progress by going to http://127.0.0.1:54321/flow/index.html
# 1340 seconds
</span><span>system.time</span><span>({</span><span>
</span><span>mod.rf</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.randomForest</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>xnames</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;SPAM&quot;</span><span>,</span><span> </span><span>training_frame</span><span> </span><span>=</span><span> </span><span>enron_split</span><span>[[</span><span>1</span><span>]],</span><span> </span><span>validation_frame</span><span> </span><span>=</span><span> </span><span>enron_split</span><span>[[</span><span>2</span><span>]],</span><span>
</span><span>ntrees</span><span> </span><span>=</span><span> </span><span>500</span><span>,</span><span> </span><span>max_depth</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span>
</span><span>stopping_tolerance</span><span> </span><span>=</span><span> </span><span>0.0001</span><span>,</span><span> </span><span>stopping_rounds</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>score_tree_interval</span><span> </span><span>=</span><span> </span><span>25</span><span>)</span><span>
</span><span>})</span><span>
</span><span># note from watching progress in flow, scoring every score_tree_interval models takes quite a lot of time.
# so I pushed out score_tree_interval. On current settings it will score
# the latest model against the validation frame every 25 trees, and stop if it hasn't improved since 3*25 trees ago.
# If score_tree_interval is a small number, it stops growing trees too quickly
</span><span>
</span><span>h</span><span>2</span><span>o.performance</span><span>(</span><span>mod.rf</span><span>,</span><span> </span><span>valid</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span># 172/6640 error rate
</span><span>
</span><span>#-------------------gradient boosting machine-----------------
# 1240 seconds
</span><span>system.time</span><span>({</span><span>
</span><span>mod.gbm</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.gbm</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>xnames</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;SPAM&quot;</span><span>,</span><span> </span><span>training_frame</span><span> </span><span>=</span><span> </span><span>enron_split</span><span>[[</span><span>1</span><span>]],</span><span> </span><span>validation_frame</span><span> </span><span>=</span><span> </span><span>enron_split</span><span>[[</span><span>2</span><span>]],</span><span>
</span><span>ntrees</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span> </span><span>max_depth</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span>
</span><span>stopping_tolerance</span><span> </span><span>=</span><span> </span><span>0.0001</span><span>,</span><span> </span><span>stopping_rounds</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>score_tree_interval</span><span> </span><span>=</span><span> </span><span>5</span><span>)</span><span>
</span><span>})</span><span>
</span><span>h</span><span>2</span><span>o.performance</span><span>(</span><span>mod.gbm</span><span>,</span><span> </span><span>valid</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span># 240/6640 error rate
</span><span>
</span><span>#-------------------artificial neural network------------------------
</span><span>
</span><span># 900 seconds; much faster when sparse = TRUE
</span><span>system.time</span><span>({</span><span>
</span><span>mod.dl</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.deeplearning</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>xnames</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;SPAM&quot;</span><span>,</span><span> </span><span>training_frame</span><span> </span><span>=</span><span> </span><span>enron_split</span><span>[[</span><span>1</span><span>]],</span><span> </span><span>validation_frame</span><span> </span><span>=</span><span> </span><span>enron_split</span><span>[[</span><span>2</span><span>]],</span><span>
</span><span>hidden</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>200</span><span>,</span><span> </span><span>200</span><span>),</span><span>
</span><span>stopping_tolerance</span><span> </span><span>=</span><span> </span><span>0.0001</span><span>,</span><span> </span><span>stopping_rounds</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>sparse</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>})</span><span>
</span><span>h</span><span>2</span><span>o.performance</span><span>(</span><span>mod.dl</span><span>,</span><span> </span><span>valid</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span># 82/6640
</span><span>
</span><span>#-----------------Naive Bayes - doesn't work--------------------
# Conditional probabilities won't fit in the driver node's memory (20.99 GB > 6.06GB)
</span><span>mod.nb</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.naiveBayes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>xnames</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;SPAM&quot;</span><span>,</span><span> </span><span>training_frame</span><span> </span><span>=</span><span> </span><span>enron_split</span><span>[[</span><span>1</span><span>]],</span><span> </span><span>validation_frame</span><span> </span><span>=</span><span> </span><span>enron_split</span><span>[[</span><span>2</span><span>]])</span><span>
</span><span>#==============presentation of results==========================
</span><span>
</span><span>perf</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>newdata</span><span>){</span><span>
</span><span>return</span><span>(</span><span>c</span><span>(</span><span>
</span><span>glm</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>h</span><span>2</span><span>o.confusionMatrix</span><span>(</span><span>mod.glm</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>newdata</span><span>)</span><span>$</span><span>Rate</span><span>[</span><span>1</span><span>]),</span><span>
</span><span>rf</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>h</span><span>2</span><span>o.confusionMatrix</span><span>(</span><span>mod.rf</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>newdata</span><span>)</span><span>$</span><span>Rate</span><span>[</span><span>1</span><span>]),</span><span>
</span><span>gbm</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>h</span><span>2</span><span>o.confusionMatrix</span><span>(</span><span>mod.gbm</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>newdata</span><span>)</span><span>$</span><span>Rate</span><span>[</span><span>1</span><span>]),</span><span>
</span><span>dl</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>h</span><span>2</span><span>o.confusionMatrix</span><span>(</span><span>mod.dl</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>newdata</span><span>)</span><span>$</span><span>Rate</span><span>[</span><span>1</span><span>])</span><span>
</span><span>))</span><span>
</span><span>}</span><span>
</span><span># this isn't the most efficient computing wise, because the performance on the training
# and validation sets could be extracted more directly but it is quick and easy to code:
</span><span>perfs</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>enron_split</span><span>,</span><span> </span><span>perf</span><span>)</span><span>
</span><span>perfsdf</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>value</span><span> </span><span>=</span><span> </span><span>unlist</span><span>(</span><span>perfs</span><span>),</span><span>
</span><span>model</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;Generalized Linear Model&quot;</span><span>,</span><span> </span><span>&quot;Random Forest&quot;</span><span>,</span><span> </span><span>&quot;Gradient Boosting Machine&quot;</span><span>,</span><span> </span><span>&quot;Deep Learning&quot;</span><span>),</span><span> </span><span>3</span><span>),</span><span>
</span><span>dataset</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;Training&quot;</span><span>,</span><span> </span><span>&quot;Validation&quot;</span><span>,</span><span> </span><span>&quot;Testing&quot;</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>4</span><span>),</span><span>
</span><span>abbrev</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>names</span><span>(</span><span>perfs</span><span>[[</span><span>1</span><span>]]),</span><span> </span><span>3</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>errors</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>str_sub</span><span>(</span><span>str_extract</span><span>(</span><span>value</span><span>,</span><span> </span><span>&quot;=[0-9]+&quot;</span><span>),</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>2</span><span>)),</span><span>
</span><span>denom</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>str_sub</span><span>(</span><span>str_extract</span><span>(</span><span>value</span><span>,</span><span> </span><span>&quot;/[0-9]+&quot;</span><span>),</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>2</span><span>)),</span><span>
</span><span>errorrate</span><span> </span><span>=</span><span> </span><span>errors</span><span> </span><span>/</span><span> </span><span>denom</span><span>,</span><span>
</span><span>model</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>model</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Deep Learning&quot;</span><span>,</span><span> </span><span>&quot;Generalized Linear Model&quot;</span><span>,</span><span> </span><span>&quot;Random Forest&quot;</span><span>,</span><span> </span><span>&quot;Gradient Boosting Machine&quot;</span><span>)),</span><span>
</span><span>dataset</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>dataset</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Training&quot;</span><span>,</span><span> </span><span>&quot;Validation&quot;</span><span>,</span><span> </span><span>&quot;Testing&quot;</span><span>)))</span><span>
</span><span>perfsdf</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>errorrate</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>dataset</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>abbrev</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>model</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_path</span><span>(</span><span>aes</span><span>(</span><span>group</span><span> </span><span>=</span><span> </span><span>abbrev</span><span>),</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_colour_brewer</span><span>(</span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;Set1&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;Error rate&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Error rates detecting ham from spam&quot;</span><span>,</span><span>
</span><span>&quot;Enron email dataset, four different statistical learning methods&quot;</span><span>)</span></code></pre>
</figure>
<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://ellisp.github.io/blog/2017/02/18/svmlite"> Peter's stats stuff - 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/NnVK4UPRPpQ"></a></div>