20.28 Rules Using Rpart Plot

We can also use rpart.plot::rpart.rules() from Stephen Milborrow’s excellent rpart.plot (Milborrow 2024) package. This will convert the decision tree into rules:

rpart.rules(model)
##  rain_tomorrow                                                 
##           0.14 when humidity_3pm <  72                         
##           0.35 when humidity_3pm is 72 to 83 & rainfall <  0.85
##           0.59 when humidity_3pm is 72 to 83 & rainfall >= 0.85
##           0.77 when humidity_3pm >=       83

Some variations appear below. The first adds the coverage of the rule, which is the percentage of the training dataset that ends up in this part of the decision tree. The second adds the proportions of observations within a leaf node that correspond to No/Yes.

rpart.rules(model, cover=TRUE)
##  rain_tomorrow                                                    cover
##           0.14 when humidity_3pm <  72                              84%
##           0.35 when humidity_3pm is 72 to 83 & rainfall <  0.85      5%
##           0.59 when humidity_3pm is 72 to 83 & rainfall >= 0.85      4%
##           0.77 when humidity_3pm >=       83                         7%
rpart.rules(model, extra=4)
##  rain_tomorrow   No Yes                                                  
##             No [.86 .14] when humidity_3pm <  72                         
##             No [.65 .35] when humidity_3pm is 72 to 83 & rainfall <  0.85
##            Yes [.41 .59] when humidity_3pm is 72 to 83 & rainfall >= 0.85
##            Yes [.23 .77] when humidity_3pm >=       83

We can even obtain an explanation of the rules used in a prediction. Here we select specific observations to obtain the prediction and the rule used to make that prediction.

rpart.predict(model, newdata=ds[c(10, 12, 17), vars], rules=TRUE)
##          No       Yes                                                    
## 1 0.8603206 0.1396794 because humidity_3pm <  72                         
## 2 0.2273650 0.7726350 because humidity_3pm >=       83                   
## 3 0.6514460 0.3485540 because humidity_3pm is 72 to 83 & rainfall <  0.85

References

Milborrow, Stephen. 2024. Rpart.plot: Plot Rpart Models: An Enhanced Version of Plot.rpart. http://www.milbo.org/rpart-plot/index.html.


Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0