<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/nyc-buses-c5-0-classification-with-r-more-than-20-minute-delay/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/nyc-buses-c5-0-classification-with-r-more-than-20-minute-delay/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<ul></ul>
<p><strong>
<p>Categories</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/category/advanced-modeling/" title="Permanent Link toAdvanced Modeling" target="_blank">Advanced Modeling</a></li>
</ol>
<p><strong>
<p>Tags</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/tag/data-management/" title="Permanent Link toData Management" target="_blank">Data Management</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/tag/data-visualisation/" title="Permanent Link toData Visualisation" target="_blank">Data Visualisation</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/tag/rstats/" title="Permanent Link toR Programming" target="_blank">R Programming</a></li>
</ol>
<p>We are continuing on with our <a rel="nofollow" href="https://datascienceplus.com/nyc-bus-delays/" target="_blank">NYC bus breakdown problem</a>. When we left off, we had constructed a rule-based Cubist regression model with our <a rel="nofollow" href="https://datascienceplus.com/nyc-buses-company-level-predictors-with-r/" target="_blank">expanded pool of predictors</a>; but we were still only managing to explain 37% of the data's variance with our model. Given how 'dirty' the target variable 'time_delayed' is (because it is human reported and of dubious precision), we decided that perhaps we should rephrase the question in order to get a more sensible answer. When a bus breakdown is called into operations, perhaps the question to ask is: “Will this delay exceed twenty minutes?”</p>
<h2>Classification – more than 20 minutes?</h2>
<p>We could choose some other time, but for simplicity, twenty minutes is probably the breaking point of human patience – be they, passengers or providers. This division also breaks the dataset approximately in half, so we don't have to deal with imbalance. As a side note, I have also run the problem for a thirty minute or more delay, breaking the data into ¾ and ¼; and the solution only improves.</p>
<p>We begin by setting up the data that we need, and the caret control objects:</p>
<pre><code>in_csv &lt;- &quot;../output/intermediate/ii_times.csv&quot;
ii_times &lt;- read_csv(in_csv) %>%
  filter(reported_before_resolved == 1) %>%
  select(-reported_before_resolved) %>%
  mutate(
    time_delayed = cut(time_delayed, breaks = c(0, 22, 10000), labels = FALSE)
  ) %>%
  mutate(time_delayed = factor(time_delayed, labels = c(&quot;t0.22&quot;,&quot;t22.&quot;)))
halfFold &lt;- createFolds(ii_times$Busbreakdown_ID, k = 5, list=TRUE)
ii_small &lt;- ii_times[halfFold[[2]],]
ii_test &lt;-ii_times[halfFold[[3]],]
ii_train &lt;- ii_times %>%
  anti_join(ii_test, by = &quot;Busbreakdown_ID&quot;)
rctrl_manual &lt;- trainControl(method = &quot;none&quot;, returnResamp = &quot;all&quot;, classProbs = TRUE, summaryFunction = twoClassSummary)
rctrl_repcv &lt;- trainControl(method = &quot;repeatedcv&quot;, number = 2, repeats = 5, returnResamp = &quot;all&quot;, classProbs = TRUE, summaryFunction = twoClassSummary)
</code></pre>
<p>Then we can use cross-fold validation on a reduced data set to quickly get a sense of which parameters we should use. Note that we deliberately choose 'rules', not 'tree' here, because we want a human-readable set of if/then conditions. Unfortunately, we will end up using 15 trials, which means our rule set is large. I have commented its output here, but it can be read this way, or with 'summary(model)'.</p>
<pre><code>#---cubist
c50_grd&lt;-expand.grid(
  .winnow = FALSE,
  .trials=c(15,30,50),
  .model=&quot;rules&quot;
)
set.seed(849)
c50_cv &lt;- train(time_delayed ~ . -Busbreakdown_ID,
  data = ii_small,
  method = &quot;C5.0&quot;,
  metric=&quot;ROC&quot;,
  na.action = na.pass,
  trControl = rctrl_repcv,
  tuneGrid = c50_grd
  )
c50_cv</code>
<em>## C5.0 ## ## 40665 samples
## 46 predictor
## 2 classes: 't0.22', 't22.' ## ## No pre-processing
## Resampling: Cross-Validated (2 fold, repeated 5 times) ## Summary of sample sizes: 20332, 20333, 20332, 20333, 20332, 20333, ... ## Resampling results across tuning parameters:
## ## trials ROC Sens Spec ## 15 0.8261069 0.8028816 0.6830990
## 30 0.8258429 0.8030490 0.6855583
## 50 0.8258429 0.8030490 0.6855583
## ## Tuning parameter 'model' was held constant at a value of rules
## ## Tuning parameter 'winnow' was held constant at a value of FALSE
## ROC was used to select the optimal model using the largest value.
## The final values used for the model were trials = 15, model = rules
## and winnow = FALSE.
</em></pre>
<pre><code>#writeLines(c50_cv$finalModel$rules)
#We can output the rule set as a block of text like this, but we have suppressed this output because with 50 trials, it is a very, very long list.
</code></pre>
<p>The system is relatively insensitive to the number of trials, so we can use 15. We run on our training and test sets to derive our true test ROC, and have a look at how our predicted probabilities correlate with the predictors, compared to the actual values.</p>
<pre><code>c50_grd&lt;-expand.grid(
  .winnow = FALSE,
  .trials=15,
  .model=&quot;rules&quot;
)
c50_man &lt;- train(time_delayed ~ . -Busbreakdown_ID,
  data = ii_train,
  method = &quot;C5.0&quot;,
  metric=&quot;ROC&quot;,
  na.action = na.pass,
  trControl = rctrl_manual,
  tuneGrid = c50_grd
)
predictions &lt;- predict(c50_man, newdata = ii_test, type = &quot;prob&quot;) 
ii_withpred &lt;- ii_test %>%
  cbind(predictions %>% tbl_df())
colAUC(ii_withpred[[&quot;t0.22&quot;]], (ii_withpred[[&quot;time_delayed&quot;]] ==&quot;t0.22&quot;) * 1L, plotROC=TRUE)
</code>
<em>## [,1]
## 0 vs. 1 0.8486122
</em></pre>
<p><a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/11/unnamed-chunk-3-1-15.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/11/unnamed-chunk-3-1-15-490x490.png?w=450&ssl=1" alt="" class="wp-image-18747" data-recalc-dims="1"></a></p>
<pre><code>#cubist: 0.847
corrr_analysis &lt;- ii_withpred %>%
  select(-Busbreakdown_ID, -t0.22, -t22.) %>%
  mutate(
    time_delayed = (time_delayed == &quot;t22.&quot;)*1L 
  ) %>%
  correlate() %>%
  focus(time_delayed) %>%
  rename(feature = rowname) %>%
  arrange(desc(abs(time_delayed))) %>%
  mutate(feature = as_factor(feature)) 
corrr_analysis %>% print(n=61)</code>
<em>## # A tibble: 45 x 2
## feature time_delayed
## </em></pre>