<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/feature-selection-in-machine-learning-breast-cancer-datasets/"></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/01/15/rfe_ga_post"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Machine learning uses so called features (i.e. variables or attributes) to generate predictive models. Using a suitable combination of features is essential for obtaining high precision and accuracy. Because too many (unspecific) features pose the problem of overfitting the model, we generally want to restrict the features in our models to those, that are most relevant for the response variable we want to predict. Using as few features as possible will also reduce the complexity of our models, which means it needs less time and computer power to run and is easier to understand.</p>
<p>There are several ways to identify how much each feature contributes to the model and to restrict the number of selected features. Here, I am going to examine the effect of feature selection via</p>
<ul>
<li>Correlation,</li>
<li>Recursive Feature Elimination (RFE) and</li>
<li>Genetic Algorithm (GA)</li>
</ul>
<p>on Random Forest models.</p>
<p>Additionally, I want to know how different data properties affect the influence of these feature selection methods on the outcome. For that I am using three breast cancer datasets, one of which has few features; the other two are larger but differ in how well the outcome clusters in PCA.</p>
<p>Based on my comparisons of the correlation method, RFE and GA, I would conclude that for Random Forest models</p>
<ul>
<li>removing highly correlated features isn’t a generally suitable method,</li>
<li>GA produced the best models in this example but is impractical for everyday use-cases with many features because it takes a lot of time to run with sufficient generations and individuals and</li>
<li>data that doesn’t allow a good classification to begin with (because the features are not very distinct between classes) don’t necessarily benefit from feature selection.</li>
</ul>
<p>My conclusions are of course not to be generalized to any ol’ data you are working with: There are many more feature selection methods and I am only looking at a limited number of datasets and only at their influence on Random Forest models. But even this small example shows how different features and parameters can influence your predictions. With machine learning, there is no “one size fits all”! It is always worthwhile to take a good hard look at your data, get acquainted with its quirks and properties before you even think about models and algorithms. And once you’ve got a feel for your data, investing the time and effort to compare different feature selection methods (or engineered features), model parameters and – finally – different machine learning algorithms can make a big difference!</p>
<p><img src="https://i0.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-86-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<hr>
<p></p>
<h2>Breast Cancer Wisconsin (Diagnostic) Dataset</h2>
<p>The data I am going to use to explore feature selection methods is the Breast Cancer Wisconsin (Diagnostic) Dataset:</p>
<blockquote>
<p>W.N. Street, W.H. Wolberg and O.L. Mangasarian. Nuclear feature extraction for breast tumor diagnosis. IS&amp;T/SPIE 1993 International Symposium on Electronic Imaging: Science and Technology, volume 1905, pages 861-870, San Jose, CA, 1993.</p>
</blockquote>
<blockquote>
<p>O.L. Mangasarian, W.N. Street and W.H. Wolberg. Breast cancer diagnosis and prognosis via linear programming. Operations Research, 43(4), pages 570-577, July-August 1995.</p>
</blockquote>
<blockquote>
<p>W.H. Wolberg, W.N. Street, and O.L. Mangasarian. Machine learning techniques to diagnose breast cancer from fine-needle aspirates. Cancer Letters 77 (1994) 163-171.</p>
</blockquote>
<blockquote>
<p>W.H. Wolberg, W.N. Street, and O.L. Mangasarian. Image analysis and machine learning applied to breast cancer diagnosis and prognosis. Analytical and Quantitative Cytology and Histology, Vol. 17 No. 2, pages 77-87, April 1995.</p>
</blockquote>
<blockquote>
<p>W.H. Wolberg, W.N. Street, D.M. Heisey, and O.L. Mangasarian. Computerized breast cancer diagnosis and prognosis from fine needle aspirates. Archives of Surgery 1995;130:511-516.</p>
</blockquote>
<blockquote>
<p>W.H. Wolberg, W.N. Street, D.M. Heisey, and O.L. Mangasarian. Computer-derived nuclear features distinguish malignant from benign breast cytology. Human Pathology, 26:792–796, 1995.</p>
</blockquote>
<p>The data was downloaded from the <a target="_blank" rel="nofollow" href="http://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+%28Diagnostic%29">UC Irvine Machine Learning Repository</a>. The features in these datasets characterise cell nucleus properties and were generated from image analysis of <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Fine-needle_aspiration">fine needle aspirates (FNA)</a> of breast masses.</p>
<p>Included are three datasets. The first dataset is small with only 9 features, the other two datasets have 30 and 33 features and vary in how strongly the two predictor classes cluster in PCA. I want to explore the effect of different feature selection methods on datasets with these different properties.</p>
<p>But first, I want to get to know the data I am working with.</p>
<p></p>
<h3>Breast cancer dataset 1</h3>
<p>The first dataset looks at the predictor classes:</p>
<ul>
<li>malignant or</li>
<li>benign breast mass.</li>
</ul>
<p>The phenotypes for characterisation are:</p>
<ul>
<li>Sample ID (code number)</li>
<li>Clump thickness</li>
<li>Uniformity of cell size</li>
<li>Uniformity of cell shape</li>
<li>Marginal adhesion</li>
<li>Single epithelial cell size</li>
<li>Number of bare nuclei</li>
<li>Bland chromatin</li>
<li>Number of normal nuclei</li>
<li>Mitosis</li>
<li>Classes, i.e. diagnosis</li>
</ul>
<p>Missing values are imputed with the <em>mice</em> package.</p>
<div>
<pre><code><span>bc_data</span><span> </span><span>&lt;-</span><span> </span><span>read.table</span><span>(</span><span>&quot;breast-cancer-wisconsin.data.txt&quot;</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;,&quot;</span><span>)</span><span>
</span><span>colnames</span><span>(</span><span>bc_data</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;sample_code_number&quot;</span><span>,</span><span> </span><span>&quot;clump_thickness&quot;</span><span>,</span><span> </span><span>&quot;uniformity_of_cell_size&quot;</span><span>,</span><span> </span><span>&quot;uniformity_of_cell_shape&quot;</span><span>,</span><span> </span><span>&quot;marginal_adhesion&quot;</span><span>,</span><span> </span><span>&quot;single_epithelial_cell_size&quot;</span><span>,</span><span> </span><span>&quot;bare_nuclei&quot;</span><span>,</span><span> </span><span>&quot;bland_chromatin&quot;</span><span>,</span><span> </span><span>&quot;normal_nucleoli&quot;</span><span>,</span><span> </span><span>&quot;mitosis&quot;</span><span>,</span><span> </span><span>&quot;classes&quot;</span><span>)</span><span>
</span><span>bc_data</span><span>$</span><span>classes</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>bc_data</span><span>$</span><span>classes</span><span> </span><span>==</span><span> </span><span>&quot;2&quot;</span><span>,</span><span> </span><span>&quot;benign&quot;</span><span>,</span><span>
</span><span>ifelse</span><span>(</span><span>bc_data</span><span>$</span><span>classes</span><span> </span><span>==</span><span> </span><span>&quot;4&quot;</span><span>,</span><span> </span><span>&quot;malignant&quot;</span><span>,</span><span> </span><span>NA</span><span>))</span><span>
</span><span>bc_data</span><span>[</span><span>bc_data</span><span> </span><span>==</span><span> </span><span>&quot;?&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>NA</span><span>
</span><span># how many NAs are in the data
</span><span>length</span><span>(</span><span>which</span><span>(</span><span>is.na</span><span>(</span><span>bc_data</span><span>)))</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 16
</code></pre>
</div>
<div>
<pre><code><span># impute missing data
</span><span>library</span><span>(</span><span>mice</span><span>)</span><span>
</span><span>bc_data</span><span>[,</span><span>2</span><span>:</span><span>10</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>apply</span><span>(</span><span>bc_data</span><span>[,</span><span> </span><span>2</span><span>:</span><span>10</span><span>],</span><span> </span><span>2</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>as.numeric</span><span>(</span><span>as.character</span><span>(</span><span>x</span><span>)))</span><span>
</span><span>dataset_impute</span><span> </span><span>&lt;-</span><span> </span><span>mice</span><span>(</span><span>bc_data</span><span>[,</span><span> </span><span>2</span><span>:</span><span>10</span><span>],</span><span> </span><span>print</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>bc_data</span><span> </span><span>&lt;-</span><span> </span><span>cbind</span><span>(</span><span>bc_data</span><span>[,</span><span> </span><span>11</span><span>,</span><span> </span><span>drop</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>],</span><span> </span><span>mice</span><span>::</span><span>complete</span><span>(</span><span>dataset_impute</span><span>,</span><span> </span><span>1</span><span>))</span><span>
</span><span>bc_data</span><span>$</span><span>classes</span><span> </span><span>&lt;-</span><span> </span><span>as.factor</span><span>(</span><span>bc_data</span><span>$</span><span>classes</span><span>)</span><span>
</span><span># how many benign and malignant cases are there?
</span><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>
<div>
<pre><code><span>str</span><span>(</span><span>bc_data</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## 'data.frame':    699 obs. of  10 variables:
##  $ classes                    : Factor w/ 2 levels &quot;benign&quot;,&quot;malignant&quot;: 1 1 1 1 1 2 1 1 1 1 ...
##  $ clump_thickness            : num  5 5 3 6 4 8 1 2 2 4 ...
##  $ uniformity_of_cell_size    : num  1 4 1 8 1 10 1 1 1 2 ...
##  $ uniformity_of_cell_shape   : num  1 4 1 8 1 10 1 2 1 1 ...
##  $ marginal_adhesion          : num  1 5 1 1 3 8 1 1 1 1 ...
##  $ single_epithelial_cell_size: num  2 7 2 3 2 7 2 2 2 2 ...
##  $ bare_nuclei                : num  1 10 2 4 1 10 10 1 1 1 ...
##  $ bland_chromatin            : num  3 3 3 3 3 9 3 3 1 2 ...
##  $ normal_nucleoli            : num  1 2 1 7 1 7 1 1 1 1 ...
##  $ mitosis                    : num  1 1 1 1 1 1 1 1 5 1 ...
</code></pre>
</div>
<p></p>
<h3>Breast cancer dataset 2</h3>
<p>The second dataset looks again at the predictor classes:</p>
<ul>
<li>M: malignant or</li>
<li>B: benign breast mass.</li>
</ul>
<p>The first two columns give:</p>
<ul>
<li>Sample ID</li>
<li>Classes, i.e. diagnosis</li>
</ul>
<p>For each cell nucleus, the following ten characteristics were measured:</p>
<ul>
<li>Radius (mean of all distances from the center to points on the perimeter)</li>
<li>Texture (standard deviation of gray-scale values)</li>
<li>Perimeter</li>
<li>Area</li>
<li>Smoothness (local variation in radius lengths)</li>
<li>Compactness (perimeter^2 / area – 1.0)</li>
<li>Concavity (severity of concave portions of the contour)</li>
<li>Concave points (number of concave portions of the contour)</li>
<li>Symmetry</li>
<li>Fractal dimension (“coastline approximation” – 1)</li>
</ul>
<p>For each characteristic three measures are given:</p>
<ul>
<li>Mean</li>
<li>Standard error</li>
<li>Largest/ “worst”</li>
</ul>
<div>
<pre><code><span>bc_data_2</span><span> </span><span>&lt;-</span><span> </span><span>read.table</span><span>(</span><span>&quot;wdbc.data.txt&quot;</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;,&quot;</span><span>)</span><span>
</span><span>phenotypes</span><span> </span><span>&lt;-</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;radius&quot;</span><span>,</span><span> </span><span>&quot;texture&quot;</span><span>,</span><span> </span><span>&quot;perimeter&quot;</span><span>,</span><span> </span><span>&quot;area&quot;</span><span>,</span><span> </span><span>&quot;smoothness&quot;</span><span>,</span><span> </span><span>&quot;compactness&quot;</span><span>,</span><span> </span><span>&quot;concavity&quot;</span><span>,</span><span> </span><span>&quot;concave_points&quot;</span><span>,</span><span> </span><span>&quot;symmetry&quot;</span><span>,</span><span> </span><span>&quot;fractal_dimension&quot;</span><span>),</span><span> </span><span>3</span><span>)</span><span>
</span><span>types</span><span> </span><span>&lt;-</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;mean&quot;</span><span>,</span><span> </span><span>&quot;se&quot;</span><span>,</span><span> </span><span>&quot;largest_worst&quot;</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>10</span><span>)</span><span>
</span><span>colnames</span><span>(</span><span>bc_data_2</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;ID&quot;</span><span>,</span><span> </span><span>&quot;diagnosis&quot;</span><span>,</span><span> </span><span>paste</span><span>(</span><span>phenotypes</span><span>,</span><span> </span><span>types</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>))</span><span>
</span><span># how many NAs are in the data
</span><span>length</span><span>(</span><span>which</span><span>(</span><span>is.na</span><span>(</span><span>bc_data_2</span><span>)))</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 0
</code></pre>
</div>
<div>
<pre><code><span># how many benign and malignant cases are there?
</span><span>summary</span><span>(</span><span>bc_data_2</span><span>$</span><span>diagnosis</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##   B   M 
## 357 212
</code></pre>
</div>
<div>
<pre><code><span>str</span><span>(</span><span>bc_data_2</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## 'data.frame':    569 obs. of  32 variables:
##  $ ID                             : int  842302 842517 84300903 84348301 84358402 843786 844359 84458202 844981 84501001 ...
##  $ diagnosis                      : Factor w/ 2 levels &quot;B&quot;,&quot;M&quot;: 2 2 2 2 2 2 2 2 2 2 ...
##  $ radius_mean                    : num  18 20.6 19.7 11.4 20.3 ...
##  $ texture_mean                   : num  10.4 17.8 21.2 20.4 14.3 ...
##  $ perimeter_mean                 : num  122.8 132.9 130 77.6 135.1 ...
##  $ area_mean                      : num  1001 1326 1203 386 1297 ...
##  $ smoothness_mean                : num  0.1184 0.0847 0.1096 0.1425 0.1003 ...
##  $ compactness_mean               : num  0.2776 0.0786 0.1599 0.2839 0.1328 ...
##  $ concavity_mean                 : num  0.3001 0.0869 0.1974 0.2414 0.198 ...
##  $ concave_points_mean            : num  0.1471 0.0702 0.1279 0.1052 0.1043 ...
##  $ symmetry_mean                  : num  0.242 0.181 0.207 0.26 0.181 ...
##  $ fractal_dimension_mean         : num  0.0787 0.0567 0.06 0.0974 0.0588 ...
##  $ radius_se                      : num  1.095 0.543 0.746 0.496 0.757 ...
##  $ texture_se                     : num  0.905 0.734 0.787 1.156 0.781 ...
##  $ perimeter_se                   : num  8.59 3.4 4.58 3.44 5.44 ...
##  $ area_se                        : num  153.4 74.1 94 27.2 94.4 ...
##  $ smoothness_se                  : num  0.0064 0.00522 0.00615 0.00911 0.01149 ...
##  $ compactness_se                 : num  0.049 0.0131 0.0401 0.0746 0.0246 ...
##  $ concavity_se                   : num  0.0537 0.0186 0.0383 0.0566 0.0569 ...
##  $ concave_points_se              : num  0.0159 0.0134 0.0206 0.0187 0.0188 ...
##  $ symmetry_se                    : num  0.03 0.0139 0.0225 0.0596 0.0176 ...
##  $ fractal_dimension_se           : num  0.00619 0.00353 0.00457 0.00921 0.00511 ...
##  $ radius_largest_worst           : num  25.4 25 23.6 14.9 22.5 ...
##  $ texture_largest_worst          : num  17.3 23.4 25.5 26.5 16.7 ...
##  $ perimeter_largest_worst        : num  184.6 158.8 152.5 98.9 152.2 ...
##  $ area_largest_worst             : num  2019 1956 1709 568 1575 ...
##  $ smoothness_largest_worst       : num  0.162 0.124 0.144 0.21 0.137 ...
##  $ compactness_largest_worst      : num  0.666 0.187 0.424 0.866 0.205 ...
##  $ concavity_largest_worst        : num  0.712 0.242 0.45 0.687 0.4 ...
##  $ concave_points_largest_worst   : num  0.265 0.186 0.243 0.258 0.163 ...
##  $ symmetry_largest_worst         : num  0.46 0.275 0.361 0.664 0.236 ...
##  $ fractal_dimension_largest_worst: num  0.1189 0.089 0.0876 0.173 0.0768 ...
</code></pre>
</div>
<p></p>
<h3>Breast cancer dataset 3</h3>
<p>The third dataset looks at the predictor classes:</p>
<ul>
<li>R: recurring or</li>
<li>N: nonrecurring breast cancer.</li>
</ul>
<p>The first two columns give:</p>
<ul>
<li>Sample ID</li>
<li>Classes, i.e. outcome</li>
</ul>
<p>For each cell nucleus, the same ten characteristics and measures were given as in dataset 2, plus:</p>
<ul>
<li>Time (recurrence time if field 2 = R, disease-free time if field 2 = N)</li>
<li>Tumor size – diameter of the excised tumor in centimeters</li>
<li>Lymph node status – number of positive axillary lymph nodes observed at time of surgery</li>
</ul>
<p>Missing values are imputed with the <em>mice</em> package.</p>
<div>
<pre><code><span>bc_data_3</span><span> </span><span>&lt;-</span><span> </span><span>read.table</span><span>(</span><span>&quot;wpbc.data.txt&quot;</span><span>,</span><span> </span><span>header</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;,&quot;</span><span>)</span><span>
</span><span>colnames</span><span>(</span><span>bc_data_3</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;ID&quot;</span><span>,</span><span> </span><span>&quot;outcome&quot;</span><span>,</span><span> </span><span>&quot;time&quot;</span><span>,</span><span> </span><span>paste</span><span>(</span><span>phenotypes</span><span>,</span><span> </span><span>types</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>),</span><span> </span><span>&quot;tumor_size&quot;</span><span>,</span><span> </span><span>&quot;lymph_node_status&quot;</span><span>)</span><span>
</span><span>bc_data_3</span><span>[</span><span>bc_data_3</span><span> </span><span>==</span><span> </span><span>&quot;?&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>NA</span><span>
</span><span># how many NAs are in the data
</span><span>length</span><span>(</span><span>which</span><span>(</span><span>is.na</span><span>(</span><span>bc_data_3</span><span>)))</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 4
</code></pre>
</div>
<div>
<pre><code><span># impute missing data
</span><span>library</span><span>(</span><span>mice</span><span>)</span><span>
</span><span>bc_data_3</span><span>[,</span><span>3</span><span>:</span><span>35</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>apply</span><span>(</span><span>bc_data_3</span><span>[,</span><span>3</span><span>:</span><span>35</span><span>],</span><span> </span><span>2</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>as.numeric</span><span>(</span><span>as.character</span><span>(</span><span>x</span><span>)))</span><span>
</span><span>dataset_impute</span><span> </span><span>&lt;-</span><span> </span><span>mice</span><span>(</span><span>bc_data_3</span><span>[,</span><span>3</span><span>:</span><span>35</span><span>],</span><span> </span><span>print</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>bc_data_3</span><span> </span><span>&lt;-</span><span> </span><span>cbind</span><span>(</span><span>bc_data_3</span><span>[,</span><span> </span><span>2</span><span>,</span><span> </span><span>drop</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>],</span><span> </span><span>mice</span><span>::</span><span>complete</span><span>(</span><span>dataset_impute</span><span>,</span><span> </span><span>1</span><span>))</span><span>
</span><span># how many recurring and non-recurring cases are there?
</span><span>summary</span><span>(</span><span>bc_data_3</span><span>$</span><span>outcome</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##   N   R 
## 151  47
</code></pre>
</div>
<div>
<pre><code><span>str</span><span>(</span><span>bc_data_3</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## 'data.frame':    198 obs. of  34 variables:
##  $ outcome                        : Factor w/ 2 levels &quot;N&quot;,&quot;R&quot;: 1 1 1 1 2 2 1 2 1 1 ...
##  $ time                           : num  31 61 116 123 27 77 60 77 119 76 ...
##  $ radius_mean                    : num  18 18 21.4 11.4 20.3 ...
##  $ texture_mean                   : num  27.6 10.4 17.4 20.4 14.3 ...
##  $ perimeter_mean                 : num  117.5 122.8 137.5 77.6 135.1 ...
##  $ area_mean                      : num  1013 1001 1373 386 1297 ...
##  $ smoothness_mean                : num  0.0949 0.1184 0.0884 0.1425 0.1003 ...
##  $ compactness_mean               : num  0.104 0.278 0.119 0.284 0.133 ...
##  $ concavity_mean                 : num  0.109 0.3 0.126 0.241 0.198 ...
##  $ concave_points_mean            : num  0.0706 0.1471 0.0818 0.1052 0.1043 ...
##  $ symmetry_mean                  : num  0.186 0.242 0.233 0.26 0.181 ...
##  $ fractal_dimension_mean         : num  0.0633 0.0787 0.0601 0.0974 0.0588 ...
##  $ radius_se                      : num  0.625 1.095 0.585 0.496 0.757 ...
##  $ texture_se                     : num  1.89 0.905 0.611 1.156 0.781 ...
##  $ perimeter_se                   : num  3.97 8.59 3.93 3.44 5.44 ...
##  $ area_se                        : num  71.5 153.4 82.2 27.2 94.4 ...
##  $ smoothness_se                  : num  0.00443 0.0064 0.00617 0.00911 0.01149 ...
##  $ compactness_se                 : num  0.0142 0.049 0.0345 0.0746 0.0246 ...
##  $ concavity_se                   : num  0.0323 0.0537 0.033 0.0566 0.0569 ...
##  $ concave_points_se              : num  0.00985 0.01587 0.01805 0.01867 0.01885 ...
##  $ symmetry_se                    : num  0.0169 0.03 0.0309 0.0596 0.0176 ...
##  $ fractal_dimension_se           : num  0.00349 0.00619 0.00504 0.00921 0.00511 ...
##  $ radius_largest_worst           : num  21.6 25.4 24.9 14.9 22.5 ...
##  $ texture_largest_worst          : num  37.1 17.3 21 26.5 16.7 ...
##  $ perimeter_largest_worst        : num  139.7 184.6 159.1 98.9 152.2 ...
##  $ area_largest_worst             : num  1436 2019 1949 568 1575 ...
##  $ smoothness_largest_worst       : num  0.119 0.162 0.119 0.21 0.137 ...
##  $ compactness_largest_worst      : num  0.193 0.666 0.345 0.866 0.205 ...
##  $ concavity_largest_worst        : num  0.314 0.712 0.341 0.687 0.4 ...
##  $ concave_points_largest_worst   : num  0.117 0.265 0.203 0.258 0.163 ...
##  $ symmetry_largest_worst         : num  0.268 0.46 0.433 0.664 0.236 ...
##  $ fractal_dimension_largest_worst: num  0.0811 0.1189 0.0907 0.173 0.0768 ...
##  $ tumor_size                     : num  5 3 2.5 2 3.5 2.5 1.5 4 2 6 ...
##  $ lymph_node_status              : num  5 2 0 0 0 0 0 10 1 20 ...
</code></pre>
</div>
<p></p>
<h2>Principal Component Analysis (PCA)</h2>
<p>To get an idea about the dimensionality and variance of the datasets, I am first looking at PCA plots for samples and features. The first two principal components (PCs) show the two components that explain the majority of variation in the data.</p>
<p>After defining my custom ggplot2 theme, I am creating a function that performs the PCA (using the <em>pcaGoPromoter</em> package), calculates ellipses of the data points (with the <em>ellipse</em> package) and produces the plot with <em>ggplot2</em>. Some of the features in datasets 2 and 3 are not very distinct and overlap in the PCA plots, therefore I am also plotting hierarchical clustering dendrograms.</p>
<div>
<pre><code><span># plotting theme
</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>my_theme</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>base_family</span><span> </span><span>=</span><span> </span><span>&quot;sans&quot;</span><span>){</span><span>
</span><span>theme_minimal</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>base_size</span><span>,</span><span> </span><span>base_family</span><span> </span><span>=</span><span> </span><span>base_family</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>),</span><span>
</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>14</span><span>),</span><span>
</span><span>panel.grid.major</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>),</span><span>
</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;aliceblue&quot;</span><span>),</span><span>
</span><span>strip.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>),</span><span>
</span><span>strip.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>),</span><span>
</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>,</span><span>
</span><span>legend.justification</span><span> </span><span>=</span><span> </span><span>&quot;top&quot;</span><span>,</span><span> </span><span>legend.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>NA</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span>
</span><span>)</span><span>
</span><span>}</span><span>
</span><span>theme_set</span><span>(</span><span>my_theme</span><span>())</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span># function for PCA plotting
</span><span>library</span><span>(</span><span>pcaGoPromoter</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ellipse</span><span>)</span><span>
</span><span>pca_func</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>data</span><span>,</span><span> </span><span>groups</span><span>,</span><span> </span><span>title</span><span>,</span><span> </span><span>print_ellipse</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>{</span><span>
</span><span># perform pca and extract scores
</span><span> </span><span>pcaOutput</span><span> </span><span>&lt;-</span><span> </span><span>pca</span><span>(</span><span>data</span><span>,</span><span> </span><span>printDropped</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>scale</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>center</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>pcaOutput2</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>pcaOutput</span><span>$</span><span>scores</span><span>)</span><span>
</span><span># define groups for plotting
</span><span> </span><span>pcaOutput2</span><span>$</span><span>groups</span><span> </span><span>&lt;-</span><span> </span><span>groups</span><span>
</span><span># when plotting samples calculate ellipses for plotting (when plotting features, there are no replicates)
</span><span> </span><span>if</span><span> </span><span>(</span><span>print_ellipse</span><span>)</span><span> </span><span>{</span><span>
</span><span>centroids</span><span> </span><span>&lt;-</span><span> </span><span>aggregate</span><span>(</span><span>cbind</span><span>(</span><span>PC1</span><span>,</span><span> </span><span>PC2</span><span>)</span><span> </span><span>~</span><span> </span><span>groups</span><span>,</span><span> </span><span>pcaOutput2</span><span>,</span><span> </span><span>mean</span><span>)</span><span>
</span><span>conf.rgn</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>rbind</span><span>,</span><span> </span><span>lapply</span><span>(</span><span>unique</span><span>(</span><span>pcaOutput2</span><span>$</span><span>groups</span><span>),</span><span> </span><span>function</span><span>(</span><span>t</span><span>)</span><span>
</span><span>data.frame</span><span>(</span><span>groups</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>t</span><span>),</span><span>
</span><span>ellipse</span><span>(</span><span>cov</span><span>(</span><span>pcaOutput2</span><span>[</span><span>pcaOutput2</span><span>$</span><span>groups</span><span> </span><span>==</span><span> </span><span>t</span><span>,</span><span> </span><span>1</span><span>:</span><span>2</span><span>]),</span><span>
</span><span>centre</span><span> </span><span>=</span><span> </span><span>as.matrix</span><span>(</span><span>centroids</span><span>[</span><span>centroids</span><span>$</span><span>groups</span><span> </span><span>==</span><span> </span><span>t</span><span>,</span><span> </span><span>2</span><span>:</span><span>3</span><span>]),</span><span>
</span><span>level</span><span> </span><span>=</span><span> </span><span>0.95</span><span>),</span><span>
</span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)))</span><span>
</span><span>plot</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>pcaOutput2</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>PC1</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>PC2</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>groups</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>groups</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_polygon</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>conf.rgn</span><span>,</span><span> </span><span>aes</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>groups</span><span>),</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span>+</span><span> </span><span>scale_color_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>labs</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>title</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;PC1: &quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>pcaOutput</span><span>$</span><span>pov</span><span>[</span><span>1</span><span>],</span><span> </span><span>digits</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span> </span><span>&quot;% variance&quot;</span><span>),</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;PC2: &quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>pcaOutput</span><span>$</span><span>pov</span><span>[</span><span>2</span><span>],</span><span> </span><span>digits</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span> </span><span>&quot;% variance&quot;</span><span>))</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span># if there are fewer than 10 groups (e.g. the predictor classes) I want to have colors from RColorBrewer
</span><span> </span><span>if</span><span> </span><span>(</span><span>length</span><span>(</span><span>unique</span><span>(</span><span>pcaOutput2</span><span>$</span><span>groups</span><span>))</span><span> </span><span>&lt;=</span><span> </span><span>10</span><span>)</span><span> </span><span>{</span><span>
</span><span>plot</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>pcaOutput2</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>PC1</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>PC2</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>groups</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>groups</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span>+</span><span> </span><span>scale_color_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>labs</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>title</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;PC1: &quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>pcaOutput</span><span>$</span><span>pov</span><span>[</span><span>1</span><span>],</span><span> </span><span>digits</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span> </span><span>&quot;% variance&quot;</span><span>),</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;PC2: &quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>pcaOutput</span><span>$</span><span>pov</span><span>[</span><span>2</span><span>],</span><span> </span><span>digits</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span> </span><span>&quot;% variance&quot;</span><span>))</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span># otherwise use the default rainbow colors
</span><span> </span><span>plot</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>pcaOutput2</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>PC1</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>PC2</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>groups</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>groups</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span>+</span><span> </span><span>labs</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>title</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;PC1: &quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>pcaOutput</span><span>$</span><span>pov</span><span>[</span><span>1</span><span>],</span><span> </span><span>digits</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span> </span><span>&quot;% variance&quot;</span><span>),</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;PC2: &quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>pcaOutput</span><span>$</span><span>pov</span><span>[</span><span>2</span><span>],</span><span> </span><span>digits</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span> </span><span>&quot;% variance&quot;</span><span>))</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>return</span><span>(</span><span>plot</span><span>)</span><span>
</span><span>}</span><span>
</span><span>library</span><span>(</span><span>gridExtra</span><span>)</span><span>
</span><span>library</span><span>(</span><span>grid</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<ul>
<li>Dataset 1</li>
</ul>
<div>
<pre><code><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>pca_func</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>t</span><span>(</span><span>bc_data</span><span>[,</span><span> </span><span>2</span><span>:</span><span>10</span><span>]),</span><span> </span><span>groups</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>bc_data</span><span>$</span><span>classes</span><span>),</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Breast cancer dataset 1: Samples&quot;</span><span>)</span><span>
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>pca_func</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data</span><span>[,</span><span> </span><span>2</span><span>:</span><span>10</span><span>],</span><span> </span><span>groups</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>colnames</span><span>(</span><span>bc_data</span><span>[,</span><span> </span><span>2</span><span>:</span><span>10</span><span>])),</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Breast cancer dataset 1: Features&quot;</span><span>,</span><span> </span><span>print_ellipse</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>grid.arrange</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>p</span><span>2</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-6-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>h_1</span><span> </span><span>&lt;-</span><span> </span><span>hclust</span><span>(</span><span>dist</span><span>(</span><span>t</span><span>(</span><span>bc_data</span><span>[,</span><span> </span><span>2</span><span>:</span><span>10</span><span>]),</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;euclidean&quot;</span><span>),</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;complete&quot;</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>h_1</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i0.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-7-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>bc_data_gather</span><span> </span><span>&lt;-</span><span> </span><span>bc_data</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>measure</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>clump_thickness</span><span>:</span><span>mitosis</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>classes</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>classes</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_density</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_rug</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_fill_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_color_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>facet_wrap</span><span>(</span><span> </span><span>~</span><span> </span><span>measure</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>,</span><span> </span><span>ncol</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/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-8-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<ul>
<li>Dataset 2</li>
</ul>
<div>
<pre><code><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>pca_func</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>t</span><span>(</span><span>bc_data_2</span><span>[,</span><span> </span><span>3</span><span>:</span><span>32</span><span>]),</span><span> </span><span>groups</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>bc_data_2</span><span>$</span><span>diagnosis</span><span>),</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Breast cancer dataset 2: Samples&quot;</span><span>)</span><span>
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>pca_func</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_2</span><span>[,</span><span> </span><span>3</span><span>:</span><span>32</span><span>],</span><span> </span><span>groups</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>colnames</span><span>(</span><span>bc_data_2</span><span>[,</span><span> </span><span>3</span><span>:</span><span>32</span><span>])),</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Breast cancer dataset 2: Features&quot;</span><span>,</span><span> </span><span>print_ellipse</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>grid.arrange</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>p</span><span>2</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>widths</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.4</span><span>,</span><span> </span><span>0.6</span><span>))</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-9-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>h_2</span><span> </span><span>&lt;-</span><span> </span><span>hclust</span><span>(</span><span>dist</span><span>(</span><span>t</span><span>(</span><span>bc_data_2</span><span>[,</span><span> </span><span>3</span><span>:</span><span>32</span><span>]),</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;euclidean&quot;</span><span>),</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;complete&quot;</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>h_2</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i2.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-10-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>bc_data_2_gather</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_2</span><span>[,</span><span> </span><span>-1</span><span>]</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>measure</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>radius_mean</span><span>:</span><span>fractal_dimension_largest_worst</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_2_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>diagnosis</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>diagnosis</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_density</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_rug</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_fill_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_color_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>facet_wrap</span><span>(</span><span> </span><span>~</span><span> </span><span>measure</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-11-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<ul>
<li>Dataset 3</li>
</ul>
<div>
<pre><code><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>pca_func</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>t</span><span>(</span><span>bc_data_3</span><span>[,</span><span> </span><span>2</span><span>:</span><span>34</span><span>]),</span><span> </span><span>groups</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>bc_data_3</span><span>$</span><span>outcome</span><span>),</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Breast cancer dataset 3: Samples&quot;</span><span>)</span><span>
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>pca_func</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_3</span><span>[,</span><span> </span><span>2</span><span>:</span><span>34</span><span>],</span><span> </span><span>groups</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>colnames</span><span>(</span><span>bc_data_3</span><span>[,</span><span> </span><span>2</span><span>:</span><span>34</span><span>])),</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Breast cancer dataset 3: Features&quot;</span><span>,</span><span> </span><span>print_ellipse</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>grid.arrange</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>p</span><span>2</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>widths</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.4</span><span>,</span><span> </span><span>0.6</span><span>))</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i0.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-12-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>h_3</span><span> </span><span>&lt;-</span><span> </span><span>hclust</span><span>(</span><span>dist</span><span>(</span><span>t</span><span>(</span><span>bc_data_3</span><span>[,</span><span>2</span><span>:</span><span>34</span><span>]),</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;euclidean&quot;</span><span>),</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;complete&quot;</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>h_3</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i2.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-13-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Datasets 1 and 2 show a nice separation of benign and malignant masses, models based on these features will likely be able to predict the classes quite well for most samples. The classes in dataset 3 don’t cluster into distinct groups, I assume that prediction will not be as accurate for these features.</p>
<p>The features of datasets 2 and 3 don’t cluster very distinctly, many features seem to show similar patterns across samples. Selecting an approriate subset of features will probably have different effects on the three different datasets.</p>
<div>
<pre><code><span>bc_data_3_gather</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_3</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>measure</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>time</span><span>:</span><span>lymph_node_status</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_3_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>outcome</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>outcome</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_density</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_rug</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_fill_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_color_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>facet_wrap</span><span>(</span><span> </span><span>~</span><span> </span><span>measure</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-14-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<h2>Feature importance</h2>
<p>To get an idea about the feature’s respective importances, I’m running Random Forest models with 10 x 10 cross validation using the <em>caret</em> package. If I wanted to use feature importance to select features for modeling, I would need to perform it on the training data instead of on the complete dataset. But here, I only want to use it to get acquainted with my data. I am again defining a function that estimates the feature importance and produces a plot.</p>
<div>
<pre><code><span>library</span><span>(</span><span>caret</span><span>)</span><span>
</span><span>library</span><span>(</span><span>doParallel</span><span>)</span><span> </span><span># parallel processing
</span><span>registerDoParallel</span><span>()</span><span>
</span><span># prepare training scheme
</span><span>control</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>feature_imp</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>model</span><span>,</span><span> </span><span>title</span><span>)</span><span> </span><span>{</span><span>
</span><span># estimate variable importance
</span><span> </span><span>importance</span><span> </span><span>&lt;-</span><span> </span><span>varImp</span><span>(</span><span>model</span><span>,</span><span> </span><span>scale</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span># prepare dataframes for plotting
</span><span> </span><span>importance_df_1</span><span> </span><span>&lt;-</span><span> </span><span>importance</span><span>$</span><span>importance</span><span>
</span><span>importance_df_1</span><span>$</span><span>group</span><span> </span><span>&lt;-</span><span> </span><span>rownames</span><span>(</span><span>importance_df_1</span><span>)</span><span>
</span><span>importance_df_2</span><span> </span><span>&lt;-</span><span> </span><span>importance_df_1</span><span>
</span><span>importance_df_2</span><span>$</span><span>Overall</span><span> </span><span>&lt;-</span><span> </span><span>0</span><span>
</span><span>importance_df</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>importance_df_1</span><span>,</span><span> </span><span>importance_df_2</span><span>)</span><span>
</span><span>plot</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>importance_df_1</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Overall</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>group</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>group</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_path</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>importance_df</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Overall</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>group</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>group</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>group</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;none&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Importance&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>title</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Scaled feature importance&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;\nDetermined with Random Forest and
repeated cross validation (10 repeats, 10 times)&quot;</span><span>
</span><span>)</span><span>
</span><span>return</span><span>(</span><span>plot</span><span>)</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span># train the model
</span><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>imp_1</span><span> </span><span>&lt;-</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>bc_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>control</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>feature_imp</span><span>(</span><span>imp_1</span><span>,</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Breast cancer dataset 1&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>imp_2</span><span> </span><span>&lt;-</span><span> </span><span>train</span><span>(</span><span>diagnosis</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_2</span><span>[,</span><span> </span><span>-1</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>control</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>feature_imp</span><span>(</span><span>imp_2</span><span>,</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Breast cancer dataset 2&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>imp_3</span><span> </span><span>&lt;-</span><span> </span><span>train</span><span>(</span><span>outcome</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_3</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>control</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>p</span><span>3</span><span> </span><span>&lt;-</span><span> </span><span>feature_imp</span><span>(</span><span>imp_3</span><span>,</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Breast cancer dataset 3&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>grid.arrange</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>p</span><span>2</span><span>,</span><span> </span><span>p</span><span>3</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>widths</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.3</span><span>,</span><span> </span><span>0.35</span><span>,</span><span> </span><span>0.35</span><span>))</span><span>
</span></code></pre>
</div>
<p><img src="https://i0.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-25-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<h1>Feature Selection</h1>
<p>Now that I have a general idea about the data, I will run three feature selection methods on all three datasets and compare how they effect the prediction accuracy of a Random Forest model.</p>
<h3>Creating train and test data</h3>
<p>Before doing anything else with the data, we need to subset the datasets into train and test data. Performing feature selection on the whole dataset would lead to prediction bias, we therefore need to run the whole modeling process on the training data alone!</p>
<p></p>
<ul>
<li>Dataset 1</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>bc_data_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>bc_data_train</span><span> </span><span>&lt;-</span><span> </span><span>bc_data</span><span>[</span><span>bc_data_index</span><span>,</span><span> </span><span>]</span><span>
</span><span>bc_data_test</span><span> </span><span>&lt;-</span><span> </span><span>bc_data</span><span>[</span><span>-</span><span>bc_data_index</span><span>,</span><span> </span><span>]</span><span>
</span></code></pre>
</div>
<p></p>
<ul>
<li>Dataset 2</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>bc_data_2_index</span><span> </span><span>&lt;-</span><span> </span><span>createDataPartition</span><span>(</span><span>bc_data_2</span><span>$</span><span>diagnosis</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>bc_data_2_train</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_2</span><span>[</span><span>bc_data_2_index</span><span>,</span><span> </span><span>]</span><span>
</span><span>bc_data_2_test</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_2</span><span>[</span><span>-</span><span>bc_data_2_index</span><span>,</span><span> </span><span>]</span><span>
</span></code></pre>
</div>
<p></p>
<ul>
<li>Dataset 3</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>bc_data_3_index</span><span> </span><span>&lt;-</span><span> </span><span>createDataPartition</span><span>(</span><span>bc_data_3</span><span>$</span><span>outcome</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>bc_data_3_train</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_3</span><span>[</span><span>bc_data_3_index</span><span>,</span><span> </span><span>]</span><span>
</span><span>bc_data_3_test</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_3</span><span>[</span><span>-</span><span>bc_data_3_index</span><span>,</span><span> </span><span>]</span><span>
</span></code></pre>
</div>
<p></p>
<h2>Correlation</h2>
<p>Often we have features that are highly correlated and thus provide redundant information. By eliminating highly correlated features we can avoid a predictive bias for the information contained in these features. This also shows us, that when we want to make statements about the biological/ medical importance of specific features, we need to keep in mind that just because they are suitable to predicting an outcome they are not necessarily causal – they could simply be correlated with causal factors.</p>
<p>Correlations between all features are calculated and visualised with the <em>corrplot</em> package. I am then removing all features with a correlation higher than 0.7, keeping the feature with the lower mean.</p>
<p></p>
<ul>
<li>Dataset 1</li>
</ul>
<div>
<pre><code><span>library</span><span>(</span><span>corrplot</span><span>)</span><span>
</span><span># calculate correlation matrix
</span><span>corMatMy</span><span> </span><span>&lt;-</span><span> </span><span>cor</span><span>(</span><span>bc_data_train</span><span>[,</span><span> </span><span>-1</span><span>])</span><span>
</span><span>corrplot</span><span>(</span><span>corMatMy</span><span>,</span><span> </span><span>order</span><span> </span><span>=</span><span> </span><span>&quot;hclust&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i2.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-29-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>#Apply correlation filter at 0.70,
</span><span>highlyCor</span><span> </span><span>&lt;-</span><span> </span><span>colnames</span><span>(</span><span>bc_data_train</span><span>[,</span><span> </span><span>-1</span><span>])[</span><span>findCorrelation</span><span>(</span><span>corMatMy</span><span>,</span><span> </span><span>cutoff</span><span> </span><span>=</span><span> </span><span>0.7</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)]</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Compare row 2  and column  3 with corr  0.913 
##   Means:  0.716 vs 0.601 so flagging column 2 
## Compare row 3  and column  6 with corr  0.741 
##   Means:  0.677 vs 0.579 so flagging column 3 
## Compare row 6  and column  7 with corr  0.706 
##   Means:  0.602 vs 0.545 so flagging column 6 
## All correlations &lt;= 0.7
</code></pre>
</div>
<div>
<pre><code><span># which variables are flagged for removal?
</span><span>highlyCor</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;uniformity_of_cell_size&quot;  &quot;uniformity_of_cell_shape&quot;
## [3] &quot;bare_nuclei&quot;
</code></pre>
</div>
<div>
<pre><code><span>#then we remove these variables
</span><span>bc_data_cor</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_train</span><span>[,</span><span> </span><span>which</span><span>(</span><span>!</span><span>colnames</span><span>(</span><span>bc_data_train</span><span>)</span><span> </span><span>%in%</span><span> </span><span>highlyCor</span><span>)]</span><span>
</span></code></pre>
</div>
<p>Correlation between features in dataset 1 is generally high and 4 out of 10 feature were flagged for removal.</p>
<p></p>
<ul>
<li>Dataset 2</li>
</ul>
<div>
<pre><code><span>corMatMy</span><span> </span><span>&lt;-</span><span> </span><span>cor</span><span>(</span><span>bc_data_2_train</span><span>[,</span><span> </span><span>3</span><span>:</span><span>32</span><span>])</span><span>
</span><span>corrplot</span><span>(</span><span>corMatMy</span><span>,</span><span> </span><span>order</span><span> </span><span>=</span><span> </span><span>&quot;hclust&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i2.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-30-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>highlyCor</span><span> </span><span>&lt;-</span><span> </span><span>colnames</span><span>(</span><span>bc_data_2_train</span><span>[,</span><span> </span><span>3</span><span>:</span><span>32</span><span>])[</span><span>findCorrelation</span><span>(</span><span>corMatMy</span><span>,</span><span> </span><span>cutoff</span><span> </span><span>=</span><span> </span><span>0.7</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)]</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Compare row 7  and column  8 with corr  0.92 
##   Means:  0.579 vs 0.393 so flagging column 7 
## Compare row 8  and column  6 with corr  0.84 
##   Means:  0.548 vs 0.38 so flagging column 8 
## Compare row 6  and column  28 with corr  0.827 
##   Means:  0.536 vs 0.368 so flagging column 6 
## Compare row 28  and column  27 with corr  0.855 
##   Means:  0.506 vs 0.357 so flagging column 28 
## Compare row 27  and column  26 with corr  0.894 
##   Means:  0.46 vs 0.346 so flagging column 27 
## Compare row 23  and column  21 with corr  0.993 
##   Means:  0.454 vs 0.336 so flagging column 23 
## Compare row 21  and column  24 with corr  0.983 
##   Means:  0.419 vs 0.327 so flagging column 21 
## Compare row 26  and column  30 with corr  0.817 
##   Means:  0.402 vs 0.323 so flagging column 26 
## Compare row 24  and column  3 with corr  0.943 
##   Means:  0.383 vs 0.312 so flagging column 24 
## Compare row 3  and column  1 with corr  0.998 
##   Means:  0.347 vs 0.306 so flagging column 3 
## Compare row 1  and column  4 with corr  0.986 
##   Means:  0.302 vs 0.304 so flagging column 4 
## Compare row 1  and column  14 with corr  0.726 
##   Means:  0.264 vs 0.304 so flagging column 14 
## Compare row 13  and column  11 with corr  0.973 
##   Means:  0.32 vs 0.304 so flagging column 13 
## Compare row 18  and column  16 with corr  0.757 
##   Means:  0.388 vs 0.295 so flagging column 18 
## Compare row 16  and column  17 with corr  0.796 
##   Means:  0.404 vs 0.288 so flagging column 16 
## Compare row 9  and column  29 with corr  0.711 
##   Means:  0.343 vs 0.274 so flagging column 9 
## Compare row 17  and column  20 with corr  0.745 
##   Means:  0.306 vs 0.261 so flagging column 17 
## Compare row 5  and column  25 with corr  0.809 
##   Means:  0.311 vs 0.255 so flagging column 5 
## Compare row 30  and column  10 with corr  0.753 
##   Means:  0.288 vs 0.241 so flagging column 30 
## Compare row 22  and column  2 with corr  0.913 
##   Means:  0.243 vs 0.242 so flagging column 22 
## All correlations &lt;= 0.7
</code></pre>
</div>
<div>
<pre><code><span>highlyCor</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  [1] &quot;concavity_mean&quot;                  &quot;concave_points_mean&quot;            
##  [3] &quot;compactness_mean&quot;                &quot;concave_points_largest_worst&quot;   
##  [5] &quot;concavity_largest_worst&quot;         &quot;perimeter_largest_worst&quot;        
##  [7] &quot;radius_largest_worst&quot;            &quot;compactness_largest_worst&quot;      
##  [9] &quot;area_largest_worst&quot;              &quot;perimeter_mean&quot;                 
## [11] &quot;perimeter_se&quot;                    &quot;area_mean&quot;                      
## [13] &quot;concave_points_se&quot;               &quot;compactness_se&quot;                 
## [15] &quot;area_se&quot;                         &quot;symmetry_mean&quot;                  
## [17] &quot;concavity_se&quot;                    &quot;smoothness_mean&quot;                
## [19] &quot;fractal_dimension_largest_worst&quot; &quot;texture_largest_worst&quot;
</code></pre>
</div>
<div>
<pre><code><span>bc_data_2_cor</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_2_train</span><span>[,</span><span> </span><span>which</span><span>(</span><span>!</span><span>colnames</span><span>(</span><span>bc_data_2_train</span><span>)</span><span> </span><span>%in%</span><span> </span><span>highlyCor</span><span>)]</span><span>
</span></code></pre>
</div>
<p>Here, we have more variation between the 30 features: some are highly correlated, while others seem to be very distinct. 20 are flagged for removal (see output above).</p>
<p></p>
<ul>
<li>Dataset 3</li>
</ul>
<div>
<pre><code><span>corMatMy</span><span> </span><span>&lt;-</span><span> </span><span>cor</span><span>(</span><span>bc_data_3_train</span><span>[,</span><span> </span><span>-1</span><span>])</span><span>
</span><span>corrplot</span><span>(</span><span>corMatMy</span><span>,</span><span> </span><span>order</span><span> </span><span>=</span><span> </span><span>&quot;hclust&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i2.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-31-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>highlyCor</span><span> </span><span>&lt;-</span><span> </span><span>colnames</span><span>(</span><span>bc_data_3_train</span><span>[,</span><span> </span><span>-1</span><span>])[</span><span>findCorrelation</span><span>(</span><span>corMatMy</span><span>,</span><span> </span><span>cutoff</span><span> </span><span>=</span><span> </span><span>0.7</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)]</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Compare row 8  and column  9 with corr  0.898 
##   Means:  0.425 vs 0.286 so flagging column 8 
## Compare row 9  and column  7 with corr  0.714 
##   Means:  0.39 vs 0.277 so flagging column 9 
## Compare row 7  and column  29 with corr  0.753 
##   Means:  0.364 vs 0.271 so flagging column 7 
## Compare row 4  and column  2 with corr  0.996 
##   Means:  0.348 vs 0.264 so flagging column 4 
## Compare row 2  and column  5 with corr  0.993 
##   Means:  0.33 vs 0.259 so flagging column 2 
## Compare row 5  and column  24 with corr  0.921 
##   Means:  0.304 vs 0.255 so flagging column 5 
## Compare row 24  and column  22 with corr  0.985 
##   Means:  0.272 vs 0.252 so flagging column 24 
## Compare row 11  and column  31 with corr  0.83 
##   Means:  0.341 vs 0.247 so flagging column 11 
## Compare row 22  and column  15 with corr  0.773 
##   Means:  0.24 vs 0.242 so flagging column 15 
## Compare row 22  and column  25 with corr  0.989 
##   Means:  0.217 vs 0.242 so flagging column 25 
## Compare row 14  and column  12 with corr  0.975 
##   Means:  0.256 vs 0.243 so flagging column 14 
## Compare row 31  and column  28 with corr  0.71 
##   Means:  0.328 vs 0.238 so flagging column 31 
## Compare row 18  and column  17 with corr  0.812 
##   Means:  0.331 vs 0.229 so flagging column 18 
## Compare row 28  and column  27 with corr  0.84 
##   Means:  0.286 vs 0.219 so flagging column 28 
## Compare row 17  and column  21 with corr  0.839 
##   Means:  0.285 vs 0.212 so flagging column 17 
## Compare row 10  and column  30 with corr  0.766 
##   Means:  0.277 vs 0.204 so flagging column 10 
## Compare row 6  and column  26 with corr  0.754 
##   Means:  0.235 vs 0.198 so flagging column 6 
## Compare row 3  and column  23 with corr  0.858 
##   Means:  0.164 vs 0.195 so flagging column 23 
## All correlations &lt;= 0.7
</code></pre>
</div>
<div>
<pre><code><span>highlyCor</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  [1] &quot;concavity_mean&quot;                  &quot;concave_points_mean&quot;            
##  [3] &quot;compactness_mean&quot;                &quot;perimeter_mean&quot;                 
##  [5] &quot;radius_mean&quot;                     &quot;area_mean&quot;                      
##  [7] &quot;perimeter_largest_worst&quot;         &quot;fractal_dimension_mean&quot;         
##  [9] &quot;perimeter_se&quot;                    &quot;area_se&quot;                        
## [11] &quot;area_largest_worst&quot;              &quot;fractal_dimension_largest_worst&quot;
## [13] &quot;concavity_se&quot;                    &quot;concavity_largest_worst&quot;        
## [15] &quot;compactness_se&quot;                  &quot;symmetry_mean&quot;                  
## [17] &quot;smoothness_mean&quot;                 &quot;texture_largest_worst&quot;
</code></pre>
</div>
<div>
<pre><code><span>bc_data_3_cor</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_3_train</span><span>[,</span><span> </span><span>which</span><span>(</span><span>!</span><span>colnames</span><span>(</span><span>bc_data_3_train</span><span>)</span><span> </span><span>%in%</span><span> </span><span>highlyCor</span><span>)]</span><span>
</span></code></pre>
</div>
<p>The features in dataset 3 look similar: some are highly correlated, others are very different. 18 are flagged for removal (see output above).</p>
<p></p>
<h2>Recursive Feature Elimination (RFE)</h2>
<p>Another way to choose features is with Recursive Feature Elimination. RFE uses a Random Forest algorithm to test combinations of features and rate each with an accuracy score. The combination with the highest score is usually preferential.</p>
<p></p>
<ul>
<li>Dataset 1</li>
</ul>
<div>
<pre><code><span># ensure the results are repeatable
</span><span>set.seed</span><span>(</span><span>7</span><span>)</span><span>
</span><span># define the control using a random forest selection function with cross validation
</span><span>control</span><span> </span><span>&lt;-</span><span> </span><span>rfeControl</span><span>(</span><span>functions</span><span> </span><span>=</span><span> </span><span>rfFuncs</span><span>,</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;cv&quot;</span><span>,</span><span> </span><span>number</span><span> </span><span>=</span><span> </span><span>10</span><span>)</span><span>
</span><span># run the RFE algorithm
</span><span>results_1</span><span> </span><span>&lt;-</span><span> </span><span>rfe</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>bc_data_train</span><span>[,</span><span> </span><span>-1</span><span>],</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>bc_data_train</span><span>$</span><span>classes</span><span>,</span><span> </span><span>sizes</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>:</span><span>9</span><span>),</span><span> </span><span>rfeControl</span><span> </span><span>=</span><span> </span><span>control</span><span>)</span><span>
</span><span># chosen features
</span><span>predictors</span><span>(</span><span>results_1</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;bare_nuclei&quot;                 &quot;uniformity_of_cell_size&quot;    
## [3] &quot;clump_thickness&quot;             &quot;uniformity_of_cell_shape&quot;   
## [5] &quot;bland_chromatin&quot;             &quot;marginal_adhesion&quot;          
## [7] &quot;mitosis&quot;                     &quot;normal_nucleoli&quot;            
## [9] &quot;single_epithelial_cell_size&quot;
</code></pre>
</div>
<div>
<pre><code><span># subset the chosen features
</span><span>bc_data_rfe</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_train</span><span>[,</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>which</span><span>(</span><span>colnames</span><span>(</span><span>bc_data_train</span><span>)</span><span> </span><span>%in%</span><span> </span><span>predictors</span><span>(</span><span>results_1</span><span>)))]</span><span>
</span></code></pre>
</div>
<p></p>
<ul>
<li>Dataset 2</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>7</span><span>)</span><span>
</span><span>results_2</span><span> </span><span>&lt;-</span><span> </span><span>rfe</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>bc_data_2_train</span><span>[,</span><span> </span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>)],</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>bc_data_2_train</span><span>$</span><span>diagnosis</span><span>),</span><span> </span><span>sizes</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>:</span><span>30</span><span>),</span><span> </span><span>rfeControl</span><span> </span><span>=</span><span> </span><span>control</span><span>)</span><span>
</span><span>predictors</span><span>(</span><span>results_2</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  [1] &quot;perimeter_largest_worst&quot;         &quot;area_largest_worst&quot;             
##  [3] &quot;radius_largest_worst&quot;            &quot;concave_points_largest_worst&quot;   
##  [5] &quot;concave_points_mean&quot;             &quot;texture_largest_worst&quot;          
##  [7] &quot;texture_mean&quot;                    &quot;area_se&quot;                        
##  [9] &quot;concavity_largest_worst&quot;         &quot;concavity_mean&quot;                 
## [11] &quot;radius_se&quot;                       &quot;perimeter_mean&quot;                 
## [13] &quot;radius_mean&quot;                     &quot;area_mean&quot;                      
## [15] &quot;perimeter_se&quot;                    &quot;smoothness_largest_worst&quot;       
## [17] &quot;compactness_largest_worst&quot;       &quot;symmetry_largest_worst&quot;         
## [19] &quot;compactness_mean&quot;                &quot;smoothness_mean&quot;                
## [21] &quot;fractal_dimension_largest_worst&quot; &quot;concavity_se&quot;                   
## [23] &quot;symmetry_mean&quot;
</code></pre>
</div>
<div>
<pre><code><span>bc_data_2_rfe</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_2_train</span><span>[,</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span> </span><span>which</span><span>(</span><span>colnames</span><span>(</span><span>bc_data_2_train</span><span>)</span><span> </span><span>%in%</span><span> </span><span>predictors</span><span>(</span><span>results_2</span><span>)))]</span><span>
</span></code></pre>
</div>
<p></p>
<ul>
<li>Dataset 3</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>7</span><span>)</span><span>
</span><span>results_3</span><span> </span><span>&lt;-</span><span> </span><span>rfe</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>bc_data_3_train</span><span>[,</span><span>-1</span><span>],</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>bc_data_3_train</span><span>$</span><span>outcome</span><span>),</span><span> </span><span>sizes</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>:</span><span>33</span><span>),</span><span> </span><span>rfeControl</span><span> </span><span>=</span><span> </span><span>control</span><span>)</span><span>
</span><span>predictors</span><span>(</span><span>results_2</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  [1] &quot;perimeter_largest_worst&quot;         &quot;area_largest_worst&quot;             
##  [3] &quot;radius_largest_worst&quot;            &quot;concave_points_largest_worst&quot;   
##  [5] &quot;concave_points_mean&quot;             &quot;texture_largest_worst&quot;          
##  [7] &quot;texture_mean&quot;                    &quot;area_se&quot;                        
##  [9] &quot;concavity_largest_worst&quot;         &quot;concavity_mean&quot;                 
## [11] &quot;radius_se&quot;                       &quot;perimeter_mean&quot;                 
## [13] &quot;radius_mean&quot;                     &quot;area_mean&quot;                      
## [15] &quot;perimeter_se&quot;                    &quot;smoothness_largest_worst&quot;       
## [17] &quot;compactness_largest_worst&quot;       &quot;symmetry_largest_worst&quot;         
## [19] &quot;compactness_mean&quot;                &quot;smoothness_mean&quot;                
## [21] &quot;fractal_dimension_largest_worst&quot; &quot;concavity_se&quot;                   
## [23] &quot;symmetry_mean&quot;
</code></pre>
</div>
<div>
<pre><code><span>bc_data_3_rfe</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_3_train</span><span>[,</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>which</span><span>(</span><span>colnames</span><span>(</span><span>bc_data_3_train</span><span>)</span><span> </span><span>%in%</span><span> </span><span>predictors</span><span>(</span><span>results_3</span><span>)))]</span><span>
</span></code></pre>
</div>
<p></p>
<h2>Genetic Algorithm (GA)</h2>
<p>The Genetic Algorithm (GA) has been developed based on evolutionary principles of natural selection: It aims to optimize a population of individuals with a given set of genotypes by modeling selection over time. In each generation (i.e. iteration), each individual’s fitness is calculated based on their genotypes. Then, the fittest individuals are chosen to produce the next generation. This subsequent generation of individuals will have genotypes resulting from (re-) combinations of the parental alleles. These new genotypes will again determine each individual’s fitness. This selection process is iterated for a specified number of generations and (ideally) leads to fixation of the fittest alleles in the gene pool.</p>
<p>This concept of optimization can be applied to non-evolutionary models as well, like feature selection processes in machine learning.</p>
<p>For demonstration purposes I am using only 10 generations consisting of 5 individuals. More iterations with larger populations would of course be preferable, but this takes quite long to run!</p>
<div>
<pre><code><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>ga_ctrl</span><span> </span><span>&lt;-</span><span> </span><span>gafsControl</span><span>(</span><span>functions</span><span> </span><span>=</span><span> </span><span>rfGA</span><span>,</span><span> </span><span># Assess fitness with RF
</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;cv&quot;</span><span>,</span><span> </span><span># 10 fold cross validation
</span><span> </span><span>genParallel</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span># Use parallel programming
</span><span> </span><span>allowParallel</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<ul>
<li>Dataset 1</li>
</ul>
<div>
<pre><code><span>lev</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;malignant&quot;</span><span>,</span><span> </span><span>&quot;benign&quot;</span><span>)</span><span> </span><span># Set the levels
</span><span>
</span><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_1</span><span> </span><span>&lt;-</span><span> </span><span>gafs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>bc_data_train</span><span>[,</span><span> </span><span>-1</span><span>],</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>bc_data_train</span><span>$</span><span>classes</span><span>,</span><span>
</span><span>iters</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span># generations of algorithm
</span><span> </span><span>popSize</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span># population size for each generation
</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>lev</span><span>,</span><span>
</span><span>gafsControl</span><span> </span><span>=</span><span> </span><span>ga_ctrl</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>plot</span><span>(</span><span>model_1</span><span>)</span><span> </span><span># Plot mean fitness (AUC) by generation
</span></code></pre>
</div>
<p><img alt="" src="https://i0.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-38-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>model_1</span><span>$</span><span>ga</span><span>$</span><span>final</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;clump_thickness&quot;             &quot;uniformity_of_cell_shape&quot;   
## [3] &quot;marginal_adhesion&quot;           &quot;single_epithelial_cell_size&quot;
## [5] &quot;bare_nuclei&quot;                 &quot;bland_chromatin&quot;            
## [7] &quot;normal_nucleoli&quot;             &quot;mitosis&quot;
</code></pre>
</div>
<div>
<pre><code><span>bc_data_ga</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_train</span><span>[,</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>which</span><span>(</span><span>colnames</span><span>(</span><span>bc_data_train</span><span>)</span><span> </span><span>%in%</span><span> </span><span>model_1</span><span>$</span><span>ga</span><span>$</span><span>final</span><span>))]</span><span>
</span></code></pre>
</div>
<p></p>
<ul>
<li>Dataset 2</li>
</ul>
<div>
<pre><code><span>lev</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;M&quot;</span><span>,</span><span> </span><span>&quot;B&quot;</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_2</span><span> </span><span>&lt;-</span><span> </span><span>gafs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>bc_data_2_train</span><span>[,</span><span> </span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>)],</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>bc_data_2_train</span><span>$</span><span>diagnosis</span><span>,</span><span>
</span><span>iters</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span># generations of algorithm
</span><span> </span><span>popSize</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span># population size for each generation
</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>lev</span><span>,</span><span>
</span><span>gafsControl</span><span> </span><span>=</span><span> </span><span>ga_ctrl</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>plot</span><span>(</span><span>model_2</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i2.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-41-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>model_2</span><span>$</span><span>ga</span><span>$</span><span>final</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  [1] &quot;radius_mean&quot;                  &quot;texture_mean&quot;                
##  [3] &quot;perimeter_mean&quot;               &quot;area_mean&quot;                   
##  [5] &quot;smoothness_mean&quot;              &quot;compactness_mean&quot;            
##  [7] &quot;concavity_mean&quot;               &quot;symmetry_mean&quot;               
##  [9] &quot;fractal_dimension_mean&quot;       &quot;texture_se&quot;                  
## [11] &quot;perimeter_se&quot;                 &quot;area_se&quot;                     
## [13] &quot;smoothness_se&quot;                &quot;compactness_se&quot;              
## [15] &quot;concavity_se&quot;                 &quot;concave_points_se&quot;           
## [17] &quot;symmetry_se&quot;                  &quot;radius_largest_worst&quot;        
## [19] &quot;texture_largest_worst&quot;        &quot;smoothness_largest_worst&quot;    
## [21] &quot;compactness_largest_worst&quot;    &quot;concave_points_largest_worst&quot;
</code></pre>
</div>
<div>
<pre><code><span>bc_data_2_ga</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_2_train</span><span>[,</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span> </span><span>which</span><span>(</span><span>colnames</span><span>(</span><span>bc_data_2_train</span><span>)</span><span> </span><span>%in%</span><span> </span><span>model_2</span><span>$</span><span>ga</span><span>$</span><span>final</span><span>))]</span><span>
</span></code></pre>
</div>
<p></p>
<ul>
<li>Dataset 3</li>
</ul>
<div>
<pre><code><span>lev</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;R&quot;</span><span>,</span><span> </span><span>&quot;N&quot;</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_3</span><span> </span><span>&lt;-</span><span> </span><span>gafs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>bc_data_3_train</span><span>[,</span><span> </span><span>-1</span><span>],</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>bc_data_3_train</span><span>$</span><span>outcome</span><span>,</span><span>
</span><span>iters</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span># generations of algorithm
</span><span> </span><span>popSize</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span># population size for each generation
</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>lev</span><span>,</span><span>
</span><span>gafsControl</span><span> </span><span>=</span><span> </span><span>ga_ctrl</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>plot</span><span>(</span><span>model_3</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-44-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>model_3</span><span>$</span><span>ga</span><span>$</span><span>final</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  [1] &quot;time&quot;                            &quot;perimeter_mean&quot;                 
##  [3] &quot;radius_se&quot;                       &quot;texture_se&quot;                     
##  [5] &quot;compactness_se&quot;                  &quot;concavity_se&quot;                   
##  [7] &quot;symmetry_se&quot;                     &quot;fractal_dimension_se&quot;           
##  [9] &quot;radius_largest_worst&quot;            &quot;texture_largest_worst&quot;          
## [11] &quot;perimeter_largest_worst&quot;         &quot;smoothness_largest_worst&quot;       
## [13] &quot;compactness_largest_worst&quot;       &quot;concavity_largest_worst&quot;        
## [15] &quot;concave_points_largest_worst&quot;    &quot;symmetry_largest_worst&quot;         
## [17] &quot;fractal_dimension_largest_worst&quot; &quot;tumor_size&quot;                     
## [19] &quot;lymph_node_status&quot;
</code></pre>
</div>
<div>
<pre><code><span>bc_data_3_ga</span><span> </span><span>&lt;-</span><span> </span><span>bc_data_3_train</span><span>[,</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>which</span><span>(</span><span>colnames</span><span>(</span><span>bc_data_3_train</span><span>)</span><span> </span><span>%in%</span><span> </span><span>model_3</span><span>$</span><span>ga</span><span>$</span><span>final</span><span>))]</span><span>
</span></code></pre>
</div>
<p></p>
<h1>Model comparison</h1>
<p>Now I can compare Random Forest models with the different feature subsets. (I chose Random Forests because I find it works reasonbly well on a large variety of data and because it’s the algorithm I’ve used most often and thus know most about…)</p>
<p>For a more detailed description of building machine learning models see <a target="_blank" rel="nofollow" href="https://shiring.github.io/machine_learning/2016/11/27/flu_outcome_ML_post">here</a> and <a target="_blank" rel="nofollow" href="https://shiring.github.io/machine_learning/2016/12/02/flu_outcome_ML_2_post">here</a>.</p>
<p></p>
<h2>All features</h2>
<ul>
<li>Dataset 1</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_all</span><span> </span><span>&lt;-</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>bc_data_train</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>5</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>cm_all_1</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_all</span><span>,</span><span> </span><span>bc_data_test</span><span>[,</span><span> </span><span>-1</span><span>]),</span><span> </span><span>bc_data_test</span><span>$</span><span>classes</span><span>)</span><span>
</span><span>cm_all_1</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##            Reference
## Prediction  benign malignant
##   benign       134         5
##   malignant      3        67
##                                          
##                Accuracy : 0.9617         
##                  95% CI : (0.926, 0.9833)
##     No Information Rate : 0.6555         
##     P-Value [Acc > NIR] : &lt;2e-16         
##                                          
##                   Kappa : 0.9147         
##  Mcnemar's Test P-Value : 0.7237         
##                                          
##             Sensitivity : 0.9781         
##             Specificity : 0.9306         
##          Pos Pred Value : 0.9640         
##          Neg Pred Value : 0.9571         
##              Prevalence : 0.6555         
##          Detection Rate : 0.6411         
##    Detection Prevalence : 0.6651         
##       Balanced Accuracy : 0.9543         
##                                          
##        'Positive' Class : benign         
## 
</code></pre>
</div>
<p></p>
<ul>
<li>Dataset 2</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_2_all</span><span> </span><span>&lt;-</span><span> </span><span>train</span><span>(</span><span>diagnosis</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_2_train</span><span>[,</span><span> </span><span>-1</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>5</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>cm_all_2</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_2_all</span><span>,</span><span> </span><span>bc_data_2_test</span><span>[,</span><span> </span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>)]),</span><span> </span><span>bc_data_2_test</span><span>$</span><span>diagnosis</span><span>)</span><span>
</span><span>cm_all_2</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   B   M
##          B 106   5
##          M   1  58
##                                           
##                Accuracy : 0.9647          
##                  95% CI : (0.9248, 0.9869)
##     No Information Rate : 0.6294          
##     P-Value [Acc > NIR] : &lt;2e-16          
##                                           
##                   Kappa : 0.9233          
##  Mcnemar's Test P-Value : 0.2207          
##                                           
##             Sensitivity : 0.9907          
##             Specificity : 0.9206          
##          Pos Pred Value : 0.9550          
##          Neg Pred Value : 0.9831          
##              Prevalence : 0.6294          
##          Detection Rate : 0.6235          
##    Detection Prevalence : 0.6529          
##       Balanced Accuracy : 0.9556          
##                                           
##        'Positive' Class : B               
## 
</code></pre>
</div>
<p></p>
<ul>
<li>Dataset 3</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_3_all</span><span> </span><span>&lt;-</span><span> </span><span>train</span><span>(</span><span>outcome</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_3_train</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>5</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>cm_all_3</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_3_all</span><span>,</span><span> </span><span>bc_data_3_test</span><span>[,</span><span> </span><span>-1</span><span>]),</span><span> </span><span>bc_data_3_test</span><span>$</span><span>outcome</span><span>)</span><span>
</span><span>cm_all_3</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  N  R
##          N 42  7
##          R  3  7
##                                           
##                Accuracy : 0.8305          
##                  95% CI : (0.7103, 0.9156)
##     No Information Rate : 0.7627          
##     P-Value [Acc > NIR] : 0.1408          
##                                           
##                   Kappa : 0.4806          
##  Mcnemar's Test P-Value : 0.3428          
##                                           
##             Sensitivity : 0.9333          
##             Specificity : 0.5000          
##          Pos Pred Value : 0.8571          
##          Neg Pred Value : 0.7000          
##              Prevalence : 0.7627          
##          Detection Rate : 0.7119          
##    Detection Prevalence : 0.8305          
##       Balanced Accuracy : 0.7167          
##                                           
##        'Positive' Class : N               
## 
</code></pre>
</div>
<p></p>
<h2>Selected features</h2>
<h3>Dataset 1</h3>
<div>
<pre><code><span>library</span><span>(</span><span>gplots</span><span>)</span><span>
</span><span>venn_list</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>cor</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>bc_data_cor</span><span>)[</span><span>-1</span><span>],</span><span>
</span><span>rfe</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>bc_data_rfe</span><span>)[</span><span>-1</span><span>],</span><span>
</span><span>ga</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>bc_data_ga</span><span>)[</span><span>-1</span><span>])</span><span>
</span><span>venn</span><span> </span><span>&lt;-</span><span> </span><span>venn</span><span>(</span><span>venn_list</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i1.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-54-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>venn</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##     num cor rfe ga
## 000   0   0   0  0
## 001   0   0   0  1
## 010   1   0   1  0
## 011   2   0   1  1
## 100   0   1   0  0
## 101   0   1   0  1
## 110   0   1   1  0
## 111   6   1   1  1
## attr(,&quot;intersections&quot;)
## attr(,&quot;intersections&quot;)$`cor:rfe:ga`
## [1] &quot;clump_thickness&quot;             &quot;marginal_adhesion&quot;          
## [3] &quot;single_epithelial_cell_size&quot; &quot;bland_chromatin&quot;            
## [5] &quot;normal_nucleoli&quot;             &quot;mitosis&quot;                    
## 
## attr(,&quot;intersections&quot;)$rfe
## [1] &quot;uniformity_of_cell_size&quot;
## 
## attr(,&quot;intersections&quot;)$`rfe:ga`
## [1] &quot;uniformity_of_cell_shape&quot; &quot;bare_nuclei&quot;             
## 
## attr(,&quot;class&quot;)
## [1] &quot;venn&quot;
</code></pre>
</div>
<p>4 out of 10 features were chosen by all three methods; the biggest overlap is seen between GA and RFE with 7 features. RFE and GA both retained 8 features for modeling, compared to only 5 based on the correlation method.</p>
<p></p>
<ul>
<li>Correlation</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_cor</span><span> </span><span>&lt;-</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>bc_data_cor</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>5</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>cm_cor_1</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_cor</span><span>,</span><span> </span><span>bc_data_test</span><span>[,</span><span> </span><span>-1</span><span>]),</span><span> </span><span>bc_data_test</span><span>$</span><span>classes</span><span>)</span><span>
</span><span>cm_cor_1</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##            Reference
## Prediction  benign malignant
##   benign       131         6
##   malignant      6        66
##                                         
##                Accuracy : 0.9426        
##                  95% CI : (0.9019, 0.97)
##     No Information Rate : 0.6555        
##     P-Value [Acc > NIR] : &lt;2e-16        
##                                         
##                   Kappa : 0.8729        
##  Mcnemar's Test P-Value : 1             
##                                         
##             Sensitivity : 0.9562        
##             Specificity : 0.9167        
##          Pos Pred Value : 0.9562        
##          Neg Pred Value : 0.9167        
##              Prevalence : 0.6555        
##          Detection Rate : 0.6268        
##    Detection Prevalence : 0.6555        
##       Balanced Accuracy : 0.9364        
##                                         
##        'Positive' Class : benign        
## 
</code></pre>
</div>
<p></p>
<ul>
<li>RFE</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_rfe</span><span> </span><span>&lt;-</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>bc_data_rfe</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>5</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>cm_rfe_1</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_rfe</span><span>,</span><span> </span><span>bc_data_test</span><span>[,</span><span> </span><span>-1</span><span>]),</span><span> </span><span>bc_data_test</span><span>$</span><span>classes</span><span>)</span><span>
</span><span>cm_rfe_1</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##            Reference
## Prediction  benign malignant
##   benign       134         4
##   malignant      3        68
##                                           
##                Accuracy : 0.9665          
##                  95% CI : (0.9322, 0.9864)
##     No Information Rate : 0.6555          
##     P-Value [Acc > NIR] : &lt;2e-16          
##                                           
##                   Kappa : 0.9256          
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9781          
##             Specificity : 0.9444          
##          Pos Pred Value : 0.9710          
##          Neg Pred Value : 0.9577          
##              Prevalence : 0.6555          
##          Detection Rate : 0.6411          
##    Detection Prevalence : 0.6603          
##       Balanced Accuracy : 0.9613          
##                                           
##        'Positive' Class : benign          
## 
</code></pre>
</div>
<p></p>
<ul>
<li>GA</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_ga</span><span> </span><span>&lt;-</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>bc_data_ga</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>5</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>cm_ga_1</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_ga</span><span>,</span><span> </span><span>bc_data_test</span><span>[,</span><span> </span><span>-1</span><span>]),</span><span> </span><span>bc_data_test</span><span>$</span><span>classes</span><span>)</span><span>
</span><span>cm_ga_1</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##            Reference
## Prediction  benign malignant
##   benign       134         3
##   malignant      3        69
##                                           
##                Accuracy : 0.9713          
##                  95% CI : (0.9386, 0.9894)
##     No Information Rate : 0.6555          
##     P-Value [Acc > NIR] : &lt;2e-16          
##                                           
##                   Kappa : 0.9364          
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9781          
##             Specificity : 0.9583          
##          Pos Pred Value : 0.9781          
##          Neg Pred Value : 0.9583          
##              Prevalence : 0.6555          
##          Detection Rate : 0.6411          
##    Detection Prevalence : 0.6555          
##       Balanced Accuracy : 0.9682          
##                                           
##        'Positive' Class : benign          
## 
</code></pre>
</div>
<p></p>
<h3>Dataset 2</h3>
<div>
<pre><code><span>venn_list</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>cor</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>bc_data_2_cor</span><span>)[</span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>)],</span><span>
</span><span>rfe</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>bc_data_2_rfe</span><span>)[</span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>)],</span><span>
</span><span>ga</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>bc_data_2_ga</span><span>)[</span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>)])</span><span>
</span><span>venn</span><span> </span><span>&lt;-</span><span> </span><span>venn</span><span>(</span><span>venn_list</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i0.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-64-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>venn</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##     num cor rfe ga
## 000   0   0   0  0
## 001   2   0   0  1
## 010   5   0   1  0
## 011  13   0   1  1
## 100   2   1   0  0
## 101   4   1   0  1
## 110   2   1   1  0
## 111   2   1   1  1
## attr(,&quot;intersections&quot;)
## attr(,&quot;intersections&quot;)$`cor:rfe:ga`
## [1] &quot;texture_mean&quot;             &quot;smoothness_largest_worst&quot;
## 
## attr(,&quot;intersections&quot;)$ga
## [1] &quot;compactness_se&quot;    &quot;concave_points_se&quot;
## 
## attr(,&quot;intersections&quot;)$cor
## [1] &quot;radius_mean&quot;          &quot;fractal_dimension_se&quot;
## 
## attr(,&quot;intersections&quot;)$rfe
## [1] &quot;concave_points_mean&quot;             &quot;perimeter_largest_worst&quot;        
## [3] &quot;area_largest_worst&quot;              &quot;concavity_largest_worst&quot;        
## [5] &quot;fractal_dimension_largest_worst&quot;
## 
## attr(,&quot;intersections&quot;)$`cor:ga`
## [1] &quot;fractal_dimension_mean&quot; &quot;texture_se&quot;            
## [3] &quot;smoothness_se&quot;          &quot;symmetry_se&quot;           
## 
## attr(,&quot;intersections&quot;)$`rfe:ga`
##  [1] &quot;perimeter_mean&quot;               &quot;area_mean&quot;                   
##  [3] &quot;smoothness_mean&quot;              &quot;compactness_mean&quot;            
##  [5] &quot;concavity_mean&quot;               &quot;symmetry_mean&quot;               
##  [7] &quot;perimeter_se&quot;                 &quot;area_se&quot;                     
##  [9] &quot;concavity_se&quot;                 &quot;radius_largest_worst&quot;        
## [11] &quot;texture_largest_worst&quot;        &quot;compactness_largest_worst&quot;   
## [13] &quot;concave_points_largest_worst&quot;
## 
## attr(,&quot;intersections&quot;)$`cor:rfe`
## [1] &quot;radius_se&quot;              &quot;symmetry_largest_worst&quot;
## 
## attr(,&quot;class&quot;)
## [1] &quot;venn&quot;
</code></pre>
</div>
<p>For dataset 2 we see a much bigger variation in chosen features between the three selection methods: only 1 feature was chosen by all and the biggest overlap is again seen between RFE and GA, followed by correlation and GA. But this time we also see quite a few features that are uniquely retained by any of the three feature selection methods.</p>
<p></p>
<ul>
<li>Correlation</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_2_cor</span><span> </span><span>&lt;-</span><span> </span><span>train</span><span>(</span><span>diagnosis</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_2_cor</span><span>[,</span><span> </span><span>-1</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>5</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>cm_cor_2</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_2_cor</span><span>,</span><span> </span><span>bc_data_2_test</span><span>[,</span><span> </span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>)]),</span><span> </span><span>bc_data_2_test</span><span>$</span><span>diagnosis</span><span>)</span><span>
</span><span>cm_cor_2</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   B   M
##          B 106   6
##          M   1  57
##                                          
##                Accuracy : 0.9588         
##                  95% CI : (0.917, 0.9833)
##     No Information Rate : 0.6294         
##     P-Value [Acc > NIR] : &lt;2e-16         
##                                          
##                   Kappa : 0.9103         
##  Mcnemar's Test P-Value : 0.1306         
##                                          
##             Sensitivity : 0.9907         
##             Specificity : 0.9048         
##          Pos Pred Value : 0.9464         
##          Neg Pred Value : 0.9828         
##              Prevalence : 0.6294         
##          Detection Rate : 0.6235         
##    Detection Prevalence : 0.6588         
##       Balanced Accuracy : 0.9477         
##                                          
##        'Positive' Class : B              
## 
</code></pre>
</div>
<p></p>
<ul>
<li>RFE</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_2_rfe</span><span> </span><span>&lt;-</span><span> </span><span>train</span><span>(</span><span>diagnosis</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_2_rfe</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>5</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>cm_rfe_2</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_2_rfe</span><span>,</span><span> </span><span>bc_data_2_test</span><span>[,</span><span> </span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>)]),</span><span> </span><span>bc_data_2_test</span><span>$</span><span>diagnosis</span><span>)</span><span>
</span><span>cm_rfe_2</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   B   M
##          B 106   5
##          M   1  58
##                                           
##                Accuracy : 0.9647          
##                  95% CI : (0.9248, 0.9869)
##     No Information Rate : 0.6294          
##     P-Value [Acc > NIR] : &lt;2e-16          
##                                           
##                   Kappa : 0.9233          
##  Mcnemar's Test P-Value : 0.2207          
##                                           
##             Sensitivity : 0.9907          
##             Specificity : 0.9206          
##          Pos Pred Value : 0.9550          
##          Neg Pred Value : 0.9831          
##              Prevalence : 0.6294          
##          Detection Rate : 0.6235          
##    Detection Prevalence : 0.6529          
##       Balanced Accuracy : 0.9556          
##                                           
##        'Positive' Class : B               
## 
</code></pre>
</div>
<p></p>
<ul>
<li>GA</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_2_ga</span><span> </span><span>&lt;-</span><span> </span><span>train</span><span>(</span><span>diagnosis</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_2_ga</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>5</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>cm_ga_2</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_2_ga</span><span>,</span><span> </span><span>bc_data_2_test</span><span>[,</span><span> </span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>)]),</span><span> </span><span>bc_data_2_test</span><span>$</span><span>diagnosis</span><span>)</span><span>
</span><span>cm_ga_2</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   B   M
##          B 106   4
##          M   1  59
##                                           
##                Accuracy : 0.9706          
##                  95% CI : (0.9327, 0.9904)
##     No Information Rate : 0.6294          
##     P-Value [Acc > NIR] : &lt;2e-16          
##                                           
##                   Kappa : 0.9363          
##  Mcnemar's Test P-Value : 0.3711          
##                                           
##             Sensitivity : 0.9907          
##             Specificity : 0.9365          
##          Pos Pred Value : 0.9636          
##          Neg Pred Value : 0.9833          
##              Prevalence : 0.6294          
##          Detection Rate : 0.6235          
##    Detection Prevalence : 0.6471          
##       Balanced Accuracy : 0.9636          
##                                           
##        'Positive' Class : B               
## 
</code></pre>
</div>
<p></p>
<h3>Dataset 3</h3>
<div>
<pre><code><span>venn_list</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>cor</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>bc_data_3_cor</span><span>)[</span><span>-1</span><span>],</span><span>
</span><span>rfe</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>bc_data_3_rfe</span><span>)[</span><span>-1</span><span>],</span><span>
</span><span>ga</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>bc_data_3_ga</span><span>)[</span><span>-1</span><span>])</span><span>
</span><span>venn</span><span> </span><span>&lt;-</span><span> </span><span>venn</span><span>(</span><span>venn_list</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i1.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-74-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>venn</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##     num cor rfe ga
## 000   0   0   0  0
## 001   5   0   0  1
## 010   6   0   1  0
## 011   2   0   1  1
## 100   2   1   0  0
## 101   4   1   0  1
## 110   1   1   1  0
## 111   8   1   1  1
## attr(,&quot;intersections&quot;)
## attr(,&quot;intersections&quot;)$`cor:rfe:ga`
## [1] &quot;time&quot;                         &quot;radius_se&quot;                   
## [3] &quot;symmetry_se&quot;                  &quot;smoothness_largest_worst&quot;    
## [5] &quot;concave_points_largest_worst&quot; &quot;symmetry_largest_worst&quot;      
## [7] &quot;tumor_size&quot;                   &quot;lymph_node_status&quot;           
## 
## attr(,&quot;intersections&quot;)$ga
## [1] &quot;perimeter_mean&quot;          &quot;compactness_se&quot;         
## [3] &quot;texture_largest_worst&quot;   &quot;perimeter_largest_worst&quot;
## [5] &quot;concavity_largest_worst&quot;
## 
## attr(,&quot;intersections&quot;)$cor
## [1] &quot;texture_mean&quot;  &quot;smoothness_se&quot;
## 
## attr(,&quot;intersections&quot;)$rfe
## [1] &quot;compactness_mean&quot;       &quot;concavity_mean&quot;        
## [3] &quot;concave_points_mean&quot;    &quot;symmetry_mean&quot;         
## [5] &quot;fractal_dimension_mean&quot; &quot;perimeter_se&quot;          
## 
## attr(,&quot;intersections&quot;)$`cor:ga`
## [1] &quot;texture_se&quot;                &quot;fractal_dimension_se&quot;     
## [3] &quot;radius_largest_worst&quot;      &quot;compactness_largest_worst&quot;
## 
## attr(,&quot;intersections&quot;)$`rfe:ga`
## [1] &quot;concavity_se&quot;                    &quot;fractal_dimension_largest_worst&quot;
## 
## attr(,&quot;intersections&quot;)$`cor:rfe`
## [1] &quot;concave_points_se&quot;
## 
## attr(,&quot;class&quot;)
## [1] &quot;venn&quot;
</code></pre>
</div>
<p>With the third dataset there is still some variation among the selected features but not as extreme as with dataset 2. This time, we find the biggest overlap between correlation and GA.</p>
<p></p>
<ul>
<li>Correlation</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_3_cor</span><span> </span><span>&lt;-</span><span> </span><span>train</span><span>(</span><span>outcome</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_3_cor</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>5</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>cm_cor_3</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_3_cor</span><span>,</span><span> </span><span>bc_data_3_test</span><span>[,</span><span> </span><span>-1</span><span>]),</span><span> </span><span>bc_data_3_test</span><span>$</span><span>outcome</span><span>)</span><span>
</span><span>cm_cor_3</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  N  R
##          N 41  7
##          R  4  7
##                                           
##                Accuracy : 0.8136          
##                  95% CI : (0.6909, 0.9031)
##     No Information Rate : 0.7627          
##     P-Value [Acc > NIR] : 0.2256          
##                                           
##                   Kappa : 0.4439          
##  Mcnemar's Test P-Value : 0.5465          
##                                           
##             Sensitivity : 0.9111          
##             Specificity : 0.5000          
##          Pos Pred Value : 0.8542          
##          Neg Pred Value : 0.6364          
##              Prevalence : 0.7627          
##          Detection Rate : 0.6949          
##    Detection Prevalence : 0.8136          
##       Balanced Accuracy : 0.7056          
##                                           
##        'Positive' Class : N               
## 
</code></pre>
</div>
<p></p>
<ul>
<li>RFE</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_3_rfe</span><span> </span><span>&lt;-</span><span> </span><span>train</span><span>(</span><span>outcome</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_3_rfe</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>5</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>cm_rfe_3</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_3_rfe</span><span>,</span><span> </span><span>bc_data_3_test</span><span>[,</span><span> </span><span>-1</span><span>]),</span><span> </span><span>bc_data_3_test</span><span>$</span><span>outcome</span><span>)</span><span>
</span><span>cm_rfe_3</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  N  R
##          N 40  7
##          R  5  7
##                                           
##                Accuracy : 0.7966          
##                  95% CI : (0.6717, 0.8902)
##     No Information Rate : 0.7627          
##     P-Value [Acc > NIR] : 0.3311          
##                                           
##                   Kappa : 0.409           
##  Mcnemar's Test P-Value : 0.7728          
##                                           
##             Sensitivity : 0.8889          
##             Specificity : 0.5000          
##          Pos Pred Value : 0.8511          
##          Neg Pred Value : 0.5833          
##              Prevalence : 0.7627          
##          Detection Rate : 0.6780          
##    Detection Prevalence : 0.7966          
##       Balanced Accuracy : 0.6944          
##                                           
##        'Positive' Class : N               
## 
</code></pre>
</div>
<p></p>
<ul>
<li>GA</li>
</ul>
<div>
<pre><code><span>set.seed</span><span>(</span><span>27</span><span>)</span><span>
</span><span>model_bc_data_3_ga</span><span> </span><span>&lt;-</span><span> </span><span>train</span><span>(</span><span>outcome</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>bc_data_3_ga</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>5</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>cm_ga_3</span><span> </span><span>&lt;-</span><span> </span><span>confusionMatrix</span><span>(</span><span>predict</span><span>(</span><span>model_bc_data_3_ga</span><span>,</span><span> </span><span>bc_data_3_test</span><span>[,</span><span> </span><span>-1</span><span>]),</span><span> </span><span>bc_data_3_test</span><span>$</span><span>outcome</span><span>)</span><span>
</span><span>cm_ga_3</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  N  R
##          N 42  7
##          R  3  7
##                                           
##                Accuracy : 0.8305          
##                  95% CI : (0.7103, 0.9156)
##     No Information Rate : 0.7627          
##     P-Value [Acc > NIR] : 0.1408          
##                                           
##                   Kappa : 0.4806          
##  Mcnemar's Test P-Value : 0.3428          
##                                           
##             Sensitivity : 0.9333          
##             Specificity : 0.5000          
##          Pos Pred Value : 0.8571          
##          Neg Pred Value : 0.7000          
##              Prevalence : 0.7627          
##          Detection Rate : 0.7119          
##    Detection Prevalence : 0.8305          
##       Balanced Accuracy : 0.7167          
##                                           
##        'Positive' Class : N               
## 
</code></pre>
</div>
<p></p>
<h2>Overall model parameters</h2>
<p>To compare the feature selection methods’ influence on the models built with the three different datasets, I am going to plot model accuracy, Kappa, precision and sensitivity and specificity.</p>
<div>
<pre><code><span>overall</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>dataset</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;1&quot;</span><span>,</span><span> </span><span>&quot;2&quot;</span><span>,</span><span> </span><span>&quot;3&quot;</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>4</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;all&quot;</span><span>,</span><span> </span><span>&quot;cor&quot;</span><span>,</span><span> </span><span>&quot;rfe&quot;</span><span>,</span><span> </span><span>&quot;ga&quot;</span><span>),</span><span> </span><span>3</span><span>),</span><span>
</span><span>rbind</span><span>(</span><span>cm_all_1</span><span>$</span><span>overall</span><span>,</span><span>
</span><span>cm_cor_1</span><span>$</span><span>overall</span><span>,</span><span>
</span><span>cm_rfe_1</span><span>$</span><span>overall</span><span>,</span><span>
</span><span>cm_ga_1</span><span>$</span><span>overall</span><span>,</span><span>
</span><span>cm_all_2</span><span>$</span><span>overall</span><span>,</span><span>
</span><span>cm_cor_2</span><span>$</span><span>overall</span><span>,</span><span>
</span><span>cm_rfe_2</span><span>$</span><span>overall</span><span>,</span><span>
</span><span>cm_ga_2</span><span>$</span><span>overall</span><span>,</span><span>
</span><span>cm_all_3</span><span>$</span><span>overall</span><span>,</span><span>
</span><span>cm_cor_3</span><span>$</span><span>overall</span><span>,</span><span>
</span><span>cm_rfe_3</span><span>$</span><span>overall</span><span>,</span><span>
</span><span>cm_ga_3</span><span>$</span><span>overall</span><span>))</span><span>
</span><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>overall_gather</span><span> </span><span>&lt;-</span><span> </span><span>overall</span><span>[,</span><span> </span><span>1</span><span>:</span><span>4</span><span>]</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>measure</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>Accuracy</span><span>:</span><span>Kappa</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>byClass</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>dataset</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;1&quot;</span><span>,</span><span> </span><span>&quot;2&quot;</span><span>,</span><span> </span><span>&quot;3&quot;</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>4</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;all&quot;</span><span>,</span><span> </span><span>&quot;cor&quot;</span><span>,</span><span> </span><span>&quot;rfe&quot;</span><span>,</span><span> </span><span>&quot;ga&quot;</span><span>),</span><span> </span><span>3</span><span>),</span><span>
</span><span>rbind</span><span>(</span><span>cm_all_1</span><span>$</span><span>byClass</span><span>,</span><span>
</span><span>cm_cor_1</span><span>$</span><span>byClass</span><span>,</span><span>
</span><span>cm_rfe_1</span><span>$</span><span>byClass</span><span>,</span><span>
</span><span>cm_ga_1</span><span>$</span><span>byClass</span><span>,</span><span>
</span><span>cm_all_2</span><span>$</span><span>byClass</span><span>,</span><span>
</span><span>cm_cor_2</span><span>$</span><span>byClass</span><span>,</span><span>
</span><span>cm_rfe_2</span><span>$</span><span>byClass</span><span>,</span><span>
</span><span>cm_ga_2</span><span>$</span><span>byClass</span><span>,</span><span>
</span><span>cm_all_3</span><span>$</span><span>byClass</span><span>,</span><span>
</span><span>cm_cor_3</span><span>$</span><span>byClass</span><span>,</span><span>
</span><span>cm_rfe_3</span><span>$</span><span>byClass</span><span>,</span><span>
</span><span>cm_ga_3</span><span>$</span><span>byClass</span><span>))</span><span>
</span><span>byClass_gather</span><span> </span><span>&lt;-</span><span> </span><span>byClass</span><span>[,</span><span> </span><span>c</span><span>(</span><span>1</span><span>:</span><span>4</span><span>,</span><span> </span><span>7</span><span>)]</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>measure</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>Sensitivity</span><span>:</span><span>Precision</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>overall_byClass_gather</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>overall_gather</span><span>,</span><span> </span><span>byClass_gather</span><span>)</span><span>
</span><span>overall_byClass_gather</span><span> </span><span>&lt;-</span><span> </span><span>within</span><span>(</span><span>overall_byClass_gather</span><span>,</span><span> </span><span>model</span><span> </span><span>&lt;-</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;all&quot;</span><span>,</span><span> </span><span>&quot;cor&quot;</span><span>,</span><span> </span><span>&quot;rfe&quot;</span><span>,</span><span> </span><span>&quot;ga&quot;</span><span>)))</span><span>
</span><span>ggplot</span><span>(</span><span>overall_byClass_gather</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>model</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>measure</span><span>,</span><span> </span><span>shape</span><span> </span><span>=</span><span> </span><span>measure</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>measure</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.8</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_path</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.7</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>facet_grid</span><span>(</span><span>dataset</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Feature Selection method&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Value&quot;</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>shape</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Comparison of feature selection methods&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;in three breast cancer datasets&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;\nBreast Cancer Wisconsin (Diagnostic) Data Sets: 1, 2 &amp; 3
Street et al., 1993;
all: no feature selection
cor: features with correlation > 0.7 removed
rfe: Recursive Feature Elimination
ga: Genetic Algorithm&quot;</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i0.wp.com/shiring.github.io/machine_learning/2017/01/15/rfe_ga_post_files/figure-markdown_github/unnamed-chunk-86-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Conclusions</h2>
<p>As expected from PCA of sample classes, which showed that the two classes recur/ non-recur did not cluster well, Random Forest models of dataset 3 had much lower prediction accuracy than models of datasets 1 and 2.</p>
<p>The correlation method operates regardless of feature importance. E.g. in dataset 1, the features with the highest importance were also flagged as highly correlated. Correlation models performed worst in all three datasets. RFE and GA tend to include features with high importance, but feature importance alone is not a good indicator for whether several features will work well in combination when predicting an outcome.</p>
<p>Dataset 1 was small with only 9 features; here, removing highly correlated features was the least successful selection method. RFE and GA both improved the predictions compared to no feature selection, while GA performed best. Dataset 2 with its 30 original features produced the best models with the GA. And in dataset 3, which had lower overall accuracy, different feature selection methods did not have a strong influence.</p>
<hr>
<p></p>
<div>
<pre><code><span>sessionInfo</span><span>()</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## R version 3.3.2 (2016-10-31)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Sierra 10.12.1
## 
## locale:
## [1] C
## 
## attached base packages:
##  [1] grid      stats4    parallel  stats     graphics  grDevices utils    
##  [8] datasets  methods   base     
## 
## other attached packages:
##  [1] gplots_3.0.1         dplyr_0.5.0          plyr_1.8.4          
##  [4] corrplot_0.77        randomForest_4.6-12  doParallel_1.0.10   
##  [7] iterators_1.0.8      foreach_1.4.3        caret_6.0-73        
## [10] lattice_0.20-34      tidyr_0.6.0          gridExtra_2.2.1     
## [13] pcaGoPromoter_1.18.0 Biostrings_2.42.1    XVector_0.14.0      
## [16] IRanges_2.8.1        S4Vectors_0.12.1     BiocGenerics_0.20.0 
## [19] ellipse_0.3-8        ggplot2_2.2.1        mice_2.25           
## [22] Rcpp_0.12.8         
## 
## loaded via a namespace (and not attached):
##  [1] gtools_3.5.0         class_7.3-14         assertthat_0.1      
##  [4] rprojroot_1.1        digest_0.6.11        R6_2.2.0            
##  [7] backports_1.0.4      MatrixModels_0.4-1   RSQLite_1.1-1       
## [10] evaluate_0.10        e1071_1.6-7          zlibbioc_1.20.0     
## [13] lazyeval_0.2.0       gdata_2.17.0         minqa_1.2.4         
## [16] SparseM_1.74         car_2.1-4            nloptr_1.0.4        
## [19] rpart_4.1-10         Matrix_1.2-7.1       rmarkdown_1.3       
## [22] labeling_0.3         splines_3.3.2        lme4_1.1-12         
## [25] stringr_1.1.0        munsell_0.4.3        compiler_3.3.2      
## [28] mgcv_1.8-16          htmltools_0.3.5      nnet_7.3-12         
## [31] tibble_1.2           codetools_0.2-15     bitops_1.0-6        
## [34] MASS_7.3-45          ModelMetrics_1.1.0   nlme_3.1-128        
## [37] gtable_0.2.0         DBI_0.5-1            magrittr_1.5        
## [40] scales_0.4.1         KernSmooth_2.23-15   stringi_1.1.2       
## [43] reshape2_1.4.2       RColorBrewer_1.1-2   tools_3.3.2         
## [46] Biobase_2.34.0       pbkrtest_0.4-6       survival_2.40-1     
## [49] yaml_2.1.14          AnnotationDbi_1.36.0 colorspace_1.3-2    
## [52] caTools_1.17.1       memoise_1.0.0        knitr_1.15.1        
## [55] quantreg_5.29
</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/01/15/rfe_ga_post"> 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/pog_hm6_rBw"></a></div>