<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/dealing-with-unbalanced-data-in-machine-learning/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shiring.github.io/machine_learning/2017/04/02/unbalanced"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="https://shiring.github.io/machine_learning/2017/03/31/webinar_code">In my last post</a>, where I shared the code that I used to produce an example analysis to go along with my <a target="_blank" rel="nofollow" href="https://github.com/ShirinG/Webinar_ISDS/blob/master/Webinar_slides.pdf">webinar on building meaningful models for disease prediction</a>, I mentioned that it is advised to consider over- or under-sampling when you have unbalanced data sets. Because my focus in this webinar was on evaluating model performance, I did not want to add an additional layer of complexity and therefore did not further discuss how to specifically deal with unbalanced data.</p>
<p>But because I had gotten a few questions regarding this, I thought it would be worthwhile to explain over- and under-sampling techniques in more detail and show how you can very easily implement them with <code>caret</code>.</p>
<div>
<pre><code><span>library</span><span>(</span><span>caret</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<h3>Unbalanced data</h3>
<p>In this context, unbalanced data refers to classification problems where we have unequal instances for different classes. Having unbalanced data is actually very common in general, but it is especially prevalent when working with disease data where we usually have more healthy control samples than disease cases. Even more extreme unbalance is seen with fraud detection, where e.g. most credit card uses are okay and only very few will be fraudulent. In the <a target="_blank" rel="nofollow" href="https://shiring.github.io/machine_learning/2017/03/31/webinar_code">example I used for my webinar</a>, a breast cancer dataset, we had about twice as many benign than malignant samples.</p>
<div>
<pre><code><span>summary</span><span>(</span><span>bc_data</span><span>$</span><span>classes</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##    benign malignant 
##       458       241
</code></pre>
</div>
<p></p>
<h3>Why is unbalanced data a problem in machine learning?</h3>
<p>Most machine learning classification algorithms are sensitive to unbalance in the predictor classes. Let’s consider an even more extreme example than our breast cancer dataset: assume we had 10 malignant vs 90 benign samples. A machine learning model that has been trained and tested on such a dataset could now predict “benign” for all samples and still gain a very high accuracy. An unbalanced dataset will bias the prediction model towards the more common class!</p>
<p></p>
<h3>How to balance data for modeling</h3>
<p>The basic theoretical concepts behind over- and under-sampling are very simple:</p>
<ul>
<li>
<p>With under-sampling, we randomly select a subset of samples from the class with more instances to match the number of samples coming from each class. In our example, we would randomly pick 241 out of the 458 benign cases. The main disadvantage of under-sampling is that we loose potentially relevant information from the left-out samples.</p>
</li>
<li>
<p>With oversampling, we randomly duplicate samples from the class with fewer instances or we generate additional instances based on the data that we have, so as to match the number of samples in each class. While we avoid loosing information with this approach, we also run the risk of overfitting our model as we are more likely to get the same samples in the training and in the test data, i.e. the test data is no longer independent from training data. This would lead to an overestimation of our model’s performance and generalizability.</p>
</li>
</ul>
<p>In reality though, we should not simply perform over- or under-sampling on our training data and then run the model. We need to account for cross-validation and perform over- or under-sampling on each fold independently to get an honest estimate of model performance!</p>
<p></p>
<h4>Modeling the original unbalanced data</h4>
<p>Here is the same model I used in my webinar example: I randomly divide the data into training and test sets (stratified by class) and perform Random Forest modeling with 10 x 10 repeated cross-validation. Final model performance is then measured on the test set.</p>
<div>
<pre><code><span>set.seed</span><span>(</span><span>42</span><span>)</span><span>
</span><span>index</span><span> </span><span>&lt;-</span><span> </span><span>createDataPartition</span><span>(</span><span>bc_data</span><span>$</span><span>classes</span><span>,</span><span> </span><span>p</span><span> </span><span>=</span><span> </span><span>0.7</span><span>,</span><span> </span><span>list</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>train_data</span><span> </span><span>&lt;-</span><span> </span><span>bc_data</span><span>[</span><span>index</span><span>,</span><span> </span><span>]</span><span>
</span><span>test_data</span><span> </span><span>&lt;-</span><span> </span><span>bc_data</span><span>[</span><span>-</span><span>index</span><span>,</span><span> </span><span>]</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>set.seed</span><span>(</span><span>42</span><span>)</span><span>
</span><span>model_rf</span><span> </span><span>&lt;-</span><span> </span><span>caret</span><span>::</span><span>train</span><span>(</span><span>classes</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>train_data</span><span>,</span><span>
</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;rf&quot;</span><span>,</span><span>
</span><span>preProcess</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;scale&quot;</span><span>,</span><span> </span><span>&quot;center&quot;</span><span>),</span><span>
</span><span>trControl</span><span> </span><span>=</span><span> </span><span>trainControl</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;repeatedcv&quot;</span><span>,</span><span> </span><span>number</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>repeats</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>verboseIter</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>))</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>final</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>actual</span><span> </span><span>=</span><span> </span><span>test_data</span><span>$</span><span>classes</span><span>,</span><span>
</span><span>predict</span><span>(</span><span>model_rf</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>test_data</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;prob&quot;</span><span>))</span><span>
</span><span>final</span><span>$</span><span>predict</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>final</span><span>$</span><span>benign</span><span> </span><span>></span><span> </span><span>0.5</span><span>,</span><span> </span><span>&quot;benign&quot;</span><span>,</span><span> </span><span>&quot;malignant&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>cm_original</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>final</span><span>$</span><span>predict</span><span>,</span><span> </span><span>test_data</span><span>$</span><span>classes</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<h4>Under-sampling</h4>
<p>Luckily, <code>caret</code> makes it very easy to incorporate over- and under-sampling techniques with cross-validation resampling. We can simply add the <code>sampling</code> option to our <code>trainControl</code> and choose <code>down</code> for under- (also called down-) sampling. The rest stays the same as with our original model.</p>
<div>
<pre><code><span>ctrl</span><span> </span><span>&lt;-</span><span> </span><span>trainControl</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;repeatedcv&quot;</span><span>,</span><span> </span><span>number</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>repeats</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>verboseIter</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>sampling</span><span> </span><span>=</span><span> </span><span>&quot;down&quot;</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>42</span><span>)</span><span>
</span><span>model_rf_under</span><span> </span><span>&lt;-</span><span> </span><span>caret</span><span>::</span><span>train</span><span>(</span><span>classes</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>train_data</span><span>,</span><span>
</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;rf&quot;</span><span>,</span><span>
</span><span>preProcess</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;scale&quot;</span><span>,</span><span> </span><span>&quot;center&quot;</span><span>),</span><span>
</span><span>trControl</span><span> </span><span>=</span><span> </span><span>ctrl</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>final_under</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>actual</span><span> </span><span>=</span><span> </span><span>test_data</span><span>$</span><span>classes</span><span>,</span><span>
</span><span>predict</span><span>(</span><span>model_rf_under</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>test_data</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;prob&quot;</span><span>))</span><span>
</span><span>final_under</span><span>$</span><span>predict</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>final_under</span><span>$</span><span>benign</span><span> </span><span>></span><span> </span><span>0.5</span><span>,</span><span> </span><span>&quot;benign&quot;</span><span>,</span><span> </span><span>&quot;malignant&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>cm_under</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>final_under</span><span>$</span><span>predict</span><span>,</span><span> </span><span>test_data</span><span>$</span><span>classes</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<h4>Oversampling</h4>
<p>For over- (also called up-) sampling we simply specify <code>sampling = &quot;up&quot;</code>.</p>
<div>
<pre><code><span>ctrl</span><span> </span><span>&lt;-</span><span> </span><span>trainControl</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;repeatedcv&quot;</span><span>,</span><span> </span><span>number</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>repeats</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>verboseIter</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>sampling</span><span> </span><span>=</span><span> </span><span>&quot;up&quot;</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>42</span><span>)</span><span>
</span><span>model_rf_over</span><span> </span><span>&lt;-</span><span> </span><span>caret</span><span>::</span><span>train</span><span>(</span><span>classes</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>train_data</span><span>,</span><span>
</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;rf&quot;</span><span>,</span><span>
</span><span>preProcess</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;scale&quot;</span><span>,</span><span> </span><span>&quot;center&quot;</span><span>),</span><span>
</span><span>trControl</span><span> </span><span>=</span><span> </span><span>ctrl</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>final_over</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>actual</span><span> </span><span>=</span><span> </span><span>test_data</span><span>$</span><span>classes</span><span>,</span><span>
</span><span>predict</span><span>(</span><span>model_rf_over</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>test_data</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;prob&quot;</span><span>))</span><span>
</span><span>final_over</span><span>$</span><span>predict</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>final_over</span><span>$</span><span>benign</span><span> </span><span>></span><span> </span><span>0.5</span><span>,</span><span> </span><span>&quot;benign&quot;</span><span>,</span><span> </span><span>&quot;malignant&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>cm_over</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>final_over</span><span>$</span><span>predict</span><span>,</span><span> </span><span>test_data</span><span>$</span><span>classes</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<h4>ROSE</h4>
<p>Besides over- and under-sampling, there are hybrid methods that combine under-sampling with the generation of additional data. Two of the most popular are ROSE and SMOTE.</p>
<blockquote>
<p>From Nicola Lunardon, Giovanna Menardi and Nicola Torelli’s <strong>“ROSE: A Package for Binary Imbalanced Learning”</strong> (R Journal, 2014, Vol. 6 Issue 1, p. 79): “The ROSE package provides functions to deal with binary classification problems in the presence of imbalanced classes. Artificial balanced samples are generated according to a smoothed bootstrap approach and allow for aiding both the phases of estimation and accuracy evaluation of a binary classifier in the presence of a rare class. Functions that implement more traditional remedies for the class imbalance and different metrics to evaluate accuracy are also provided. These are estimated by holdout, bootstrap, or cross-validation methods.”</p>
</blockquote>
<p>You implement them the same way as before, this time choosing <code>sampling = &quot;rose&quot;</code>…</p>
<div>
<pre><code><span>ctrl</span><span> </span><span>&lt;-</span><span> </span><span>trainControl</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;repeatedcv&quot;</span><span>,</span><span> </span><span>number</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>repeats</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>verboseIter</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>sampling</span><span> </span><span>=</span><span> </span><span>&quot;rose&quot;</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>42</span><span>)</span><span>
</span><span>model_rf_rose</span><span> </span><span>&lt;-</span><span> </span><span>caret</span><span>::</span><span>train</span><span>(</span><span>classes</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>train_data</span><span>,</span><span>
</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;rf&quot;</span><span>,</span><span>
</span><span>preProcess</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;scale&quot;</span><span>,</span><span> </span><span>&quot;center&quot;</span><span>),</span><span>
</span><span>trControl</span><span> </span><span>=</span><span> </span><span>ctrl</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>final_rose</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>actual</span><span> </span><span>=</span><span> </span><span>test_data</span><span>$</span><span>classes</span><span>,</span><span>
</span><span>predict</span><span>(</span><span>model_rf_rose</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>test_data</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;prob&quot;</span><span>))</span><span>
</span><span>final_rose</span><span>$</span><span>predict</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>final_rose</span><span>$</span><span>benign</span><span> </span><span>></span><span> </span><span>0.5</span><span>,</span><span> </span><span>&quot;benign&quot;</span><span>,</span><span> </span><span>&quot;malignant&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>cm_rose</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>final_rose</span><span>$</span><span>predict</span><span>,</span><span> </span><span>test_data</span><span>$</span><span>classes</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<h4>SMOTE</h4>
<p>… or by choosing <code>sampling = &quot;smote&quot;</code> in the <code>trainControl</code> settings.</p>
<blockquote>
<p>From Nitesh V. Chawla, Kevin W. Bowyer, Lawrence O. Hall and W. Philip Kegelmeyer’s <strong>“SMOTE: Synthetic Minority Over-sampling Technique”</strong> (Journal of Artificial Intelligence Research, 2002, Vol. 16, pp. 321–357): “This paper shows that a combination of our method of over-sampling the minority (abnormal) class and under-sampling the majority (normal) class can achieve better classifier performance (in ROC space) than only under-sampling the majority class. This paper also shows that a combination of our method of over-sampling the minority class and under-sampling the majority class can achieve better classifier performance (in ROC space) than varying the loss ratios in Ripper or class priors in Naive Bayes. Our method of over-sampling the minority class involves creating synthetic minority class examples.”</p>
</blockquote>
<div>
<pre><code><span>ctrl</span><span> </span><span>&lt;-</span><span> </span><span>trainControl</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;repeatedcv&quot;</span><span>,</span><span> </span><span>number</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>repeats</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>verboseIter</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>sampling</span><span> </span><span>=</span><span> </span><span>&quot;smote&quot;</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>42</span><span>)</span><span>
</span><span>model_rf_smote</span><span> </span><span>&lt;-</span><span> </span><span>caret</span><span>::</span><span>train</span><span>(</span><span>classes</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>train_data</span><span>,</span><span>
</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;rf&quot;</span><span>,</span><span>
</span><span>preProcess</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;scale&quot;</span><span>,</span><span> </span><span>&quot;center&quot;</span><span>),</span><span>
</span><span>trControl</span><span> </span><span>=</span><span> </span><span>ctrl</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>final_smote</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>actual</span><span> </span><span>=</span><span> </span><span>test_data</span><span>$</span><span>classes</span><span>,</span><span>
</span><span>predict</span><span>(</span><span>model_rf_smote</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>test_data</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;prob&quot;</span><span>))</span><span>
</span><span>final_smote</span><span>$</span><span>predict</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>final_smote</span><span>$</span><span>benign</span><span> </span><span>></span><span> </span><span>0.5</span><span>,</span><span> </span><span>&quot;benign&quot;</span><span>,</span><span> </span><span>&quot;malignant&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>cm_smote</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>final_smote</span><span>$</span><span>predict</span><span>,</span><span> </span><span>test_data</span><span>$</span><span>classes</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<h3>Predictions</h3>
<p>Now let’s compare the predictions of all these models:</p>
<div>
<pre><code><span>models</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>original</span><span> </span><span>=</span><span> </span><span>model_rf</span><span>,</span><span>
</span><span>under</span><span> </span><span>=</span><span> </span><span>model_rf_under</span><span>,</span><span>
</span><span>over</span><span> </span><span>=</span><span> </span><span>model_rf_over</span><span>,</span><span>
</span><span>smote</span><span> </span><span>=</span><span> </span><span>model_rf_smote</span><span>,</span><span>
</span><span>rose</span><span> </span><span>=</span><span> </span><span>model_rf_rose</span><span>)</span><span>
</span><span>resampling</span><span> </span><span>&lt;-</span><span> </span><span>resamples</span><span>(</span><span>models</span><span>)</span><span>
</span><span>bwplot</span><span>(</span><span>resampling</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/shiring.github.io/machine_learning/2017/04/02/unbalanced_files/figure-markdown_github/unnamed-chunk-30-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>comparison</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>model</span><span> </span><span>=</span><span> </span><span>names</span><span>(</span><span>models</span><span>),</span><span>
</span><span>Sensitivity</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>NA</span><span>,</span><span> </span><span>length</span><span>(</span><span>models</span><span>)),</span><span>
</span><span>Specificity</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>NA</span><span>,</span><span> </span><span>length</span><span>(</span><span>models</span><span>)),</span><span>
</span><span>Precision</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>NA</span><span>,</span><span> </span><span>length</span><span>(</span><span>models</span><span>)),</span><span>
</span><span>Recall</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>NA</span><span>,</span><span> </span><span>length</span><span>(</span><span>models</span><span>)),</span><span>
</span><span>F</span><span>1</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>NA</span><span>,</span><span> </span><span>length</span><span>(</span><span>models</span><span>)))</span><span>
</span><span>for</span><span> </span><span>(</span><span>name</span><span> </span><span>in</span><span> </span><span>names</span><span>(</span><span>models</span><span>))</span><span> </span><span>{</span><span>
</span><span>model</span><span> </span><span>&lt;-</span><span> </span><span>get</span><span>(</span><span>paste0</span><span>(</span><span>&quot;cm_&quot;</span><span>,</span><span> </span><span>name</span><span>))</span><span>
</span><span>comparison</span><span>[</span><span>comparison</span><span>$</span><span>model</span><span> </span><span>==</span><span> </span><span>name</span><span>,</span><span> </span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>comparison</span><span>,</span><span> </span><span>model</span><span> </span><span>==</span><span> </span><span>name</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Sensitivity</span><span> </span><span>=</span><span> </span><span>model</span><span>$</span><span>byClass</span><span>[</span><span>&quot;Sensitivity&quot;</span><span>],</span><span>
</span><span>Specificity</span><span> </span><span>=</span><span> </span><span>model</span><span>$</span><span>byClass</span><span>[</span><span>&quot;Specificity&quot;</span><span>],</span><span>
</span><span>Precision</span><span> </span><span>=</span><span> </span><span>model</span><span>$</span><span>byClass</span><span>[</span><span>&quot;Precision&quot;</span><span>],</span><span>
</span><span>Recall</span><span> </span><span>=</span><span> </span><span>model</span><span>$</span><span>byClass</span><span>[</span><span>&quot;Recall&quot;</span><span>],</span><span>
</span><span>F</span><span>1</span><span> </span><span>=</span><span> </span><span>model</span><span>$</span><span>byClass</span><span>[</span><span>&quot;F1&quot;</span><span>])</span><span>
</span><span>}</span><span>
</span><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>comparison</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span> </span><span>Sensitivity</span><span>:</span><span>F</span><span>1</span><span>)</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>x</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>y</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>model</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_jitter</span><span>(</span><span>width</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i0.wp.com/shiring.github.io/machine_learning/2017/04/02/unbalanced_files/figure-markdown_github/unnamed-chunk-32-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>With this small dataset, we can already see how the different techniques can influence model performance. Sensitivity (or recall) describes the proportion of benign cases that have been predicted correctly, while specificity describes the proportion of malignant cases that have been predicted correctly. Precision describes the true positives, i.e. the proportion of benign predictions that were actual from benign samples. F1 is the weighted average of precision and sensitivity/ recall.</p>
<p>Here, all four methods improved specificity and precision compared to the original model.<br>
Under-sampling, over-sampling and ROSE additionally improved precision and the F1 score.</p>
<p></p>
<p>This post shows a simple example of how to correct for unbalance in datasets for machine learning. For more advanced instructions and potential caveats with these techniques, check out the excellent <a target="_blank" rel="nofollow" href="https://topepo.github.io/caret/subsampling-for-class-imbalances.html">caret documentation</a>.</p>
<hr>
<p>If you are interested in more machine learning posts, check out <a target="_blank" rel="nofollow" href="https://shiring.github.io/categories.html#machine_learning-ref">the category listing for <strong>machine_learning</strong> on my blog</a>.</p>
<hr>
<p></p>
<div>
<pre><code><span>sessionInfo</span><span>()</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## R version 3.3.3 (2017-03-06)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Sierra 10.12.3
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] tidyr_0.6.1         dplyr_0.5.0         randomForest_4.6-12
## [4] caret_6.0-73        ggplot2_2.2.1       lattice_0.20-34    
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.9        nloptr_1.0.4       plyr_1.8.4        
##  [4] class_7.3-14       iterators_1.0.8    tools_3.3.3       
##  [7] digest_0.6.12      lme4_1.1-12        evaluate_0.10     
## [10] tibble_1.2         gtable_0.2.0       nlme_3.1-131      
## [13] mgcv_1.8-17        Matrix_1.2-8       foreach_1.4.3     
## [16] DBI_0.5-1          yaml_2.1.14        parallel_3.3.3    
## [19] SparseM_1.74       e1071_1.6-8        stringr_1.2.0     
## [22] knitr_1.15.1       MatrixModels_0.4-1 stats4_3.3.3      
## [25] rprojroot_1.2      grid_3.3.3         nnet_7.3-12       
## [28] R6_2.2.0           rmarkdown_1.3      minqa_1.2.4       
## [31] reshape2_1.4.2     car_2.1-4          magrittr_1.5      
## [34] backports_1.0.5    scales_0.4.1       codetools_0.2-15  
## [37] ModelMetrics_1.1.0 htmltools_0.3.5    MASS_7.3-45       
## [40] splines_3.3.3      assertthat_0.1     pbkrtest_0.4-6    
## [43] colorspace_1.3-2   labeling_0.3       quantreg_5.29     
## [46] stringi_1.1.2      lazyeval_0.2.0     munsell_0.4.3
</code></pre>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://shiring.github.io/machine_learning/2017/04/02/unbalanced"> Shirin's playgRound</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/LrSIUoG58jY"></a></div>