21.28 Rules Using Rpart Plot

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

rpart.rules(model)
##  rain_tomorrow                                                      
##           0.14 when humidity_3pm <  72                              
##           0.37 when humidity_3pm is 72 to 84 & wind_gust_speed <  42
##           0.62 when humidity_3pm is 72 to 84 & wind_gust_speed >= 42
##           0.79 when humidity_3pm >=       84

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.37 when humidity_3pm is 72 to 84 & wind_gust_speed <  42      6%
##           0.62 when humidity_3pm is 72 to 84 & wind_gust_speed >= 42      4%
##           0.79 when humidity_3pm >=       84                              6%
rpart.rules(model, extra=4)
##  rain_tomorrow   No Yes                                                       
##             No [.86 .14] when humidity_3pm <  72                              
##             No [.63 .37] when humidity_3pm is 72 to 84 & wind_gust_speed <  42
##            Yes [.38 .62] when humidity_3pm is 72 to 84 & wind_gust_speed >= 42
##            Yes [.21 .79] when humidity_3pm >=       84

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.8611423 0.1388577 because humidity_3pm <  72                              
## 2 0.2111623 0.7888377 because humidity_3pm >=       84                        
## 3 0.6269006 0.3730994 because humidity_3pm is 72 to 84 & wind_gust_speed <  42

References

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


Your donation will support ongoing development 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-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.