<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/chaid-and-r-when-you-need-explanation-may-15-2018/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ibecav.github.io/chaidtutor1/"> Chuck Powell</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A modern data scientist using R has access to an almost bewildering<br>
number of tools, libraries and algorithms to analyze the data. In my<br>
next two posts I’m going to focus on an in depth visit with CHAID<br>
(Chi-square automatic interaction detection). The title should give you<br>
a hint for why I think CHAID is a good “tool” for your analytical<br>
toolbox. There are lots of tools that can help you predict or classify<br>
but CHAID is especially good at helping you explain to <strong>any audience</strong><br>
how the model arrives at it’s prediction or classification. It’s also<br>
incredibly robust from a statistical perspective, making almost no<br>
assumptions about your data for distribution or normality. I’ll try and<br>
elaborate on that as we work the example.</p>
<p>You can get a very brief summary of CHAID from<br>
<a rel="nofollow" href="https://en.wikipedia.org/wiki/Chi-square_automatic_interaction_detection" target="_blank">wikipedia</a><br>
and mentions of it scattered about in places like <a rel="nofollow" href="https://www.analyticsvidhya.com/blog/2016/04/complete-tutorial-tree-based-modeling-scratch-in-python/" target="_blank">Analytics<br>
Vidhya</a><br>
or <a rel="nofollow" href="https://data-flair.training/blogs/r-decision-trees/" target="_blank">Data Flair</a>. If<br>
you prefer a more scholarly bent the original article can be found in<br>
places like <a rel="nofollow" href="https://www.jstor.org/stable/2986296" target="_blank">JSTOR</a>. As the name<br>
implies it is fundamentally based on the venerable Chi-square test – and<br>
while not the most powerful (in terms of detecting the smallest possible<br>
differences) or the fastest, it really is easy to manage and more<br>
importantly to tell the story after using it.</p>
<p>Compared to some other techniques it’s also quite simple to use, as I<br>
hope you’ll agree, by the end of these posts. To showcase it we’re going<br>
to be using a dataset that comes to us from the <a rel="nofollow" href="https://www.ibm.com/communities/analytics/watson-analytics-blog/hr-employee-attrition/" target="_blank">IBM Watson<br>
Project</a><br>
and comes packaged with the <code>rsample</code> library. It’s a very practical and<br>
understandable dataset. A great use case for a tree based algorithm.<br>
Imagine yourself in a fictional company faced with the task of trying to<br>
figure out which employees you are going to “lose” a.k.a. attrition or<br>
turnover. There’s a steep cost involved in keeping good employees and<br>
training and on-boarding can be expensive. Being able to predict<br>
attrition even a little bit better would save you lots of money and make<br>
the company better, especially if you can understand exactly what you<br>
have to “watch out” for that might indicate the person is a high risk to<br>
leave.</p>
<h2>Setup and library loading</h2>
<p>If you’ve never used <code>CHAID</code> before you may also not have <code>partykit</code>.<br>
<code>CHAID</code> isn’t on <code>CRAN</code> but I have commented out the install command<br>
below. You’ll also get a variety of messages, none of which is relevant<br>
to this example so I’ve suppressed them.</p>
<div>
<div>
<pre><code><span># install.packages(&quot;partykit&quot;)</span><span>
</span><span># install.packages(&quot;CHAID&quot;, repos=&quot;http://R-Forge.R-project.org&quot;)</span><span>
</span><span>require</span><span>(</span><span>rsample</span><span>)</span><span> </span><span># for dataset and splitting also loads broom and tidyr</span><span>
</span><span>require</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>require</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>theme_set</span><span>(</span><span>theme_bw</span><span>())</span><span> </span><span># set theme</span><span>
</span><span>require</span><span>(</span><span>CHAID</span><span>)</span><span>
</span><span>require</span><span>(</span><span>purrr</span><span>)</span><span>
</span><span>require</span><span>(</span><span>caret</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<h2>Predicting attrition in a fictional company</h2>
<p>Let’s load up the <code>attrition</code> dataset and take a look at the variables<br>
we have.</p>
<div>
<div>
<pre><code><span># data(attrition)</span><span>
</span><span>str</span><span>(</span><span>attrition</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 'data.frame':    1470 obs. of  31 variables:
##  $ Age                     : int  41 49 37 33 27 32 59 30 38 36 ...
##  $ Attrition               : Factor w/ 2 levels &quot;No&quot;,&quot;Yes&quot;: 2 1 2 1 1 1 1 1 1 1 ...
##  $ BusinessTravel          : Factor w/ 3 levels &quot;Non-Travel&quot;,&quot;Travel_Frequently&quot;,..: 3 2 3 2 3 2 3 3 2 3 ...
##  $ DailyRate               : int  1102 279 1373 1392 591 1005 1324 1358 216 1299 ...
##  $ Department              : Factor w/ 3 levels &quot;Human_Resources&quot;,..: 3 2 2 2 2 2 2 2 2 2 ...
##  $ DistanceFromHome        : int  1 8 2 3 2 2 3 24 23 27 ...
##  $ Education               : Ord.factor w/ 5 levels &quot;Below_College&quot;&lt;..: 2 1 2 4 1 2 3 1 3 3 ...
##  $ EducationField          : Factor w/ 6 levels &quot;Human_Resources&quot;,..: 2 2 5 2 4 2 4 2 2 4 ...
##  $ EnvironmentSatisfaction : Ord.factor w/ 4 levels &quot;Low&quot;&lt;&quot;Medium&quot;&lt;..: 2 3 4 4 1 4 3 4 4 3 ...
##  $ Gender                  : Factor w/ 2 levels &quot;Female&quot;,&quot;Male&quot;: 1 2 2 1 2 2 1 2 2 2 ...
##  $ HourlyRate              : int  94 61 92 56 40 79 81 67 44 94 ...
##  $ JobInvolvement          : Ord.factor w/ 4 levels &quot;Low&quot;&lt;&quot;Medium&quot;&lt;..: 3 2 2 3 3 3 4 3 2 3 ...
##  $ JobLevel                : int  2 2 1 1 1 1 1 1 3 2 ...
##  $ JobRole                 : Factor w/ 9 levels &quot;Healthcare_Representative&quot;,..: 8 7 3 7 3 3 3 3 5 1 ...
##  $ JobSatisfaction         : Ord.factor w/ 4 levels &quot;Low&quot;&lt;&quot;Medium&quot;&lt;..: 4 2 3 3 2 4 1 3 3 3 ...
##  $ MaritalStatus           : Factor w/ 3 levels &quot;Divorced&quot;,&quot;Married&quot;,..: 3 2 3 2 2 3 2 1 3 2 ...
##  $ MonthlyIncome           : int  5993 5130 2090 2909 3468 3068 2670 2693 9526 5237 ...
##  $ MonthlyRate             : int  19479 24907 2396 23159 16632 11864 9964 13335 8787 16577 ...
##  $ NumCompaniesWorked      : int  8 1 6 1 9 0 4 1 0 6 ...
##  $ OverTime                : Factor w/ 2 levels &quot;No&quot;,&quot;Yes&quot;: 2 1 2 2 1 1 2 1 1 1 ...
##  $ PercentSalaryHike       : int  11 23 15 11 12 13 20 22 21 13 ...
##  $ PerformanceRating       : Ord.factor w/ 4 levels &quot;Low&quot;&lt;&quot;Good&quot;&lt;&quot;Excellent&quot;&lt;..: 3 4 3 3 3 3 4 4 4 3 ...
##  $ RelationshipSatisfaction: Ord.factor w/ 4 levels &quot;Low&quot;&lt;&quot;Medium&quot;&lt;..: 1 4 2 3 4 3 1 2 2 2 ...
##  $ StockOptionLevel        : int  0 1 0 0 1 0 3 1 0 2 ...
##  $ TotalWorkingYears       : int  8 10 7 8 6 8 12 1 10 17 ...
##  $ TrainingTimesLastYear   : int  0 3 3 3 3 2 3 2 2 3 ...
##  $ WorkLifeBalance         : Ord.factor w/ 4 levels &quot;Bad&quot;&lt;&quot;Good&quot;&lt;&quot;Better&quot;&lt;..: 1 3 3 3 3 2 2 3 3 2 ...
##  $ YearsAtCompany          : int  6 10 0 8 2 7 1 1 9 7 ...
##  $ YearsInCurrentRole      : int  4 7 0 7 2 7 0 0 7 7 ...
##  $ YearsSinceLastPromotion : int  0 1 0 3 2 3 0 0 1 7 ...
##  $ YearsWithCurrManager    : int  5 7 0 0 2 6 0 0 8 7 ...
</code></pre>
</div>
</div>
<p>Okay we have data on 1,470 employees. We have 30 potential predictor or<br>
independent variables and the all important <code>attrition</code> variable which<br>
gives us a yes or no answer to the question of whether or not the<br>
employee left. We’re to build the most accurate predictive model we can<br>
that is also simple (parsimonious) and explainable. The predictors we<br>
have seem to be the sorts of data we might have on hand in our HR files<br>
and thank goodness are labelled in a way that makes them pretty self<br>
explanatory.</p>
<p>The <code>CHAID</code> library in <code>R</code> requires that any variables that we enter as<br>
predictors be either nominal or ordinal variables (see <code>?CHAID::chaid</code>),<br>
which in R speak means we have to get them in as either <code>factor</code> or<br>
<code>ordered factor</code>. The <code>str</code> command shows we have a bunch of variables<br>
which are of type <code>integer</code>. As it turns out moving from integer to<br>
factor is simple in terms of code but has to be thoughtful for<br>
substantive reasons. So let’s see how things breakdown.</p>
<div>
<div>
<pre><code><span>attrition</span><span> </span><span>%>%</span><span>
</span><span>select_if</span><span>(</span><span>is.factor</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ncol</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 15
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>attrition</span><span> </span><span>%>%</span><span>
</span><span>select_if</span><span>(</span><span>is.numeric</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ncol</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 16
</code></pre>
</div>
</div>
<p>Hmmmm, 15 factors and 16 integers. Let’s explore further. Of the<br>
variables that are integers how many of them have a small number of<br>
values (a.k.a. levels) and can therefore be simply and easily converted<br>
to true factors. We’ll use a <code>dplyr pipe</code> to see how many have 5 or<br>
fewer levels and 10 or fewer levels.</p>
<div>
<div>
<pre><code><span>attrition</span><span> </span><span>%>%</span><span>
</span><span>select_if</span><span>(</span><span>function</span><span>(</span><span>col</span><span>)</span><span>
</span><span>length</span><span>(</span><span>unique</span><span>(</span><span>col</span><span>))</span><span> </span><span>&lt;=</span><span> </span><span>5</span><span> </span><span>&amp;</span><span> </span><span>is.integer</span><span>(</span><span>col</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>head</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##   JobLevel StockOptionLevel
## 1        2                0
## 2        2                1
## 4        1                0
## 5        1                0
## 7        1                1
## 8        1                0
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>attrition</span><span> </span><span>%>%</span><span>
</span><span>select_if</span><span>(</span><span>function</span><span>(</span><span>col</span><span>)</span><span>
</span><span>length</span><span>(</span><span>unique</span><span>(</span><span>col</span><span>))</span><span> </span><span>&lt;=</span><span> </span><span>10</span><span> </span><span>&amp;</span><span> </span><span>is.integer</span><span>(</span><span>col</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>head</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##   JobLevel NumCompaniesWorked StockOptionLevel TrainingTimesLastYear
## 1        2                  8                0                     0
## 2        2                  1                1                     3
## 4        1                  6                0                     3
## 5        1                  1                0                     3
## 7        1                  9                1                     3
## 8        1                  0                0                     2
</code></pre>
</div>
</div>
<p>2 and 4 respectively. We can be pretty confident that converting these<br>
from <code>integer</code> to <code>factor</code> won’t lose much information. Simple to run a<br>
<code>mutate</code> operation across the 4 we have identified. Probably more<br>
elegant though to make it a <code>mutate_if</code>. That way in the future we<br>
decide we like 4 or 7 or 122 as our criteria for the change we only have<br>
to change one number. The “if” variation is also less to type and less<br>
likely to make a manual mistake.</p>
<div>
<div>
<pre><code><span>attrition</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>
</span><span>JobLevel</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>JobLevel</span><span>),</span><span>
</span><span>NumCompaniesWorked</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>NumCompaniesWorked</span><span>),</span><span>
</span><span>StockOptionLevel</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>StockOptionLevel</span><span>),</span><span>
</span><span>TrainingTimesLastYear</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>TrainingTimesLastYear</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span> </span><span>str</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 'data.frame':    1470 obs. of  31 variables:
##  $ Age                     : int  41 49 37 33 27 32 59 30 38 36 ...
##  $ Attrition               : Factor w/ 2 levels &quot;No&quot;,&quot;Yes&quot;: 2 1 2 1 1 1 1 1 1 1 ...
##  $ BusinessTravel          : Factor w/ 3 levels &quot;Non-Travel&quot;,&quot;Travel_Frequently&quot;,..: 3 2 3 2 3 2 3 3 2 3 ...
##  $ DailyRate               : int  1102 279 1373 1392 591 1005 1324 1358 216 1299 ...
##  $ Department              : Factor w/ 3 levels &quot;Human_Resources&quot;,..: 3 2 2 2 2 2 2 2 2 2 ...
##  $ DistanceFromHome        : int  1 8 2 3 2 2 3 24 23 27 ...
##  $ Education               : Ord.factor w/ 5 levels &quot;Below_College&quot;&lt;..: 2 1 2 4 1 2 3 1 3 3 ...
##  $ EducationField          : Factor w/ 6 levels &quot;Human_Resources&quot;,..: 2 2 5 2 4 2 4 2 2 4 ...
##  $ EnvironmentSatisfaction : Ord.factor w/ 4 levels &quot;Low&quot;&lt;&quot;Medium&quot;&lt;..: 2 3 4 4 1 4 3 4 4 3 ...
##  $ Gender                  : Factor w/ 2 levels &quot;Female&quot;,&quot;Male&quot;: 1 2 2 1 2 2 1 2 2 2 ...
##  $ HourlyRate              : int  94 61 92 56 40 79 81 67 44 94 ...
##  $ JobInvolvement          : Ord.factor w/ 4 levels &quot;Low&quot;&lt;&quot;Medium&quot;&lt;..: 3 2 2 3 3 3 4 3 2 3 ...
##  $ JobLevel                : Factor w/ 5 levels &quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,..: 2 2 1 1 1 1 1 1 3 2 ...
##  $ JobRole                 : Factor w/ 9 levels &quot;Healthcare_Representative&quot;,..: 8 7 3 7 3 3 3 3 5 1 ...
##  $ JobSatisfaction         : Ord.factor w/ 4 levels &quot;Low&quot;&lt;&quot;Medium&quot;&lt;..: 4 2 3 3 2 4 1 3 3 3 ...
##  $ MaritalStatus           : Factor w/ 3 levels &quot;Divorced&quot;,&quot;Married&quot;,..: 3 2 3 2 2 3 2 1 3 2 ...
##  $ MonthlyIncome           : int  5993 5130 2090 2909 3468 3068 2670 2693 9526 5237 ...
##  $ MonthlyRate             : int  19479 24907 2396 23159 16632 11864 9964 13335 8787 16577 ...
##  $ NumCompaniesWorked      : Factor w/ 10 levels &quot;0&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,..: 9 2 7 2 10 1 5 2 1 7 ...
##  $ OverTime                : Factor w/ 2 levels &quot;No&quot;,&quot;Yes&quot;: 2 1 2 2 1 1 2 1 1 1 ...
##  $ PercentSalaryHike       : int  11 23 15 11 12 13 20 22 21 13 ...
##  $ PerformanceRating       : Ord.factor w/ 4 levels &quot;Low&quot;&lt;&quot;Good&quot;&lt;&quot;Excellent&quot;&lt;..: 3 4 3 3 3 3 4 4 4 3 ...
##  $ RelationshipSatisfaction: Ord.factor w/ 4 levels &quot;Low&quot;&lt;&quot;Medium&quot;&lt;..: 1 4 2 3 4 3 1 2 2 2 ...
##  $ StockOptionLevel        : Factor w/ 4 levels &quot;0&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;: 1 2 1 1 2 1 4 2 1 3 ...
##  $ TotalWorkingYears       : int  8 10 7 8 6 8 12 1 10 17 ...
##  $ TrainingTimesLastYear   : Factor w/ 7 levels &quot;0&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,..: 1 4 4 4 4 3 4 3 3 4 ...
##  $ WorkLifeBalance         : Ord.factor w/ 4 levels &quot;Bad&quot;&lt;&quot;Good&quot;&lt;&quot;Better&quot;&lt;..: 1 3 3 3 3 2 2 3 3 2 ...
##  $ YearsAtCompany          : int  6 10 0 8 2 7 1 1 9 7 ...
##  $ YearsInCurrentRole      : int  4 7 0 7 2 7 0 0 7 7 ...
##  $ YearsSinceLastPromotion : int  0 1 0 3 2 3 0 0 1 7 ...
##  $ YearsWithCurrManager    : int  5 7 0 0 2 6 0 0 8 7 ...
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>attrition</span><span> </span><span>&lt;-</span><span> </span><span>attrition</span><span> </span><span>%>%</span><span> </span><span>mutate_if</span><span>(</span><span>function</span><span>(</span><span>col</span><span>)</span><span> </span><span>length</span><span>(</span><span>unique</span><span>(</span><span>col</span><span>))</span><span> </span><span>&lt;=</span><span> </span><span>10</span><span> </span><span>&amp;</span><span> </span><span>is.integer</span><span>(</span><span>col</span><span>),</span><span> </span><span>as.factor</span><span>)</span><span>
</span><span>summary</span><span>(</span><span>attrition</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##       Age        Attrition            BusinessTravel   DailyRate     
##  Min.   :18.00   No :1233   Non-Travel       : 150   Min.   : 102.0  
##  1st Qu.:30.00   Yes: 237   Travel_Frequently: 277   1st Qu.: 465.0  
##  Median :36.00              Travel_Rarely    :1043   Median : 802.0  
##  Mean   :36.92                                       Mean   : 802.5  
##  3rd Qu.:43.00                                       3rd Qu.:1157.0  
##  Max.   :60.00                                       Max.   :1499.0  
##                                                                      
##                 Department  DistanceFromHome         Education  
##  Human_Resources     : 63   Min.   : 1.000   Below_College:170  
##  Research_Development:961   1st Qu.: 2.000   College      :282  
##  Sales               :446   Median : 7.000   Bachelor     :572  
##                             Mean   : 9.193   Master       :398  
##                             3rd Qu.:14.000   Doctor       : 48  
##                             Max.   :29.000                      
##                                                                 
##           EducationField EnvironmentSatisfaction    Gender   
##  Human_Resources : 27    Low      :284           Female:588  
##  Life_Sciences   :606    Medium   :287           Male  :882  
##  Marketing       :159    High     :453                       
##  Medical         :464    Very_High:446                       
##  Other           : 82                                        
##  Technical_Degree:132                                        
##                                                              
##    HourlyRate       JobInvolvement JobLevel
##  Min.   : 30.00   Low      : 83    1:543   
##  1st Qu.: 48.00   Medium   :375    2:534   
##  Median : 66.00   High     :868    3:218   
##  Mean   : 65.89   Very_High:144    4:106   
##  3rd Qu.: 83.75                    5: 69   
##  Max.   :100.00                            
##                                            
##                       JobRole     JobSatisfaction  MaritalStatus
##  Sales_Executive          :326   Low      :289    Divorced:327  
##  Research_Scientist       :292   Medium   :280    Married :673  
##  Laboratory_Technician    :259   High     :442    Single  :470  
##  Manufacturing_Director   :145   Very_High:459                  
##  Healthcare_Representative:131                                  
##  Manager                  :102                                  
##  (Other)                  :215                                  
##  MonthlyIncome    MonthlyRate    NumCompaniesWorked OverTime  
##  Min.   : 1009   Min.   : 2094   1      :521        No :1054  
##  1st Qu.: 2911   1st Qu.: 8047   0      :197        Yes: 416  
##  Median : 4919   Median :14236   3      :159                  
##  Mean   : 6503   Mean   :14313   2      :146                  
##  3rd Qu.: 8379   3rd Qu.:20462   4      :139                  
##  Max.   :19999   Max.   :26999   7      : 74                  
##                                  (Other):234                  
##  PercentSalaryHike   PerformanceRating RelationshipSatisfaction
##  Min.   :11.00     Low        :   0    Low      :276           
##  1st Qu.:12.00     Good       :   0    Medium   :303           
##  Median :14.00     Excellent  :1244    High     :459           
##  Mean   :15.21     Outstanding: 226    Very_High:432           
##  3rd Qu.:18.00                                                 
##  Max.   :25.00                                                 
##                                                                
##  StockOptionLevel TotalWorkingYears TrainingTimesLastYear WorkLifeBalance
##  0:631            Min.   : 0.00     0: 54                 Bad   : 80     
##  1:596            1st Qu.: 6.00     1: 71                 Good  :344     
##  2:158            Median :10.00     2:547                 Better:893     
##  3: 85            Mean   :11.28     3:491                 Best  :153     
##                   3rd Qu.:15.00     4:123                                
##                   Max.   :40.00     5:119                                
##                                     6: 65                                
##  YearsAtCompany   YearsInCurrentRole YearsSinceLastPromotion
##  Min.   : 0.000   Min.   : 0.000     Min.   : 0.000         
##  1st Qu.: 3.000   1st Qu.: 2.000     1st Qu.: 0.000         
##  Median : 5.000   Median : 3.000     Median : 1.000         
##  Mean   : 7.008   Mean   : 4.229     Mean   : 2.188         
##  3rd Qu.: 9.000   3rd Qu.: 7.000     3rd Qu.: 3.000         
##  Max.   :40.000   Max.   :18.000     Max.   :15.000         
##                                                             
##  YearsWithCurrManager
##  Min.   : 0.000      
##  1st Qu.: 2.000      
##  Median : 3.000      
##  Mean   : 4.123      
##  3rd Qu.: 7.000      
##  Max.   :17.000      
## 
</code></pre>
</div>
</div>
<p>As you look at the results this is a good time to remind you that<br>
<code>CHAID</code> is “non parametric” which means that we don’t have to worry<br>
about how the distribution (normality) looks nor make any assumptions<br>
about the variance. We are assuming that the predictors are independent<br>
of one another, but that is true of every statistical test and this is a<br>
robust procedure. So for now, let’s simply ignore all the variables that<br>
are still integers. I promise we’ll come back and deal with them later.<br>
But for now I’m eager to actually use CHAID and do some predicting.<br>
We’re also going to defer and address the issue of “over-fitting” and<br>
how to most wisely use the data we have. We’re simply going to build a<br>
first model using all 1,470 cases, the 18 factors we have available to<br>
predict with and we are trying to predict attrition. We’ll create a new<br>
dataframe called <code>newattrit</code> (how original right?).</p>
<div>
<div>
<pre><code><span>newattrit</span><span> </span><span>&lt;-</span><span> </span><span>attrition</span><span> </span><span>%>%</span><span> </span><span>select_if</span><span>(</span><span>is.factor</span><span>)</span><span>
</span><span>dim</span><span>(</span><span>newattrit</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 1470   19
</code></pre>
</div>
</div>
<p>The <code>chaid</code> command accepts two pieces of information in it’s simplest<br>
case, a formula like <code>outcome ~ predictors</code> and a dataframe. We’re going<br>
to make use of the <code>~ .</code> shortcut on the right hand side and add<br>
<code>attrition</code> on the left and <code>newattrit</code> as our dataframe.</p>
<p>About 6 seconds later (at least on my Mac) we’ll have a solution that we<br>
can <code>print</code> and <code>plot</code>.</p>
<blockquote>
<p><strong>I’m going to output all the plots in a smaller size for the benefit<br>
of you the readers. I’m doing that via RMarkdown and it won’t happen<br>
automatically for you if you download and use the code. I’ll initially<br>
be using, fig.height=10, fig.width=20, dpi=90, out.width=“900px”</strong></p>
</blockquote>
<p>What does CHAID do? Straight from the help pages “Select the predictor<br>
that has the smallest adjusted p-value (i.e., most significant). If this<br>
adjusted p-value is less than or equal to a user-specified alpha-level<br>
alpha4, split the node using this predictor. Else, do not split and the<br>
node is considered as a terminal node.” So it will take our 18<br>
predictors and test each one against our outcome variable – attrition.<br>
The one with the lowest p value (a proxy for is most predictive) will<br>
“anchor” our decision tree. It will then repeat this process of<br>
splitting until more splits fail to yield <em>significant</em> results. I’m way<br>
over-simplifying, of course, but you get the idea. The end result will<br>
be a series of <code>terminal nodes</code> (think of them as “prediction buckets”<br>
that have a group of employees who all meet the same criteria who we<br>
think will either attrit or not attrit). Let’s run it.</p>
<div>
<div>
<pre><code><span># demonstrate a full model using chaid with defaults</span><span>
</span><span>chaidattrit1</span><span> </span><span>&lt;-</span><span> </span><span>chaid</span><span>(</span><span>Attrition</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>newattrit</span><span>)</span><span>
</span><span>print</span><span>(</span><span>chaidattrit1</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
## Model formula:
## Attrition ~ BusinessTravel + Department + Education + EducationField + 
##     EnvironmentSatisfaction + Gender + JobInvolvement + JobLevel + 
##     JobRole + JobSatisfaction + MaritalStatus + NumCompaniesWorked + 
##     OverTime + PerformanceRating + RelationshipSatisfaction + 
##     StockOptionLevel + TrainingTimesLastYear + WorkLifeBalance
## 
## Fitted party:
## [1] root
## |   [2] OverTime in No
## |   |   [3] StockOptionLevel in 0
## |   |   |   [4] JobSatisfaction in Low
## |   |   |   |   [5] RelationshipSatisfaction in Low, Medium, High: No (n = 56, err = 42.9%)
## |   |   |   |   [6] RelationshipSatisfaction in Very_High: No (n = 28, err = 7.1%)
## |   |   |   [7] JobSatisfaction in Medium, High
## |   |   |   |   [8] JobInvolvement in Low: Yes (n = 12, err = 41.7%)
## |   |   |   |   [9] JobInvolvement in Medium, High, Very_High
## |   |   |   |   |   [10] BusinessTravel in Non-Travel, Travel_Rarely: No (n = 181, err = 9.9%)
## |   |   |   |   |   [11] BusinessTravel in Travel_Frequently
## |   |   |   |   |   |   [12] RelationshipSatisfaction in Low: Yes (n = 8, err = 25.0%)
## |   |   |   |   |   |   [13] RelationshipSatisfaction in Medium, High, Very_High: No (n = 30, err = 16.7%)
## |   |   |   [14] JobSatisfaction in Very_High: No (n = 134, err = 7.5%)
## |   |   [15] StockOptionLevel in 1, 2, 3
## |   |   |   [16] EnvironmentSatisfaction in Low: No (n = 127, err = 11.0%)
## |   |   |   [17] EnvironmentSatisfaction in Medium, High, Very_High
## |   |   |   |   [18] Department in Human_Resources, Sales: No (n = 164, err = 8.5%)
## |   |   |   |   [19] Department in Research_Development: No (n = 314, err = 3.2%)
## |   [20] OverTime in Yes
## |   |   [21] JobLevel in 1
## |   |   |   [22] StockOptionLevel in 0, 3
## |   |   |   |   [23] JobSatisfaction in Low, Medium, High: Yes (n = 61, err = 26.2%)
## |   |   |   |   [24] JobSatisfaction in Very_High: No (n = 28, err = 46.4%)
## |   |   |   [25] StockOptionLevel in 1, 2
## |   |   |   |   [26] BusinessTravel in Non-Travel, Travel_Rarely: No (n = 50, err = 26.0%)
## |   |   |   |   [27] BusinessTravel in Travel_Frequently: Yes (n = 17, err = 35.3%)
## |   |   [28] JobLevel in 2, 3, 4, 5
## |   |   |   [29] MaritalStatus in Divorced, Married
## |   |   |   |   [30] EnvironmentSatisfaction in Low, Medium: No (n = 60, err = 20.0%)
## |   |   |   |   [31] EnvironmentSatisfaction in High, Very_High
## |   |   |   |   |   [32] TrainingTimesLastYear in 0, 6: No (n = 10, err = 40.0%)
## |   |   |   |   |   [33] TrainingTimesLastYear in 1, 2, 3, 4, 5
## |   |   |   |   |   |   [34] EnvironmentSatisfaction in Low, Medium, High: No (n = 57, err = 0.0%)
## |   |   |   |   |   |   [35] EnvironmentSatisfaction in Very_High: No (n = 61, err = 6.6%)
## |   |   |   [36] MaritalStatus in Single
## |   |   |   |   [37] Department in Human_Resources, Research_Development: No (n = 37, err = 10.8%)
## |   |   |   |   [38] Department in Sales: Yes (n = 35, err = 40.0%)
## 
## Number of inner nodes:    18
## Number of terminal nodes: 20
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>chaidattrit1</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i0.wp.com/ibecav.github.io/images/best6-1.png?w=456" data-recalc-dims="1"></p>
<div>
<div>
<pre><code><span>chisq.test</span><span>(</span><span>newattrit</span><span>$</span><span>Attrition</span><span>,</span><span> </span><span>newattrit</span><span>$</span><span>OverTime</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  newattrit$Attrition and newattrit$OverTime
## X-squared = 87.564, df = 1, p-value &lt; 2.2e-16
</code></pre>
</div>
</div>
<p>I happen to be a visual learner and prefer the <code>plot</code> to the <code>print</code> but<br>
they are obviously reporting the same information so use them as you see<br>
fit. As you can see the very first split it decides on is overtime yes<br>
or no. I’ve run the chi-square test so that you can see the <code>p value</code> is<br>
indeed very small (0.00000000000000022).</p>
<p>So the algorithm has decided that the most predictive way to divide our<br>
sample of employees is into 20 terminal nodes or buckets. Each one of<br>
the nodes represents a distinct set of predictors. Take a minute to look<br>
at node 19. Every person there shares the following characteristics.</p>
<ul>
<li>[2] OverTime in No</li>
<li>[15] StockOptionLevel in 1, 2, 3</li>
<li>[17] EnvironmentSatisfaction in Medium, High, Very_High</li>
<li>[19] Department in Research_Development: No</li>
</ul>
<p>There are n = 314 in this group, our prediction is that <code>No</code> they will<br>
not attrit and we were “wrong” err = 3.2%. That’s some useful<br>
information. To quote an old Star Wars movie “These are not the droids<br>
you’re looking for…”. In other words, this is not a group we should be<br>
overly worried about losing and we can say that with pretty high<br>
confidence.</p>
<p>For contrast let’s look at node #23:</p>
<ul>
<li>[20] OverTime in Yes</li>
<li>[21] JobLevel in 1</li>
<li>[22] StockOptionLevel in 0, 3</li>
<li>[23] JobSatisfaction in Low, Medium, High:</li>
</ul>
<p>Where there are n = 61 staff, we predict they will leave <code>Yes</code> and we<br>
get it wrong err = 26.2% of the time. A little worrisome that we’re not<br>
as accurate but this is a group that bears watching or intervention if<br>
we want to retain them.</p>
<p>Some other things to note. Because the predictors are considered<br>
categorical we will get splits like we do for node 22, where 0 and 3 are<br>
on one side and 1, 2 is on the other. The number of people in any node<br>
can be quite variable. Finally, notice that a variable can occur at<br>
different levels of the model like <code>StockOptionLevel</code> does!</p>
<p>On the <code>plot</code> side of things there are a few key options you can adjust<br>
to make things easier to read. The next blocks of code show you how to<br>
adjust some key options such as adding a title, reducing the font size,<br>
using “simple” mode, and changing colors.</p>
<div>
<div>
<pre><code><span># digress for plotting</span><span>
</span><span>plot</span><span>(</span><span>chaidattrit1</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;simple&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i1.wp.com/ibecav.github.io/images/best7-1.png?w=456" data-recalc-dims="1"></p>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>
</span><span>chaidattrit1</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Testing Graphical Options&quot;</span><span>,</span><span>
</span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>fontsize</span><span> </span><span>=</span><span> </span><span>8</span><span>),</span><span>
</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;simple&quot;</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i1.wp.com/ibecav.github.io/images/best7-2.png?w=456" data-recalc-dims="1"></p>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>
</span><span>chaidattrit1</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Testing More Graphical Options&quot;</span><span>,</span><span>
</span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span>
</span><span>lty</span><span> </span><span>=</span><span> </span><span>&quot;solid&quot;</span><span>,</span><span>
</span><span>lwd</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span>
</span><span>fontsize</span><span> </span><span>=</span><span> </span><span>10</span><span>
</span><span>)</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i2.wp.com/ibecav.github.io/images/best7-3.png?w=456" data-recalc-dims="1"></p>
<h2>Exercising some control</h2>
<p>Next let’s look into varying the parameters <code>chaid</code> uses to build the<br>
model. <code>chaid_control</code> (not surprisingly) controls the behavior of the<br>
model building. When you check the documentation at <code>?chaid_control</code> you<br>
can see the list of 8 parameters you can adjust. We’ve already run the<br>
default settings implicitly when we built <code>chaidattrit1</code> let’s look at<br>
three others.</p>
<ul>
<li><code>minsplit</code> – Number of observations in splitted response at which no<br>
further split is desired.</li>
<li><code>minprob</code> – Minimum frequency of observations in terminal nodes.</li>
<li><code>maxheight</code> – Maximum height for the tree.</li>
</ul>
<p>We’ll use those but our fourth model we’ll simply require a higher<br>
significance level for alpha2 and alpha4.</p>
<div>
<div>
<pre><code><span>ctrl</span><span> </span><span>&lt;-</span><span> </span><span>chaid_control</span><span>(</span><span>minsplit</span><span> </span><span>=</span><span> </span><span>200</span><span>,</span><span> </span><span>minprob</span><span> </span><span>=</span><span> </span><span>0.05</span><span>)</span><span>
</span><span>ctrl</span><span> </span><span># notice the rest of the list is there at the default value</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## $alpha2
## [1] 0.05
## 
## $alpha3
## [1] -1
## 
## $alpha4
## [1] 0.05
## 
## $minsplit
## [1] 200
## 
## $minbucket
## [1] 7
## 
## $minprob
## [1] 0.05
## 
## $stump
## [1] FALSE
## 
## $maxheight
## [1] -1
## 
## attr(,&quot;class&quot;)
## [1] &quot;chaid_control&quot;
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>chaidattrit2</span><span> </span><span>&lt;-</span><span> </span><span>chaid</span><span>(</span><span>Attrition</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>newattrit</span><span>,</span><span> </span><span>control</span><span> </span><span>=</span><span> </span><span>ctrl</span><span>)</span><span>
</span><span>print</span><span>(</span><span>chaidattrit2</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
## Model formula:
## Attrition ~ BusinessTravel + Department + Education + EducationField + 
##     EnvironmentSatisfaction + Gender + JobInvolvement + JobLevel + 
##     JobRole + JobSatisfaction + MaritalStatus + NumCompaniesWorked + 
##     OverTime + PerformanceRating + RelationshipSatisfaction + 
##     StockOptionLevel + TrainingTimesLastYear + WorkLifeBalance
## 
## Fitted party:
## [1] root
## |   [2] OverTime in No
## |   |   [3] StockOptionLevel in 0
## |   |   |   [4] JobSatisfaction in Low: No (n = 84, err = 31.0%)
## |   |   |   [5] JobSatisfaction in Medium, High
## |   |   |   |   [6] JobInvolvement in Low: Yes (n = 12, err = 41.7%)
## |   |   |   |   [7] JobInvolvement in Medium, High, Very_High
## |   |   |   |   |   [8] BusinessTravel in Non-Travel, Travel_Rarely: No (n = 181, err = 9.9%)
## |   |   |   |   |   [9] BusinessTravel in Travel_Frequently: No (n = 38, err = 28.9%)
## |   |   |   [10] JobSatisfaction in Very_High: No (n = 134, err = 7.5%)
## |   |   [11] StockOptionLevel in 1, 2, 3
## |   |   |   [12] EnvironmentSatisfaction in Low: No (n = 127, err = 11.0%)
## |   |   |   [13] EnvironmentSatisfaction in Medium, High, Very_High
## |   |   |   |   [14] Department in Human_Resources, Sales: No (n = 164, err = 8.5%)
## |   |   |   |   [15] Department in Research_Development: No (n = 314, err = 3.2%)
## |   [16] OverTime in Yes
## |   |   [17] JobLevel in 1: Yes (n = 156, err = 47.4%)
## |   |   [18] JobLevel in 2, 3, 4, 5
## |   |   |   [19] MaritalStatus in Divorced, Married: No (n = 188, err = 10.6%)
## |   |   |   [20] MaritalStatus in Single: No (n = 72, err = 34.7%)
## 
## Number of inner nodes:     9
## Number of terminal nodes: 11
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>
</span><span>chaidattrit2</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;minsplit = 200, minprob = 0.05&quot;</span><span>,</span><span>
</span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span>
</span><span>lty</span><span> </span><span>=</span><span> </span><span>&quot;solid&quot;</span><span>,</span><span>
</span><span>lwd</span><span> </span><span>=</span><span> </span><span>3</span><span> </span><span>)</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i0.wp.com/ibecav.github.io/images/best8-1.png?w=456" data-recalc-dims="1"></p>
<div>
<div>
<pre><code><span>ctrl</span><span> </span><span>&lt;-</span><span> </span><span>chaid_control</span><span>(</span><span>maxheight</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span>
</span><span>chaidattrit3</span><span> </span><span>&lt;-</span><span> </span><span>chaid</span><span>(</span><span>Attrition</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>newattrit</span><span>,</span><span> </span><span>control</span><span> </span><span>=</span><span> </span><span>ctrl</span><span>)</span><span>
</span><span>print</span><span>(</span><span>chaidattrit3</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
## Model formula:
## Attrition ~ BusinessTravel + Department + Education + EducationField + 
##     EnvironmentSatisfaction + Gender + JobInvolvement + JobLevel + 
##     JobRole + JobSatisfaction + MaritalStatus + NumCompaniesWorked + 
##     OverTime + PerformanceRating + RelationshipSatisfaction + 
##     StockOptionLevel + TrainingTimesLastYear + WorkLifeBalance
## 
## Fitted party:
## [1] root
## |   [2] OverTime in No
## |   |   [3] StockOptionLevel in 0
## |   |   |   [4] JobSatisfaction in Low: No (n = 84, err = 31.0%)
## |   |   |   [5] JobSatisfaction in Medium, High: No (n = 231, err = 15.6%)
## |   |   |   [6] JobSatisfaction in Very_High: No (n = 134, err = 7.5%)
## |   |   [7] StockOptionLevel in 1, 2, 3
## |   |   |   [8] EnvironmentSatisfaction in Low: No (n = 127, err = 11.0%)
## |   |   |   [9] EnvironmentSatisfaction in Medium, High, Very_High: No (n = 478, err = 5.0%)
## |   [10] OverTime in Yes
## |   |   [11] JobLevel in 1
## |   |   |   [12] StockOptionLevel in 0, 3: Yes (n = 89, err = 34.8%)
## |   |   |   [13] StockOptionLevel in 1, 2: No (n = 67, err = 35.8%)
## |   |   [14] JobLevel in 2, 3, 4, 5
## |   |   |   [15] MaritalStatus in Divorced, Married: No (n = 188, err = 10.6%)
## |   |   |   [16] MaritalStatus in Single: No (n = 72, err = 34.7%)
## 
## Number of inner nodes:    7
## Number of terminal nodes: 9
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>
</span><span>chaidattrit3</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;maxheight = 3&quot;</span><span>,</span><span>
</span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span>
</span><span>lty</span><span> </span><span>=</span><span> </span><span>&quot;solid&quot;</span><span>,</span><span>
</span><span>lwd</span><span> </span><span>=</span><span> </span><span>3</span><span> </span><span>)</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i1.wp.com/ibecav.github.io/images/best8-2.png?w=456" data-recalc-dims="1"></p>
<div>
<div>
<pre><code><span>ctrl</span><span> </span><span>&lt;-</span><span> </span><span>chaid_control</span><span>(</span><span>alpha2</span><span> </span><span>=</span><span> </span><span>.01</span><span>,</span><span> </span><span>alpha4</span><span> </span><span>=</span><span> </span><span>.01</span><span>)</span><span>
</span><span>chaidattrit4</span><span> </span><span>&lt;-</span><span> </span><span>chaid</span><span>(</span><span>Attrition</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>newattrit</span><span>,</span><span> </span><span>control</span><span> </span><span>=</span><span> </span><span>ctrl</span><span>)</span><span>
</span><span>print</span><span>(</span><span>chaidattrit4</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
## Model formula:
## Attrition ~ BusinessTravel + Department + Education + EducationField + 
##     EnvironmentSatisfaction + Gender + JobInvolvement + JobLevel + 
##     JobRole + JobSatisfaction + MaritalStatus + NumCompaniesWorked + 
##     OverTime + PerformanceRating + RelationshipSatisfaction + 
##     StockOptionLevel + TrainingTimesLastYear + WorkLifeBalance
## 
## Fitted party:
## [1] root
## |   [2] OverTime in No
## |   |   [3] StockOptionLevel in 0
## |   |   |   [4] JobSatisfaction in Low
## |   |   |   |   [5] RelationshipSatisfaction in Low, Medium, High: No (n = 56, err = 42.9%)
## |   |   |   |   [6] RelationshipSatisfaction in Very_High: No (n = 28, err = 7.1%)
## |   |   |   [7] JobSatisfaction in Medium, High, Very_High
## |   |   |   |   [8] JobInvolvement in Low: No (n = 20, err = 45.0%)
## |   |   |   |   [9] JobInvolvement in Medium, High, Very_High
## |   |   |   |   |   [10] JobLevel in 1: No (n = 139, err = 18.0%)
## |   |   |   |   |   [11] JobLevel in 2, 3, 4, 5: No (n = 206, err = 5.8%)
## |   |   [12] StockOptionLevel in 1, 2, 3: No (n = 605, err = 6.3%)
## |   [13] OverTime in Yes
## |   |   [14] JobLevel in 1
## |   |   |   [15] StockOptionLevel in 0, 3: Yes (n = 89, err = 34.8%)
## |   |   |   [16] StockOptionLevel in 1, 2: No (n = 67, err = 35.8%)
## |   |   [17] JobLevel in 2, 3, 4, 5
## |   |   |   [18] MaritalStatus in Divorced, Married: No (n = 188, err = 10.6%)
## |   |   |   [19] MaritalStatus in Single
## |   |   |   |   [20] Department in Human_Resources, Research_Development: No (n = 37, err = 10.8%)
## |   |   |   |   [21] Department in Sales: Yes (n = 35, err = 40.0%)
## 
## Number of inner nodes:    10
## Number of terminal nodes: 11
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>
</span><span>chaidattrit4</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;alpha2 = .01, alpha4 = .01&quot;</span><span>,</span><span>
</span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span>
</span><span>lty</span><span> </span><span>=</span><span> </span><span>&quot;solid&quot;</span><span>,</span><span>
</span><span>lwd</span><span> </span><span>=</span><span> </span><span>3</span><span> </span><span>)</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i2.wp.com/ibecav.github.io/images/best8-3.png?w=456" data-recalc-dims="1"></p>
<p>Let me call your attention to <code>chaidattrit3</code> for a minute to highlight<br>
two important things. First it is a good picture of what we get for<br>
answer if we were to ask a question about what are the most important<br>
predictors, what variables should we focus on. An important technical<br>
detail has emerged as well. Notice that when you look at inner node #3<br>
that there is no technical reason why a node has to have a <em>binary</em><br>
split in chaid. As this example clearly shows node#3 leads to a three<br>
way split that is nodes #4-6.</p>
<h2>How good is our model?</h2>
<p>So the obvious question is which model is best? IMHO the joy of CHAID is<br>
in giving you a clear picture of what you would predict given the data<br>
and why. Then of course there is the usual problem every data scientist<br>
has, which is, I have what I think is a great model. How well will it<br>
generalize to new data? Whether that’s next years attrition numbers for<br>
the same company or say data from a different company.</p>
<p>But it’s time to talk about accuracy and all the related ideas, so on<br>
with the show…</p>
<p>When it’s all said and done we built a model called <code>chaidattrit1</code> to be<br>
able to predict or classify the 1,470 staff members. Seems reasonable<br>
then that we can get back these predictions from the model for all 1,470<br>
people and see how we did compared to the data we have about whether<br>
they attrited or not. The print and plot commands sort of summarize that<br>
for us at the terminal node level with an error rate but all in all<br>
which of our four models is best?</p>
<p>The first step is to get the predictions for each model and put them<br>
somewhere. For that we’ll use the <code>predict</code> command. If you inspect the<br>
object you create (in my case with a head command) you’ll see it’s a<br>
vector of factors where the attribute names is set to be the terminal<br>
node the prediction is associated with. So <code>pmodel1 &lt;-<br>
predict(chaidattrit1)</code> puts our predictions using the first model we<br>
built in a nice orderly fashion. On the other side <code>newattrit$Attrition</code><br>
has the actual outcome of whether the employee departed or not.</p>
<p>What we want is a comparison of how well we did. How often did we get it<br>
right or wrong? Turns out what we need is called a confusion matrix. The<br>
<code>caret</code> package has a function called <code>confusionMatrix</code> that will give<br>
us what we want nicely formatted and printed.</p>
<p>There’s a nice short summary of what is produced at this url <a rel="nofollow" href="http://www.dataschool.io/simple-guide-to-confusion-matrix-terminology/" target="_blank">Confusion<br>
Matrix</a>,<br>
so I won’t even try to repeat that material. I’ll just run the<br>
appropriate commands. Later we’ll revisit this topic to be more<br>
efficient. For now I want to focus on the results.</p>
<div>
<div>
<pre><code><span># digress how accurate were we</span><span>
</span><span>pmodel1</span><span> </span><span>&lt;-</span><span> </span><span>predict</span><span>(</span><span>chaidattrit1</span><span>)</span><span>
</span><span>head</span><span>(</span><span>pmodel1</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##  38  19  23  23  16  14 
## Yes  No Yes Yes  No  No 
## Levels: No Yes
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>pmodel2</span><span> </span><span>&lt;-</span><span> </span><span>predict</span><span>(</span><span>chaidattrit2</span><span>)</span><span>
</span><span>pmodel3</span><span> </span><span>&lt;-</span><span> </span><span>predict</span><span>(</span><span>chaidattrit3</span><span>)</span><span>
</span><span>pmodel4</span><span> </span><span>&lt;-</span><span> </span><span>predict</span><span>(</span><span>chaidattrit4</span><span>)</span><span>
</span><span>confusionMatrix</span><span>(</span><span>pmodel1</span><span>,</span><span> </span><span>newattrit</span><span>$</span><span>Attrition</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   No  Yes
##        No  1190  147
##        Yes   43   90
##                                           
##                Accuracy : 0.8707          
##                  95% CI : (0.8525, 0.8875)
##     No Information Rate : 0.8388          
##     P-Value [Acc > NIR] : 0.0003553       
##                                           
##                   Kappa : 0.4192          
##  Mcnemar's Test P-Value : 7.874e-14       
##                                           
##             Sensitivity : 0.9651          
##             Specificity : 0.3797          
##          Pos Pred Value : 0.8901          
##          Neg Pred Value : 0.6767          
##              Prevalence : 0.8388          
##          Detection Rate : 0.8095          
##    Detection Prevalence : 0.9095          
##       Balanced Accuracy : 0.6724          
##                                           
##        'Positive' Class : No              
## 
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>confusionMatrix</span><span>(</span><span>pmodel2</span><span>,</span><span> </span><span>newattrit</span><span>$</span><span>Attrition</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   No  Yes
##        No  1154  148
##        Yes   79   89
##                                           
##                Accuracy : 0.8456          
##                  95% CI : (0.8261, 0.8637)
##     No Information Rate : 0.8388          
##     P-Value [Acc > NIR] : 0.2516          
##                                           
##                   Kappa : 0.353           
##  Mcnemar's Test P-Value : 6.382e-06       
##                                           
##             Sensitivity : 0.9359          
##             Specificity : 0.3755          
##          Pos Pred Value : 0.8863          
##          Neg Pred Value : 0.5298          
##              Prevalence : 0.8388          
##          Detection Rate : 0.7850          
##    Detection Prevalence : 0.8857          
##       Balanced Accuracy : 0.6557          
##                                           
##        'Positive' Class : No              
## 
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>confusionMatrix</span><span>(</span><span>pmodel3</span><span>,</span><span> </span><span>newattrit</span><span>$</span><span>Attrition</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   No  Yes
##        No  1202  179
##        Yes   31   58
##                                           
##                Accuracy : 0.8571          
##                  95% CI : (0.8382, 0.8746)
##     No Information Rate : 0.8388          
##     P-Value [Acc > NIR] : 0.02864         
##                                           
##                   Kappa : 0.2936          
##  Mcnemar's Test P-Value : &lt; 2e-16         
##                                           
##             Sensitivity : 0.9749          
##             Specificity : 0.2447          
##          Pos Pred Value : 0.8704          
##          Neg Pred Value : 0.6517          
##              Prevalence : 0.8388          
##          Detection Rate : 0.8177          
##    Detection Prevalence : 0.9395          
##       Balanced Accuracy : 0.6098          
##                                           
##        'Positive' Class : No              
## 
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>confusionMatrix</span><span>(</span><span>pmodel4</span><span>,</span><span> </span><span>newattrit</span><span>$</span><span>Attrition</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   No  Yes
##        No  1188  158
##        Yes   45   79
##                                           
##                Accuracy : 0.8619          
##                  95% CI : (0.8432, 0.8791)
##     No Information Rate : 0.8388          
##     P-Value [Acc > NIR] : 0.007845        
##                                           
##                   Kappa : 0.3676          
##  Mcnemar's Test P-Value : 3.815e-15       
##                                           
##             Sensitivity : 0.9635          
##             Specificity : 0.3333          
##          Pos Pred Value : 0.8826          
##          Neg Pred Value : 0.6371          
##              Prevalence : 0.8388          
##          Detection Rate : 0.8082          
##    Detection Prevalence : 0.9156          
##       Balanced Accuracy : 0.6484          
##                                           
##        'Positive' Class : No              
## 
</code></pre>
</div>
</div>
<p>There we have it, four matrices, one for each of the models we made with<br>
the different control parameters. It helpfully provides not just<br>
Accuracy but also other common measures you may be interested in. I<br>
won’t review them all that’s why I provided <a rel="nofollow" href="http://www.dataschool.io/simple-guide-to-confusion-matrix-terminology/" target="_blank">the link to a detailed<br>
description</a><br>
of all the measures. Before we leave the topic for a bit however, I do<br>
want to highlight a way you can use the <code>purrr</code> package to make your<br>
life a lot easier. A special thanks to <a rel="nofollow" href="https://seslezak.github.io/" target="_blank">Steven at<br>
MungeX-3D</a> for his recent post on <code>purrr</code><br>
which got me thinking about it.</p>
<p>We have 4 models so far (with more to come) we have the nice neat output<br>
from <code>caret</code> but honestly to compare values across the 4 models involves<br>
way too much scrolling back and forth right now. Let’s use <code>purrr</code> to<br>
create a nice neat dataframe. <code>purrr</code>’s <code>map</code> command is like <code>lapply</code><br>
from base R, designed to apply some operations or functions to a list of<br>
objects. So what we’ll do is as follows:</p>
<ol>
<li>Create a named list called <code>modellist</code> to point to our four existing<br>
models (perhaps at a latter date we’ll start even earlier in our<br>
modelling process).</li>
<li>It’s a named list so we can name each model (for now with the<br>
accurate but uninteresting name Modelx)</li>
<li>Pass the list using <code>map</code> to the <code>predict</code> function to generate our<br>
predictions</li>
<li>Pipe <code>%>%</code> those results to the <code>confusionMatrix</code> function with<br>
<code>map</code></li>
<li>Pipe <code>%>%</code> the confusion matrix results to map_dfr. The results of<br>
confusionMattrix are actually a list of six items. The ones we want<br>
to capture are in <code>$overall</code> and <code>$byClass</code>. We grab them, transpose<br>
them, and make them into a dataframe then bind the two dataframes<br>
together so everything is neatly packaged. The <code>.id = ModelNumb</code><br>
tells <code>map_dfr</code> to add an identifying column to the dataframe. It is<br>
populated with the name of the list item we passed in <code>modellist</code>.<br>
Therefore the object CHAIDresults contains everything we might want<br>
to use to compare models in one neat dataframe.</li>
</ol>
<p>The <code>kable</code> call is simply for your reading convenience. Makes it a<br>
little easier to read than a traditional print call.</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>kableExtra</span><span>)</span><span>
</span><span>modellist</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>Model1</span><span> </span><span>=</span><span> </span><span>chaidattrit1</span><span>,</span><span> </span><span>Model2</span><span> </span><span>=</span><span> </span><span>chaidattrit2</span><span>,</span><span> </span><span>Model3</span><span> </span><span>=</span><span> </span><span>chaidattrit3</span><span>,</span><span> </span><span>Model4</span><span> </span><span>=</span><span> </span><span>chaidattrit4</span><span>)</span><span>
</span><span>CHAIDResults</span><span> </span><span>&lt;-</span><span> </span><span>map</span><span>(</span><span>modellist</span><span>,</span><span> </span><span>~</span><span> </span><span>predict</span><span>(</span><span>.x</span><span>))</span><span> </span><span>%>%</span><span> </span><span>map</span><span>(</span><span>~</span><span> </span><span>confusionMatrix</span><span>(</span><span>newattrit</span><span>$</span><span>Attrition</span><span>,</span><span> </span><span>.x</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>map_dfr</span><span>(</span><span>~</span><span> </span><span>cbind</span><span>(</span><span>as.data.frame</span><span>(</span><span>t</span><span>(</span><span>.x</span><span>$</span><span>overall</span><span>)),</span><span>as.data.frame</span><span>(</span><span>t</span><span>(</span><span>.x</span><span>$</span><span>byClass</span><span>))),</span><span> </span><span>.id</span><span> </span><span>=</span><span> </span><span>&quot;ModelNumb&quot;</span><span>)</span><span>
</span><span>kable</span><span>(</span><span>CHAIDResults</span><span>,</span><span> </span><span>&quot;html&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>kable_styling</span><span>(</span><span>bootstrap_options</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;striped&quot;</span><span>,</span><span> </span><span>&quot;hover&quot;</span><span>,</span><span> </span><span>&quot;condensed&quot;</span><span>,</span><span> </span><span>&quot;responsive&quot;</span><span>),</span><span> </span><span>font_size</span><span> </span><span>=</span><span> </span><span>9</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<table>
<thead>
<tr>
<th>
<p>ModelNumb</p>
</th>
<th>
<p>Accuracy</p>
</th>
<th>
<p>Kappa</p>
</th>
<th>
<p>AccuracyLower</p>
</th>
<th>
<p>AccuracyUpper</p>
</th>
<th>
<p>AccuracyNull</p>
</th>
<th>
<p>AccuracyPValue</p>
</th>
<th>
<p>McnemarPValue</p>
</th>
<th>
<p>Sensitivity</p>
</th>
<th>
<p>Specificity</p>
</th>
<th>
<p>Pos Pred Value</p>
</th>
<th>
<p>Neg Pred Value</p>
</th>
<th>
<p>Precision</p>
</th>
<th>
<p>Recall</p>
</th>
<th>
<p>F1</p>
</th>
<th>
<p>Prevalence</p>
</th>
<th>
<p>Detection Rate</p>
</th>
<th>
<p>Detection Prevalence</p>
</th>
<th>
<p>Balanced Accuracy</p>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p>Model1</p>
</td>
<td>
<p>0.8707483</p>
</td>
<td>
<p>0.4191632</p>
</td>
<td>
<p>0.8525159</p>
</td>
<td>
<p>0.8874842</p>
</td>
<td>
<p>0.9095238</p>
</td>
<td>
<p>0.9999996</p>
</td>
<td>
<p>0.0e+00</p>
</td>
<td>
<p>0.8900524</p>
</td>
<td>
<p>0.6766917</p>
</td>
<td>
<p>0.9651257</p>
</td>
<td>
<p>0.3797468</p>
</td>
<td>
<p>0.9651257</p>
</td>
<td>
<p>0.8900524</p>
</td>
<td>
<p>0.9260700</p>
</td>
<td>
<p>0.9095238</p>
</td>
<td>
<p>0.8095238</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.7833720</p>
</td>
</tr>
<tr>
<td>
<p>Model2</p>
</td>
<td>
<p>0.8455782</p>
</td>
<td>
<p>0.3529603</p>
</td>
<td>
<p>0.8260781</p>
</td>
<td>
<p>0.8636860</p>
</td>
<td>
<p>0.8857143</p>
</td>
<td>
<p>0.9999985</p>
</td>
<td>
<p>6.4e-06</p>
</td>
<td>
<p>0.8863287</p>
</td>
<td>
<p>0.5297619</p>
</td>
<td>
<p>0.9359286</p>
</td>
<td>
<p>0.3755274</p>
</td>
<td>
<p>0.9359286</p>
</td>
<td>
<p>0.8863287</p>
</td>
<td>
<p>0.9104536</p>
</td>
<td>
<p>0.8857143</p>
</td>
<td>
<p>0.7850340</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.7080453</p>
</td>
</tr>
<tr>
<td>
<p>Model3</p>
</td>
<td>
<p>0.8571429</p>
</td>
<td>
<p>0.2936476</p>
</td>
<td>
<p>0.8382017</p>
</td>
<td>
<p>0.8746440</p>
</td>
<td>
<p>0.9394558</p>
</td>
<td>
<p>1.0000000</p>
</td>
<td>
<p>0.0e+00</p>
</td>
<td>
<p>0.8703838</p>
</td>
<td>
<p>0.6516854</p>
</td>
<td>
<p>0.9748581</p>
</td>
<td>
<p>0.2447257</p>
</td>
<td>
<p>0.9748581</p>
</td>
<td>
<p>0.8703838</p>
</td>
<td>
<p>0.9196634</p>
</td>
<td>
<p>0.9394558</p>
</td>
<td>
<p>0.8176871</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.7610346</p>
</td>
</tr>
<tr>
<td>
<p>Model4</p>
</td>
<td>
<p>0.8619048</p>
</td>
<td>
<p>0.3676334</p>
</td>
<td>
<p>0.8432050</p>
</td>
<td>
<p>0.8791447</p>
</td>
<td>
<p>0.9156463</p>
</td>
<td>
<p>1.0000000</p>
</td>
<td>
<p>0.0e+00</p>
</td>
<td>
<p>0.8826152</p>
</td>
<td>
<p>0.6370968</p>
</td>
<td>
<p>0.9635036</p>
</td>
<td>
<p>0.3333333</p>
</td>
<td>
<p>0.9635036</p>
</td>
<td>
<p>0.8826152</p>
</td>
<td>
<p>0.9212873</p>
</td>
<td>
<p>0.9156463</p>
</td>
<td>
<p>0.8081633</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.7598560</p>
</td>
</tr>
</tbody>
</table>
<p>One other thing I’ll mention in passing is that the <code>partykit</code> package<br>
offers a way of assessing the relative importance of the variables in<br>
the model via the <code>varimp</code> command. We’ll come back to this concept of<br>
variable importance later but for now a simple example of text and plot<br>
output.</p>
<div>
<div>
<pre><code><span>sort</span><span>(</span><span>varimp</span><span>(</span><span>chaidattrit1</span><span>),</span><span> </span><span>decreasing</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##                 JobLevel                 OverTime  EnvironmentSatisfaction 
##              0.142756888              0.114384725              0.071069051 
##         StockOptionLevel            MaritalStatus          JobSatisfaction 
##              0.058726463              0.030332565              0.029157845 
##    TrainingTimesLastYear RelationshipSatisfaction               Department 
##              0.025637743              0.015700750              0.013815233 
##           BusinessTravel           JobInvolvement 
##              0.009906245              0.009205317
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>sort</span><span>(</span><span>varimp</span><span>(</span><span>chaidattrit1</span><span>),</span><span> </span><span>decreasing</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="" src="https://i2.wp.com/ibecav.github.io/images/best11-1.png?w=456" data-recalc-dims="1"></p>
<h2>What about those other variables?</h2>
<p>But before we go much farther we should probably circle back and make<br>
use of all those variables that were coded as integers that we<br>
conveniently ignored in building our first four models. Let’s bring them<br>
into our model building activities and see what they can add to our<br>
understanding. As a first step let’s use <code>ggplot2</code> and take a look at<br>
their distribution using a density plot.</p>
<div>
<div>
<pre><code><span># Turning numeric variables into factors</span><span>
</span><span>## what do they look like</span><span>
</span><span>attrition</span><span> </span><span>%>%</span><span>
</span><span>select_if</span><span>(</span><span>is.numeric</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>metric</span><span>,</span><span> </span><span>value</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>value</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>metric</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_density</span><span>(</span><span>show.legend</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span> </span><span>~</span><span> </span><span>metric</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="" src="https://i2.wp.com/ibecav.github.io/images/best12-1.png?w=456" data-recalc-dims="1"></p>
<p>Well other than <code>Age</code> very few of those variables appear to have<br>
especially normal distributions. That’s okay we’re going to wind up<br>
cutting them up into factors anyway. The only question is what are the<br>
best cut-points to use? In base R the <code>cut</code> function default is equal<br>
intervals (distances along the x axis). You can also specify your own<br>
cutpoints and your own labels as shown below.</p>
<div>
<div>
<pre><code><span>table</span><span>(</span><span>cut</span><span>(</span><span>attrition</span><span>$</span><span>YearsWithCurrManager</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>5</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
## (-0.017,3.4]    (3.4,6.8]   (6.8,10.2]  (10.2,13.6]    (13.6,17] 
##          825          158          414           54           19
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>table</span><span>(</span><span>attrition</span><span>$</span><span>YearsSinceLastPromotion</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
##   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15 
## 581 357 159  52  61  45  32  76  18  17   6  24  10  10   9  13
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>table</span><span>(</span><span>cut</span><span>(</span><span>
</span><span>attrition</span><span>$</span><span>YearsSinceLastPromotion</span><span>,</span><span>
</span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-1</span><span>,</span><span> </span><span>0.9</span><span>,</span><span> </span><span>1.9</span><span>,</span><span> </span><span>2.9</span><span>,</span><span> </span><span>30</span><span>),</span><span>
</span><span>labels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Less than 1&quot;</span><span>,</span><span> </span><span>&quot;1&quot;</span><span>,</span><span> </span><span>&quot;2&quot;</span><span>,</span><span> </span><span>&quot;More than 2&quot;</span><span>)</span><span>
</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
## Less than 1           1           2 More than 2 
##         581         357         159         373
</code></pre>
</div>
</div>
<p><code>ggplot2</code> has three helper functions I prefer to use: <code>cut_interval</code>,<br>
<code>cut_number</code>, and <code>cut_width</code>. <code>cut_interval</code> makes n groups with equal<br>
range, <code>cut_number</code> makes n groups with (approximately) equal numbers of<br>
observations, and <code>cut_width</code> makes groups of a fixed specified width.<br>
As we think about moving the numeric variables into factors any of these<br>
might be a viable alternative.</p>
<div>
<div>
<pre><code><span># cut_interval makes n groups with equal range</span><span>
</span><span>table</span><span>(</span><span>cut_interval</span><span>(</span><span>attrition</span><span>$</span><span>YearsWithCurrManager</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>5</span><span>))</span><span> </span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
##     [0,3.4]   (3.4,6.8]  (6.8,10.2] (10.2,13.6]   (13.6,17] 
##         825         158         414          54          19
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span># cut_number makes n groups with (approximately) equal numbers of observations</span><span>
</span><span>table</span><span>(</span><span>cut_number</span><span>(</span><span>attrition</span><span>$</span><span>YearsWithCurrManager</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>5</span><span>))</span><span> </span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
##  [0,1]  (1,2]  (2,4]  (4,7] (7,17] 
##    339    344    240    276    271
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span># cut_width makes groups of width width</span><span>
</span><span>table</span><span>(</span><span>cut_width</span><span>(</span><span>attrition</span><span>$</span><span>YearsWithCurrManager</span><span>,</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>2</span><span>))</span><span> </span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
##  [-1,1]   (1,3]   (3,5]   (5,7]   (7,9]  (9,11] (11,13] (13,15] (15,17] 
##     339     486     129     245     171      49      32      10       9
</code></pre>
</div>
</div>
<p>For the sake of our current example let’s say that I would like to focus<br>
on groups of more or less equal size which means that I would need to<br>
apply <code>cut_number</code> to each of the 12 variables under discussion. I’m not<br>
enamored of running the function 12 times though so I would prefer to<br>
wrap it in a <code>mutate_if</code> statement. If the variable is numeric then<br>
apply <code>cut_number</code> with n=5.</p>
<p>The problem is that <code>cut_number</code> will error out if it doesn’t think<br>
there are enough values to produce the bins you requested. So…</p>
<div>
<div>
<pre><code><span>cut_number</span><span>(</span><span>attrition</span><span>$</span><span>YearsWithCurrManager</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>6</span><span>)</span><span>
</span><span># Error: Insufficient data values to produce 6 bins.</span><span>
</span><span>cut_number</span><span>(</span><span>attrition</span><span>$</span><span>YearsSinceLastPromotion</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span>
</span><span># Error: Insufficient data values to produce 4 bins.</span><span>
</span><span>attrition</span><span> </span><span>%>%</span><span> </span><span>mutate_if</span><span>(</span><span>is.numeric</span><span>,</span><span> </span><span>funs</span><span>(</span><span>cut_number</span><span>(</span><span>.</span><span>,</span><span> </span><span>n</span><span>=</span><span>5</span><span>)))</span><span>
</span><span># Error in mutate_impl(.data, dots) : </span><span>
</span><span># Evaluation error: Insufficient data values to produce 5 bins..</span><span>
</span></code></pre>
</div>
</div>
<p>A little sleuthing reveals that there is one variable among the 12 that<br>
has too few values for the <code>cut_number</code> function to work. That variable<br>
is <code>YearsSinceLastPromotion</code>. Let’s try what we would like but<br>
explicitly <code>select</code> out that variable.</p>
<div>
<div>
<pre><code><span>attrition</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>-</span><span>YearsSinceLastPromotion</span><span>)</span><span> </span><span>%>%</span><span> </span><span>mutate_if</span><span>(</span><span>is.numeric</span><span>,</span><span> </span><span>funs</span><span>(</span><span>cut_number</span><span>(</span><span>.</span><span>,</span><span> </span><span>n</span><span>=</span><span>5</span><span>)))</span><span> </span><span>%>%</span><span> </span><span>head</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##       Age Attrition    BusinessTravel          DailyRate
## 1 (38,45]       Yes     Travel_Rarely     (942,1.22e+03]
## 2 (45,60]        No Travel_Frequently          [102,392]
## 3 (34,38]       Yes     Travel_Rarely (1.22e+03,1.5e+03]
## 4 (29,34]        No Travel_Frequently (1.22e+03,1.5e+03]
## 5 [18,29]        No     Travel_Rarely          (392,656]
## 6 (29,34]        No Travel_Frequently     (942,1.22e+03]
##             Department DistanceFromHome     Education EducationField
## 1                Sales            [1,2]       College  Life_Sciences
## 2 Research_Development            (5,9] Below_College  Life_Sciences
## 3 Research_Development            [1,2]       College          Other
## 4 Research_Development            (2,5]        Master  Life_Sciences
## 5 Research_Development            [1,2] Below_College        Medical
## 6 Research_Development            [1,2]       College  Life_Sciences
##   EnvironmentSatisfaction Gender HourlyRate JobInvolvement JobLevel
## 1                  Medium Female   (87,100]           High        2
## 2                    High   Male    (59,73]         Medium        2
## 3               Very_High   Male   (87,100]         Medium        1
## 4               Very_High Female    (45,59]           High        1
## 5                     Low   Male    [30,45]           High        1
## 6               Very_High   Male    (73,87]           High        1
##                 JobRole JobSatisfaction MaritalStatus       MonthlyIncome
## 1       Sales_Executive       Very_High        Single (5.74e+03,9.86e+03]
## 2    Research_Scientist          Medium       Married (4.23e+03,5.74e+03]
## 3 Laboratory_Technician            High        Single  [1.01e+03,2.7e+03]
## 4    Research_Scientist            High       Married  (2.7e+03,4.23e+03]
## 5 Laboratory_Technician          Medium       Married  (2.7e+03,4.23e+03]
## 6 Laboratory_Technician       Very_High        Single  (2.7e+03,4.23e+03]
##           MonthlyRate NumCompaniesWorked OverTime PercentSalaryHike
## 1 (1.67e+04,2.17e+04]                  8      Yes           [11,12]
## 2  (2.17e+04,2.7e+04]                  1       No           (19,25]
## 3 [2.09e+03,6.89e+03]                  6      Yes           (13,15]
## 4  (2.17e+04,2.7e+04]                  1      Yes           [11,12]
## 5 (1.18e+04,1.67e+04]                  9       No           [11,12]
## 6 (1.18e+04,1.67e+04]                  0       No           (12,13]
##   PerformanceRating RelationshipSatisfaction StockOptionLevel
## 1         Excellent                      Low                0
## 2       Outstanding                Very_High                1
## 3         Excellent                   Medium                0
## 4         Excellent                     High                0
## 5         Excellent                Very_High                1
## 6         Excellent                     High                0
##   TotalWorkingYears TrainingTimesLastYear WorkLifeBalance YearsAtCompany
## 1             (5,8]                     0             Bad          (5,7]
## 2            (8,10]                     3          Better         (7,10]
## 3             (5,8]                     3          Better          [0,2]
## 4             (5,8]                     3          Better         (7,10]
## 5             (5,8]                     3          Better          [0,2]
## 6             (5,8]                     2            Good          (5,7]
##   YearsInCurrentRole YearsWithCurrManager
## 1              (2,4]                (4,7]
## 2              (4,7]                (4,7]
## 3              [0,1]                [0,1]
## 4              (4,7]                [0,1]
## 5              (1,2]                (1,2]
## 6              (4,7]                (4,7]
</code></pre>
</div>
</div>
<p>Yes that appears to be it. So let’s manually cut it into 4 groups and<br>
then apply the 5 grouping code to the other 11 variables. Once we have<br>
accomplished that we can run the same <code>newattrit &lt;- attrition %>%<br>
select_if(is.factor)</code> we ran earlier to produce a <code>newattrit</code> dataframe<br>
we can work with.</p>
<div>
<div>
<pre><code><span>attrition</span><span>$</span><span>YearsSinceLastPromotion</span><span> </span><span>&lt;-</span><span> </span><span>cut</span><span>(</span><span>
</span><span>attrition</span><span>$</span><span>YearsSinceLastPromotion</span><span>,</span><span>
</span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-1</span><span>,</span><span> </span><span>0.9</span><span>,</span><span> </span><span>1.9</span><span>,</span><span> </span><span>2.9</span><span>,</span><span> </span><span>30</span><span>),</span><span>
</span><span>labels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Less than 1&quot;</span><span>,</span><span> </span><span>&quot;1&quot;</span><span>,</span><span> </span><span>&quot;2&quot;</span><span>,</span><span> </span><span>&quot;More than 2&quot;</span><span>)</span><span>
</span><span>)</span><span>
</span><span>attrition</span><span> </span><span>&lt;-</span><span> </span><span>attrition</span><span> </span><span>%>%</span><span> </span><span>mutate_if</span><span>(</span><span>is.numeric</span><span>,</span><span> </span><span>funs</span><span>(</span><span>cut_number</span><span>(</span><span>.</span><span>,</span><span> </span><span>n</span><span>=</span><span>5</span><span>)))</span><span>
</span><span>summary</span><span>(</span><span>attrition</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##       Age      Attrition            BusinessTravel
##  [18,29]:326   No :1233   Non-Travel       : 150  
##  (29,34]:325   Yes: 237   Travel_Frequently: 277  
##  (34,38]:255              Travel_Rarely    :1043  
##  (38,45]:291                                      
##  (45,60]:273                                      
##                                                   
##                                                   
##               DailyRate                  Department  DistanceFromHome
##  [102,392]         :294   Human_Resources     : 63   [1,2]  :419     
##  (392,656]         :294   Research_Development:961   (2,5]  :213     
##  (656,942]         :294   Sales               :446   (5,9]  :308     
##  (942,1.22e+03]    :294                              (9,17] :253     
##  (1.22e+03,1.5e+03]:294                              (17,29]:277     
##                                                                      
##                                                                      
##          Education            EducationField EnvironmentSatisfaction
##  Below_College:170   Human_Resources : 27    Low      :284          
##  College      :282   Life_Sciences   :606    Medium   :287          
##  Bachelor     :572   Marketing       :159    High     :453          
##  Master       :398   Medical         :464    Very_High:446          
##  Doctor       : 48   Other           : 82                           
##                      Technical_Degree:132                           
##                                                                     
##     Gender       HourlyRate    JobInvolvement JobLevel
##  Female:588   [30,45] :306   Low      : 83    1:543   
##  Male  :882   (45,59] :298   Medium   :375    2:534   
##               (59,73] :280   High     :868    3:218   
##               (73,87] :312   Very_High:144    4:106   
##               (87,100]:274                    5: 69   
##                                                       
##                                                       
##                       JobRole     JobSatisfaction  MaritalStatus
##  Sales_Executive          :326   Low      :289    Divorced:327  
##  Research_Scientist       :292   Medium   :280    Married :673  
##  Laboratory_Technician    :259   High     :442    Single  :470  
##  Manufacturing_Director   :145   Very_High:459                  
##  Healthcare_Representative:131                                  
##  Manager                  :102                                  
##  (Other)                  :215                                  
##              MonthlyIncome              MonthlyRate  NumCompaniesWorked
##  [1.01e+03,2.7e+03] :294   [2.09e+03,6.89e+03]:294   1      :521       
##  (2.7e+03,4.23e+03] :294   (6.89e+03,1.18e+04]:294   0      :197       
##  (4.23e+03,5.74e+03]:294   (1.18e+04,1.67e+04]:294   3      :159       
##  (5.74e+03,9.86e+03]:294   (1.67e+04,2.17e+04]:294   2      :146       
##  (9.86e+03,2e+04]   :294   (2.17e+04,2.7e+04] :294   4      :139       
##                                                      7      : 74       
##                                                      (Other):234       
##  OverTime   PercentSalaryHike   PerformanceRating RelationshipSatisfaction
##  No :1054   [11,12]:408       Low        :   0    Low      :276           
##  Yes: 416   (12,13]:209       Good       :   0    Medium   :303           
##             (13,15]:302       Excellent  :1244    High     :459           
##             (15,19]:325       Outstanding: 226    Very_High:432           
##             (19,25]:226                                                   
##                                                                           
##                                                                           
##  StockOptionLevel TotalWorkingYears TrainingTimesLastYear WorkLifeBalance
##  0:631            [0,5]  :316       0: 54                 Bad   : 80     
##  1:596            (5,8]  :309       1: 71                 Good  :344     
##  2:158            (8,10] :298       2:547                 Better:893     
##  3: 85            (10,17]:261       3:491                 Best  :153     
##                   (17,40]:286       4:123                                
##                                     5:119                                
##                                     6: 65                                
##  YearsAtCompany YearsInCurrentRole YearsSinceLastPromotion
##  [0,2]  :342    [0,1] :301         Less than 1:581        
##  (2,5]  :434    (1,2] :372         1          :357        
##  (5,7]  :166    (2,4] :239         2          :159        
##  (7,10] :282    (4,7] :295         More than 2:373        
##  (10,40]:246    (7,18]:263                                
##                                                           
##                                                           
##  YearsWithCurrManager
##  [0,1] :339          
##  (1,2] :344          
##  (2,4] :240          
##  (4,7] :276          
##  (7,17]:271          
##                      
## 
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>newattrit</span><span> </span><span>&lt;-</span><span> </span><span>attrition</span><span> </span><span>%>%</span><span> </span><span>select_if</span><span>(</span><span>is.factor</span><span>)</span><span>
</span><span>dim</span><span>(</span><span>newattrit</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 1470   31
</code></pre>
</div>
</div>
<p>Now we have <code>newattrit</code> with all 30 predictor variables. We will simply<br>
repeat the process we used earlier to develop 4 new models.</p>
<div>
<div>
<pre><code><span># Repeat to produce models 5-8</span><span>
</span><span>chaidattrit5</span><span> </span><span>&lt;-</span><span> </span><span>chaid</span><span>(</span><span>Attrition</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>newattrit</span><span>)</span><span>
</span><span>print</span><span>(</span><span>chaidattrit5</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
## Model formula:
## Attrition ~ Age + BusinessTravel + DailyRate + Department + DistanceFromHome + 
##     Education + EducationField + EnvironmentSatisfaction + Gender + 
##     HourlyRate + JobInvolvement + JobLevel + JobRole + JobSatisfaction + 
##     MaritalStatus + MonthlyIncome + MonthlyRate + NumCompaniesWorked + 
##     OverTime + PercentSalaryHike + PerformanceRating + RelationshipSatisfaction + 
##     StockOptionLevel + TotalWorkingYears + TrainingTimesLastYear + 
##     WorkLifeBalance + YearsAtCompany + YearsInCurrentRole + YearsSinceLastPromotion + 
##     YearsWithCurrManager
## 
## Fitted party:
## [1] root
## |   [2] OverTime in No
## |   |   [3] YearsAtCompany in [0,2]
## |   |   |   [4] Age in [18,29], (29,34]
## |   |   |   |   [5] StockOptionLevel in 0
## |   |   |   |   |   [6] BusinessTravel in Non-Travel, Travel_Rarely: No (n = 56, err = 41.1%)
## |   |   |   |   |   [7] BusinessTravel in Travel_Frequently: Yes (n = 10, err = 10.0%)
## |   |   |   |   [8] StockOptionLevel in 1, 2, 3: No (n = 63, err = 15.9%)
## |   |   |   [9] Age in (34,38], (38,45], (45,60]
## |   |   |   |   [10] WorkLifeBalance in Bad: No (n = 4, err = 50.0%)
## |   |   |   |   [11] WorkLifeBalance in Good, Better, Best
## |   |   |   |   |   [12] EducationField in Human_Resources, Life_Sciences, Marketing, Medical: No (n = 92, err = 2.2%)
## |   |   |   |   |   [13] EducationField in Other, Technical_Degree: No (n = 13, err = 23.1%)
## |   |   [14] YearsAtCompany in (2,5], (5,7], (7,10], (10,40]
## |   |   |   [15] WorkLifeBalance in Bad: No (n = 45, err = 22.2%)
## |   |   |   [16] WorkLifeBalance in Good, Better, Best
## |   |   |   |   [17] JobSatisfaction in Low
## |   |   |   |   |   [18] StockOptionLevel in 0
## |   |   |   |   |   |   [19] RelationshipSatisfaction in Low: Yes (n = 11, err = 45.5%)
## |   |   |   |   |   |   [20] RelationshipSatisfaction in Medium: No (n = 12, err = 8.3%)
## |   |   |   |   |   |   [21] RelationshipSatisfaction in High: No (n = 17, err = 47.1%)
## |   |   |   |   |   |   [22] RelationshipSatisfaction in Very_High: No (n = 20, err = 0.0%)
## |   |   |   |   |   [23] StockOptionLevel in 1, 2, 3: No (n = 93, err = 4.3%)
## |   |   |   |   [24] JobSatisfaction in Medium, High, Very_High
## |   |   |   |   |   [25] Age in [18,29], (29,34], (34,38], (38,45]
## |   |   |   |   |   |   [26] BusinessTravel in Non-Travel, Travel_Rarely
## |   |   |   |   |   |   |   [27] JobInvolvement in Low: No (n = 25, err = 12.0%)
## |   |   |   |   |   |   |   [28] JobInvolvement in Medium, High, Very_High
## |   |   |   |   |   |   |   |   [29] RelationshipSatisfaction in Low: No (n = 81, err = 3.7%)
## |   |   |   |   |   |   |   |   [30] RelationshipSatisfaction in Medium, High: No (n = 198, err = 0.0%)
## |   |   |   |   |   |   |   |   [31] RelationshipSatisfaction in Very_High
## |   |   |   |   |   |   |   |   |   [32] DistanceFromHome in [1,2], (2,5], (5,9], (17,29]: No (n = 92, err = 2.2%)
## |   |   |   |   |   |   |   |   |   [33] DistanceFromHome in (9,17]: No (n = 13, err = 23.1%)
## |   |   |   |   |   |   [34] BusinessTravel in Travel_Frequently: No (n = 95, err = 8.4%)
## |   |   |   |   |   [35] Age in (45,60]
## |   |   |   |   |   |   [36] JobSatisfaction in Low, Medium, High
## |   |   |   |   |   |   |   [37] TotalWorkingYears in [0,5], (5,8], (8,10], (17,40]: No (n = 57, err = 0.0%)
## |   |   |   |   |   |   |   [38] TotalWorkingYears in (10,17]: No (n = 14, err = 28.6%)
## |   |   |   |   |   |   [39] JobSatisfaction in Very_High: No (n = 43, err = 20.9%)
## |   [40] OverTime in Yes
## |   |   [41] JobLevel in 1
## |   |   |   [42] StockOptionLevel in 0, 3
## |   |   |   |   [43] DistanceFromHome in [1,2], (2,5]
## |   |   |   |   |   [44] EnvironmentSatisfaction in Low: Yes (n = 12, err = 16.7%)
## |   |   |   |   |   [45] EnvironmentSatisfaction in Medium, High, Very_High: No (n = 33, err = 36.4%)
## |   |   |   |   [46] DistanceFromHome in (5,9], (9,17], (17,29]: Yes (n = 44, err = 18.2%)
## |   |   |   [47] StockOptionLevel in 1, 2
## |   |   |   |   [48] BusinessTravel in Non-Travel, Travel_Rarely: No (n = 50, err = 26.0%)
## |   |   |   |   [49] BusinessTravel in Travel_Frequently: Yes (n = 17, err = 35.3%)
## |   |   [50] JobLevel in 2, 3, 4, 5
## |   |   |   [51] MaritalStatus in Divorced, Married
## |   |   |   |   [52] EnvironmentSatisfaction in Low, Medium: No (n = 60, err = 20.0%)
## |   |   |   |   [53] EnvironmentSatisfaction in High, Very_High
## |   |   |   |   |   [54] TrainingTimesLastYear in 0, 6: No (n = 10, err = 40.0%)
## |   |   |   |   |   [55] TrainingTimesLastYear in 1, 2, 3, 4, 5
## |   |   |   |   |   |   [56] YearsInCurrentRole in [0,1], (1,2]: No (n = 36, err = 11.1%)
## |   |   |   |   |   |   [57] YearsInCurrentRole in (2,4], (4,7], (7,18]: No (n = 82, err = 0.0%)
## |   |   |   [58] MaritalStatus in Single
## |   |   |   |   [59] Department in Human_Resources, Research_Development: No (n = 37, err = 10.8%)
## |   |   |   |   [60] Department in Sales: Yes (n = 35, err = 40.0%)
## 
## Number of inner nodes:    28
## Number of terminal nodes: 32
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>
</span><span>chaidattrit5</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Default control sliced numerics&quot;</span><span>,</span><span>
</span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span>
</span><span>lty</span><span> </span><span>=</span><span> </span><span>&quot;solid&quot;</span><span>,</span><span>
</span><span>lwd</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span>
</span><span>fontsize</span><span> </span><span>=</span><span> </span><span>8</span><span>
</span><span>)</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i0.wp.com/ibecav.github.io/images/best18-1.png?w=456" data-recalc-dims="1"></p>
<div>
<div>
<pre><code><span>ctrl</span><span> </span><span>&lt;-</span><span> </span><span>chaid_control</span><span>(</span><span>minsplit</span><span> </span><span>=</span><span> </span><span>200</span><span>,</span><span> </span><span>minprob</span><span> </span><span>=</span><span> </span><span>0.05</span><span>)</span><span>
</span><span>chaidattrit6</span><span> </span><span>&lt;-</span><span> </span><span>chaid</span><span>(</span><span>Attrition</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>newattrit</span><span>,</span><span> </span><span>control</span><span> </span><span>=</span><span> </span><span>ctrl</span><span>)</span><span>
</span><span>print</span><span>(</span><span>chaidattrit6</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
## Model formula:
## Attrition ~ Age + BusinessTravel + DailyRate + Department + DistanceFromHome + 
##     Education + EducationField + EnvironmentSatisfaction + Gender + 
##     HourlyRate + JobInvolvement + JobLevel + JobRole + JobSatisfaction + 
##     MaritalStatus + MonthlyIncome + MonthlyRate + NumCompaniesWorked + 
##     OverTime + PercentSalaryHike + PerformanceRating + RelationshipSatisfaction + 
##     StockOptionLevel + TotalWorkingYears + TrainingTimesLastYear + 
##     WorkLifeBalance + YearsAtCompany + YearsInCurrentRole + YearsSinceLastPromotion + 
##     YearsWithCurrManager
## 
## Fitted party:
## [1] root
## |   [2] OverTime in No
## |   |   [3] YearsAtCompany in [0,2]
## |   |   |   [4] Age in [18,29], (29,34]: No (n = 129, err = 32.6%)
## |   |   |   [5] Age in (34,38], (38,45], (45,60]: No (n = 109, err = 6.4%)
## |   |   [6] YearsAtCompany in (2,5], (5,7], (7,10], (10,40]
## |   |   |   [7] WorkLifeBalance in Bad: No (n = 45, err = 22.2%)
## |   |   |   [8] WorkLifeBalance in Good, Better, Best
## |   |   |   |   [9] JobSatisfaction in Low: No (n = 153, err = 12.4%)
## |   |   |   |   [10] JobSatisfaction in Medium, High, Very_High
## |   |   |   |   |   [11] Age in [18,29], (29,34], (34,38], (38,45]
## |   |   |   |   |   |   [12] BusinessTravel in Non-Travel, Travel_Rarely
## |   |   |   |   |   |   |   [13] JobInvolvement in Low: No (n = 25, err = 12.0%)
## |   |   |   |   |   |   |   [14] JobInvolvement in Medium, High, Very_High
## |   |   |   |   |   |   |   |   [15] RelationshipSatisfaction in Low: No (n = 81, err = 3.7%)
## |   |   |   |   |   |   |   |   [16] RelationshipSatisfaction in Medium, High: No (n = 198, err = 0.0%)
## |   |   |   |   |   |   |   |   [17] RelationshipSatisfaction in Very_High: No (n = 105, err = 4.8%)
## |   |   |   |   |   |   [18] BusinessTravel in Travel_Frequently: No (n = 95, err = 8.4%)
## |   |   |   |   |   [19] Age in (45,60]: No (n = 114, err = 11.4%)
## |   [20] OverTime in Yes
## |   |   [21] JobLevel in 1: Yes (n = 156, err = 47.4%)
## |   |   [22] JobLevel in 2, 3, 4, 5
## |   |   |   [23] MaritalStatus in Divorced, Married: No (n = 188, err = 10.6%)
## |   |   |   [24] MaritalStatus in Single: No (n = 72, err = 34.7%)
## 
## Number of inner nodes:    11
## Number of terminal nodes: 13
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>
</span><span>chaidattrit6</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;minsplit = 200, minprob = 0.05&quot;</span><span>,</span><span>
</span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span>
</span><span>lty</span><span> </span><span>=</span><span> </span><span>&quot;solid&quot;</span><span>,</span><span>
</span><span>lwd</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span>
</span><span>fontsize</span><span> </span><span>=</span><span> </span><span>8</span><span>
</span><span>)</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i2.wp.com/ibecav.github.io/images/best18-2.png?w=456" data-recalc-dims="1"></p>
<div>
<div>
<pre><code><span>ctrl</span><span> </span><span>&lt;-</span><span> </span><span>chaid_control</span><span>(</span><span>maxheight</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span>
</span><span>chaidattrit7</span><span> </span><span>&lt;-</span><span> </span><span>chaid</span><span>(</span><span>Attrition</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>newattrit</span><span>,</span><span> </span><span>control</span><span> </span><span>=</span><span> </span><span>ctrl</span><span>)</span><span>
</span><span>print</span><span>(</span><span>chaidattrit7</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
## Model formula:
## Attrition ~ Age + BusinessTravel + DailyRate + Department + DistanceFromHome + 
##     Education + EducationField + EnvironmentSatisfaction + Gender + 
##     HourlyRate + JobInvolvement + JobLevel + JobRole + JobSatisfaction + 
##     MaritalStatus + MonthlyIncome + MonthlyRate + NumCompaniesWorked + 
##     OverTime + PercentSalaryHike + PerformanceRating + RelationshipSatisfaction + 
##     StockOptionLevel + TotalWorkingYears + TrainingTimesLastYear + 
##     WorkLifeBalance + YearsAtCompany + YearsInCurrentRole + YearsSinceLastPromotion + 
##     YearsWithCurrManager
## 
## Fitted party:
## [1] root
## |   [2] OverTime in No
## |   |   [3] YearsAtCompany in [0,2]
## |   |   |   [4] Age in [18,29], (29,34]: No (n = 129, err = 32.6%)
## |   |   |   [5] Age in (34,38], (38,45], (45,60]: No (n = 109, err = 6.4%)
## |   |   [6] YearsAtCompany in (2,5], (5,7], (7,10], (10,40]
## |   |   |   [7] WorkLifeBalance in Bad: No (n = 45, err = 22.2%)
## |   |   |   [8] WorkLifeBalance in Good, Better, Best: No (n = 771, err = 6.6%)
## |   [9] OverTime in Yes
## |   |   [10] JobLevel in 1
## |   |   |   [11] StockOptionLevel in 0, 3: Yes (n = 89, err = 34.8%)
## |   |   |   [12] StockOptionLevel in 1, 2: No (n = 67, err = 35.8%)
## |   |   [13] JobLevel in 2, 3, 4, 5
## |   |   |   [14] MaritalStatus in Divorced, Married: No (n = 188, err = 10.6%)
## |   |   |   [15] MaritalStatus in Single: No (n = 72, err = 34.7%)
## 
## Number of inner nodes:    7
## Number of terminal nodes: 8
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>
</span><span>chaidattrit7</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;maxheight = 3&quot;</span><span>,</span><span>
</span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span>
</span><span>lty</span><span> </span><span>=</span><span> </span><span>&quot;solid&quot;</span><span>,</span><span>
</span><span>lwd</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span>
</span><span>fontsize</span><span> </span><span>=</span><span> </span><span>8</span><span>
</span><span>)</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i2.wp.com/ibecav.github.io/images/best18-3.png?w=456" data-recalc-dims="1"></p>
<div>
<div>
<pre><code><span>ctrl</span><span> </span><span>&lt;-</span><span> </span><span>chaid_control</span><span>(</span><span>alpha2</span><span> </span><span>=</span><span> </span><span>.01</span><span>,</span><span> </span><span>alpha4</span><span> </span><span>=</span><span> </span><span>.01</span><span>)</span><span>
</span><span>chaidattrit8</span><span> </span><span>&lt;-</span><span> </span><span>chaid</span><span>(</span><span>Attrition</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>newattrit</span><span>,</span><span> </span><span>control</span><span> </span><span>=</span><span> </span><span>ctrl</span><span>)</span><span>
</span><span>print</span><span>(</span><span>chaidattrit8</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
## Model formula:
## Attrition ~ Age + BusinessTravel + DailyRate + Department + DistanceFromHome + 
##     Education + EducationField + EnvironmentSatisfaction + Gender + 
##     HourlyRate + JobInvolvement + JobLevel + JobRole + JobSatisfaction + 
##     MaritalStatus + MonthlyIncome + MonthlyRate + NumCompaniesWorked + 
##     OverTime + PercentSalaryHike + PerformanceRating + RelationshipSatisfaction + 
##     StockOptionLevel + TotalWorkingYears + TrainingTimesLastYear + 
##     WorkLifeBalance + YearsAtCompany + YearsInCurrentRole + YearsSinceLastPromotion + 
##     YearsWithCurrManager
## 
## Fitted party:
## [1] root
## |   [2] OverTime in No
## |   |   [3] YearsAtCompany in [0,2]
## |   |   |   [4] Age in [18,29], (29,34]
## |   |   |   |   [5] StockOptionLevel in 0: No (n = 66, err = 48.5%)
## |   |   |   |   [6] StockOptionLevel in 1, 2, 3: No (n = 63, err = 15.9%)
## |   |   |   [7] Age in (34,38], (38,45], (45,60]
## |   |   |   |   [8] WorkLifeBalance in Bad: No (n = 4, err = 50.0%)
## |   |   |   |   [9] WorkLifeBalance in Good, Better, Best: No (n = 105, err = 4.8%)
## |   |   [10] YearsAtCompany in (2,5], (5,7], (7,10], (10,40]
## |   |   |   [11] WorkLifeBalance in Bad: No (n = 45, err = 22.2%)
## |   |   |   [12] WorkLifeBalance in Good, Better, Best
## |   |   |   |   [13] JobSatisfaction in Low
## |   |   |   |   |   [14] JobRole in Healthcare_Representative, Human_Resources, Laboratory_Technician, Manager, Manufacturing_Director, Research_Director, Research_Scientist, Sales_Executive
## |   |   |   |   |   |   [15] StockOptionLevel in 0: No (n = 58, err = 22.4%)
## |   |   |   |   |   |   [16] StockOptionLevel in 1, 2, 3: No (n = 92, err = 3.3%)
## |   |   |   |   |   [17] JobRole in Sales_Representative: Yes (n = 3, err = 0.0%)
## |   |   |   |   [18] JobSatisfaction in Medium, High, Very_High: No (n = 618, err = 5.2%)
## |   [19] OverTime in Yes
## |   |   [20] JobLevel in 1
## |   |   |   [21] StockOptionLevel in 0, 3: Yes (n = 89, err = 34.8%)
## |   |   |   [22] StockOptionLevel in 1, 2: No (n = 67, err = 35.8%)
## |   |   [23] JobLevel in 2, 3, 4, 5
## |   |   |   [24] MaritalStatus in Divorced, Married: No (n = 188, err = 10.6%)
## |   |   |   [25] MaritalStatus in Single
## |   |   |   |   [26] Department in Human_Resources, Research_Development: No (n = 37, err = 10.8%)
## |   |   |   |   [27] Department in Sales: Yes (n = 35, err = 40.0%)
## 
## Number of inner nodes:    13
## Number of terminal nodes: 14
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>
</span><span>chaidattrit8</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;alpha2 = .01, alpha4 = .01&quot;</span><span>,</span><span>
</span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span>
</span><span>lty</span><span> </span><span>=</span><span> </span><span>&quot;solid&quot;</span><span>,</span><span>
</span><span>lwd</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span>
</span><span>fontsize</span><span> </span><span>=</span><span> </span><span>8</span><span>
</span><span>)</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i1.wp.com/ibecav.github.io/images/best18-4.png?w=456" data-recalc-dims="1"></p>
<p>As we did earlier we’ll also repeat the steps necessary to build a table<br>
of results.</p>
<div>
<div>
<pre><code><span>modellist</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>Model1</span><span> </span><span>=</span><span> </span><span>chaidattrit1</span><span>,</span><span> </span><span>Model2</span><span> </span><span>=</span><span> </span><span>chaidattrit2</span><span>,</span><span> </span><span>Model3</span><span> </span><span>=</span><span> </span><span>chaidattrit3</span><span>,</span><span> </span><span>Model4</span><span> </span><span>=</span><span> </span><span>chaidattrit4</span><span>,</span><span> </span><span>Model5</span><span> </span><span>=</span><span> </span><span>chaidattrit5</span><span>,</span><span> </span><span>Model6</span><span> </span><span>=</span><span> </span><span>chaidattrit6</span><span>,</span><span> </span><span>Model7</span><span> </span><span>=</span><span> </span><span>chaidattrit7</span><span>,</span><span> </span><span>Model8</span><span> </span><span>=</span><span> </span><span>chaidattrit8</span><span>)</span><span>
</span><span>CHAIDResults</span><span> </span><span>&lt;-</span><span> </span><span>map</span><span>(</span><span>modellist</span><span>,</span><span> </span><span>~</span><span> </span><span>predict</span><span>(</span><span>.x</span><span>))</span><span> </span><span>%>%</span><span> </span><span>map</span><span>(</span><span>~</span><span> </span><span>confusionMatrix</span><span>(</span><span>newattrit</span><span>$</span><span>Attrition</span><span>,</span><span> </span><span>.x</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>map_dfr</span><span>(</span><span>~</span><span> </span><span>cbind</span><span>(</span><span>as.data.frame</span><span>(</span><span>t</span><span>(</span><span>.x</span><span>$</span><span>overall</span><span>)),</span><span>as.data.frame</span><span>(</span><span>t</span><span>(</span><span>.x</span><span>$</span><span>byClass</span><span>))),</span><span> </span><span>.id</span><span> </span><span>=</span><span> </span><span>&quot;ModelNumb&quot;</span><span>)</span><span>
</span><span>kable</span><span>(</span><span>CHAIDResults</span><span>,</span><span> </span><span>&quot;html&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>kable_styling</span><span>(</span><span>bootstrap_options</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;striped&quot;</span><span>,</span><span> </span><span>&quot;hover&quot;</span><span>,</span><span> </span><span>&quot;condensed&quot;</span><span>,</span><span> </span><span>&quot;responsive&quot;</span><span>),</span><span> </span><span>font_size</span><span> </span><span>=</span><span> </span><span>10</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<table>
<thead>
<tr>
<th>
<p>ModelNumb</p>
</th>
<th>
<p>Accuracy</p>
</th>
<th>
<p>Kappa</p>
</th>
<th>
<p>AccuracyLower</p>
</th>
<th>
<p>AccuracyUpper</p>
</th>
<th>
<p>AccuracyNull</p>
</th>
<th>
<p>AccuracyPValue</p>
</th>
<th>
<p>McnemarPValue</p>
</th>
<th>
<p>Sensitivity</p>
</th>
<th>
<p>Specificity</p>
</th>
<th>
<p>Pos Pred Value</p>
</th>
<th>
<p>Neg Pred Value</p>
</th>
<th>
<p>Precision</p>
</th>
<th>
<p>Recall</p>
</th>
<th>
<p>F1</p>
</th>
<th>
<p>Prevalence</p>
</th>
<th>
<p>Detection Rate</p>
</th>
<th>
<p>Detection Prevalence</p>
</th>
<th>
<p>Balanced Accuracy</p>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p>Model1</p>
</td>
<td>
<p>0.8707483</p>
</td>
<td>
<p>0.4191632</p>
</td>
<td>
<p>0.8525159</p>
</td>
<td>
<p>0.8874842</p>
</td>
<td>
<p>0.9095238</p>
</td>
<td>
<p>0.9999996</p>
</td>
<td>
<p>0.0e+00</p>
</td>
<td>
<p>0.8900524</p>
</td>
<td>
<p>0.6766917</p>
</td>
<td>
<p>0.9651257</p>
</td>
<td>
<p>0.3797468</p>
</td>
<td>
<p>0.9651257</p>
</td>
<td>
<p>0.8900524</p>
</td>
<td>
<p>0.9260700</p>
</td>
<td>
<p>0.9095238</p>
</td>
<td>
<p>0.8095238</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.7833720</p>
</td>
</tr>
<tr>
<td>
<p>Model2</p>
</td>
<td>
<p>0.8455782</p>
</td>
<td>
<p>0.3529603</p>
</td>
<td>
<p>0.8260781</p>
</td>
<td>
<p>0.8636860</p>
</td>
<td>
<p>0.8857143</p>
</td>
<td>
<p>0.9999985</p>
</td>
<td>
<p>6.4e-06</p>
</td>
<td>
<p>0.8863287</p>
</td>
<td>
<p>0.5297619</p>
</td>
<td>
<p>0.9359286</p>
</td>
<td>
<p>0.3755274</p>
</td>
<td>
<p>0.9359286</p>
</td>
<td>
<p>0.8863287</p>
</td>
<td>
<p>0.9104536</p>
</td>
<td>
<p>0.8857143</p>
</td>
<td>
<p>0.7850340</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.7080453</p>
</td>
</tr>
<tr>
<td>
<p>Model3</p>
</td>
<td>
<p>0.8571429</p>
</td>
<td>
<p>0.2936476</p>
</td>
<td>
<p>0.8382017</p>
</td>
<td>
<p>0.8746440</p>
</td>
<td>
<p>0.9394558</p>
</td>
<td>
<p>1.0000000</p>
</td>
<td>
<p>0.0e+00</p>
</td>
<td>
<p>0.8703838</p>
</td>
<td>
<p>0.6516854</p>
</td>
<td>
<p>0.9748581</p>
</td>
<td>
<p>0.2447257</p>
</td>
<td>
<p>0.9748581</p>
</td>
<td>
<p>0.8703838</p>
</td>
<td>
<p>0.9196634</p>
</td>
<td>
<p>0.9394558</p>
</td>
<td>
<p>0.8176871</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.7610346</p>
</td>
</tr>
<tr>
<td>
<p>Model4</p>
</td>
<td>
<p>0.8619048</p>
</td>
<td>
<p>0.3676334</p>
</td>
<td>
<p>0.8432050</p>
</td>
<td>
<p>0.8791447</p>
</td>
<td>
<p>0.9156463</p>
</td>
<td>
<p>1.0000000</p>
</td>
<td>
<p>0.0e+00</p>
</td>
<td>
<p>0.8826152</p>
</td>
<td>
<p>0.6370968</p>
</td>
<td>
<p>0.9635036</p>
</td>
<td>
<p>0.3333333</p>
</td>
<td>
<p>0.9635036</p>
</td>
<td>
<p>0.8826152</p>
</td>
<td>
<p>0.9212873</p>
</td>
<td>
<p>0.9156463</p>
</td>
<td>
<p>0.8081633</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.7598560</p>
</td>
</tr>
<tr>
<td>
<p>Model5</p>
</td>
<td>
<p>0.8775510</p>
</td>
<td>
<p>0.4451365</p>
</td>
<td>
<p>0.8596959</p>
</td>
<td>
<p>0.8938814</p>
</td>
<td>
<p>0.9122449</p>
</td>
<td>
<p>0.9999968</p>
</td>
<td>
<p>0.0e+00</p>
</td>
<td>
<p>0.8926174</p>
</td>
<td>
<p>0.7209302</p>
</td>
<td>
<p>0.9708029</p>
</td>
<td>
<p>0.3924051</p>
</td>
<td>
<p>0.9708029</p>
</td>
<td>
<p>0.8926174</p>
</td>
<td>
<p>0.9300699</p>
</td>
<td>
<p>0.9122449</p>
</td>
<td>
<p>0.8142857</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.8067738</p>
</td>
</tr>
<tr>
<td>
<p>Model6</p>
</td>
<td>
<p>0.8442177</p>
</td>
<td>
<p>0.3317731</p>
</td>
<td>
<p>0.8246542</p>
</td>
<td>
<p>0.8623944</p>
</td>
<td>
<p>0.8938776</p>
</td>
<td>
<p>1.0000000</p>
</td>
<td>
<p>1.0e-07</p>
</td>
<td>
<p>0.8820396</p>
</td>
<td>
<p>0.5256410</p>
</td>
<td>
<p>0.9399838</p>
</td>
<td>
<p>0.3459916</p>
</td>
<td>
<p>0.9399838</p>
</td>
<td>
<p>0.8820396</p>
</td>
<td>
<p>0.9100903</p>
</td>
<td>
<p>0.8938776</p>
</td>
<td>
<p>0.7884354</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.7038403</p>
</td>
</tr>
<tr>
<td>
<p>Model7</p>
</td>
<td>
<p>0.8571429</p>
</td>
<td>
<p>0.2936476</p>
</td>
<td>
<p>0.8382017</p>
</td>
<td>
<p>0.8746440</p>
</td>
<td>
<p>0.9394558</p>
</td>
<td>
<p>1.0000000</p>
</td>
<td>
<p>0.0e+00</p>
</td>
<td>
<p>0.8703838</p>
</td>
<td>
<p>0.6516854</p>
</td>
<td>
<p>0.9748581</p>
</td>
<td>
<p>0.2447257</p>
</td>
<td>
<p>0.9748581</p>
</td>
<td>
<p>0.8703838</p>
</td>
<td>
<p>0.9196634</p>
</td>
<td>
<p>0.9394558</p>
</td>
<td>
<p>0.8176871</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.7610346</p>
</td>
</tr>
<tr>
<td>
<p>Model8</p>
</td>
<td>
<p>0.8639456</p>
</td>
<td>
<p>0.3808988</p>
</td>
<td>
<p>0.8453515</p>
</td>
<td>
<p>0.8810715</p>
</td>
<td>
<p>0.9136054</p>
</td>
<td>
<p>1.0000000</p>
</td>
<td>
<p>0.0e+00</p>
</td>
<td>
<p>0.8845867</p>
</td>
<td>
<p>0.6456693</p>
</td>
<td>
<p>0.9635036</p>
</td>
<td>
<p>0.3459916</p>
</td>
<td>
<p>0.9635036</p>
</td>
<td>
<p>0.8845867</p>
</td>
<td>
<p>0.9223602</p>
</td>
<td>
<p>0.9136054</p>
</td>
<td>
<p>0.8081633</p>
</td>
<td>
<p>0.8387755</p>
</td>
<td>
<p>0.7651280</p>
</td>
</tr>
</tbody>
</table>
<p>You can clearly see that <code>Overtime</code> remains the first cut in our tree<br>
structure but that now other variables have started to influence our<br>
model as well, such as how long they’ve worked for us and their age. You<br>
can see from the table that model #5 is apparently the most accurate<br>
now. Not by a huge amount but apparently these numeric variables we<br>
ignored at first pass do matter at least to some degree.</p>
<h2>Not done yet</h2>
<p>I’m not going to dwell on the current results too much they are simply<br>
for an example and in my next post I’d like to spend some time on<br>
over-fitting and cross validation.</p>
<p>I hope you’ve found this useful. I am always open to comments,<br>
corrections and suggestions.</p>
<p>Chuck (ibecav at gmail dot<br>
com)</p>
<h3>License</h3>
<p><a rel="nofollow" href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank"><img alt="Creative Commons License" src="https://i1.wp.com/i.creativecommons.org/l/by-sa/4.0/88x31.png?w=456&ssl=1" data-recalc-dims="1"></a><br>This<br>
work is licensed under a<br>
<a rel="nofollow" href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank">Creative<br>
Commons Attribution-ShareAlike 4.0 International License</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ibecav.github.io/chaidtutor1/"> Chuck Powell</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/gXY4yc-4u-Y" height="1"></a></div>