20.27 Rules Using Rpart Plot

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

rpart.rules(model)
##  rain_tomorrow                                                      
##           0.14 when humidity_3pm <  72                              
##           0.36 when humidity_3pm is 72 to 83 & wind_gust_speed <  42
##           0.60 when humidity_3pm is 72 to 83 & wind_gust_speed >= 42
##           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                                   83%
##           0.36 when humidity_3pm is 72 to 83 & wind_gust_speed <  42      5%
##           0.60 when humidity_3pm is 72 to 83 & wind_gust_speed >= 42      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 [.64 .36] when humidity_3pm is 72 to 83 & wind_gust_speed <  42
##            Yes [.40 .60] when humidity_3pm is 72 to 83 & wind_gust_speed >= 42
##            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.8626962 0.1373038 because humidity_3pm <  72                              
## 2 0.2279167 0.7720833 because humidity_3pm >=       83                        
## 3 0.6350854 0.3649146 because humidity_3pm is 72 to 83 & wind_gust_speed <  42

References

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


If you find this curated material useful then you can consider a donation to support it's ongoing availability and give you access to the PDF version of this book. The material has been scoped up by Generative AI without permission or any kind of recompense so do consider a donation if you can afford it. Unlike Generative AI your access to this materials is freely given. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Togaware has a 30 year tradition of making popular open source software which includes sold privacy preserving productivity apps, 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