<span><span lang="en"><span>We will develop a classification exercise using C5.0 decision tree algorithm. The exercise was originally published in <i>&quot;Machine Learning in R&quot; </i>by Brett Lantz, PACKT publishing 2015 (open source community experience destilled).</span></span></span><br><br><span><span lang="en"><span><span lang="en"><span>The example we will develop is about identifying risky bank loans.</span></span></span></span></span><br><span><br><span lang="en"><span><span lang="en"><span><span lang="en"><span>We will carry out the exercise verbatim as published in the aforementioned reference.</span></span></span></span></span></span></span><br><span><br></span><br><span>### install required packages<br>#install.packages(&quot;C50&quot;)<br><br>### load required packages<br>library(C50)<br>library(gmodels)<br><br>### read and explore the data<br>credit &lt;- read.csv(&quot;credit.csv&quot;)<br>str(credit)<br><br>### look a couple of loan features that seem likely<br>### to predict a default (currency Deutsche Marks DM)<br>table(credit$checking_balance)<br>table(credit$savings_balance)<br><br>### some of the loan's features are numeric<br>summary(credit$months_loan_duration)<br>summary(credit$amount)<br><br>### $default indicates whether the loan applicant<br>### went or not into default<br><br>### change levels of $default<br>credit$default&lt;- factor(credit$default, levels = c(&quot;1&quot;, &quot;2&quot;),<br>                        labels = c(&quot;no&quot;, &quot;yes&quot;))<br> </span><br><span>### see frequencies of $default<br>table(credit$default)<br><br>### data preparation<br>### creating random training (90%) and test datasets (10%)<br><br>### random sample numbers<br>set.seed(123)<br>train_sample &lt;- sample(1:1000, 900)<br>  <br>### split the dataset<br>credit_train &lt;- credit[train_sample, ]<br>credit_test &lt;- credit[-train_sample, ]<br><br>### check for about 30% of defaulted loans in each of<br>### the datasets<br>prop.table(table(credit_train$default))<br>prop.table(table(credit_test$default))<br><br>### training a model on the data<br><br>### $default column in credit_train is the default class<br>### variable, so we need to exclude it from the training<br>### data frame, but supply it as the target factor vector<br>### for classification<br>credit_model &lt;- C5.0(credit_train[-17], credit_train$default)<br><br>### see some basic data about the tree<br>credit_model<br>summary(credit_model)<br><br>### evaluating model performance<br>credit_pred &lt;- predict(credit_model, credit_test)<br> </span><br><span>CrossTable(credit_test$default, credit_pred,<br>           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,<br>           dnn = c('actual default', 'predicted default'))</span><br><br><br><div><span><a href="https://1.bp.blogspot.com/-1i20IvGVBDg/WQe1hxt1hqI/AAAAAAAAC1U/Nmuhtk-9qLEf2EFDl9MKVrnT2Fa3mAAYACLcB/s1600/c50_2.png"></a></span></div><span>### improving model performance?<br>### boosting the accuracy of decision trees<br>### add an additional trials parameter indicating the<br>### number of separate decision trees to use in the<br>### boosted team<br>credit_boost10 &lt;- C5.0(credit_train[-17], credit_train$default,<br>                       trials = 10)<br><br>credit_boost10<br>summary(credit_boost10)<br><br>credit_boost_pred10 &lt;- predict(credit_boost10, credit_test)<br> </span><br><span>CrossTable(credit_test$default, credit_boost_pred10,<br>           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,<br>           dnn = c('actual default', 'predicted default'))</span><br><span><br></span><br><div><span><a href="https://1.bp.blogspot.com/-JKs-Mc6m2sc/WQe12VwJpXI/AAAAAAAAC1Y/azBQzhG0P_0iQBaH4QzSxbXDdu1dgN2HACLcB/s1600/c50_3.png"></a></span></div><span>### a way to reduces false negatives<br>### set penalties to different types of errors<br>### cost matrix<br>matrix_dimensions &lt;- list(c(&quot;no&quot;, &quot;yes&quot;), c(&quot;no&quot;, &quot;yes&quot;))<br> </span><br><span>names(matrix_dimensions) &lt;- c(&quot;predicted&quot;, &quot;actual&quot;)<br><br>matrix_dimensions<br><br>### assign the penalty for the types of errors<br><br>### if a loan default costs four times as much as<br>### a missed opportunity<br><br>error_cost &lt;- matrix(c(0, 1, 4, 0), nrow = 2,<br>                     dimnames = matrix_dimensions)<br> </span><br><span>### false negative has a cost of 4 versus a false<br>### positive's cost of 1</span><br><span>error_cost<br><br>### now use costs parameter of the C50()<br>credit_cost &lt;- C5.0(credit_train[-17], credit_train$default,<br>                    costs = error_cost)<br> </span><br><span>credit_cost_pred &lt;- predict(credit_cost, credit_test)<br> </span><br><span>CrossTable(credit_test$default, credit_cost_pred,<br>           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,<br>           dnn = c('actual default', 'predicted default'))</span><br><br><br><div><span><a href="https://4.bp.blogspot.com/-Cg3WtJspO_Q/WQe2XSXBg3I/AAAAAAAAC1g/2nsrUdLO6AA4kk0-OVtMN1A4Ln8XwGIBACLcB/s1600/c50_4.png"></a></span></div><span><br></span><span>### false negatives was reduced at the expense of<br>### increasing false positives</span><br><br><span><span lang="en"><span>You can get the exercise and the dataset in:</span></span> </span><br><span><span><a target="_blank" href="https://github.com/pakinja/Data-R-Value/tree/master/MachineLearning_C5.0_Classification_Algorithm">https://github.com/pakinja/Data-R-Value</a></span></span><br><br>