rpart
rpart copied to clipboard
Obtaining "Decision Rules" from rpart trees
Hello,
I used the "rpart" library and fit a decision tree using some data:
#from a previous question : https://stackoverflow.com/questions/65678552/r-changing-plot-sizes
library(rpart)
car.test.frame$Reliability = as.factor(car.test.frame$Reliability)
z.auto <- rpart(Reliability ~ ., car.test.frame)
plot(z.auto)
text(z.auto, use.n=TRUE, xpd=TRUE, cex=.8)
I am looking for an easier way to summarize the results of this tree in case the tree becomes too big, complicated and cluttered (and impossible to visualize). I found another stackoverflow post (https://stackoverflow.com/questions/36401411/extracting-information-from-the-decision-rules-in-rpart-package/51680932) over here that shows how to obtain a listing of rules:
library(party)
library(partykit)
party_obj <- as.party.rpart(z.auto, data = TRUE)
decisions <- partykit:::.list.rules.party(party_obj)
cat(paste(decisions, collapse = "\n")
```)
This returns the following list of rules (each line is a rule corresponding to the plot of "z.auto"):
Country %in% c("NA", "Germany", "Korea", "Mexico", "Sweden", "USA") & Weight >= 3167.5
Country %in% c("NA", "Germany", "Korea", "Mexico", "Sweden", "USA") & Weight < 3167.5 Country %in% c("NA", "Japan", "Japan/USA")>
However, from this list, it is not possible to know which rule results in which value of "Reliability". For the time being, I am manually interpreting the tree and manually tracing each rule to the result, but is there a way to add to each line "the corresponding value of reliability"?
e.g. Is it possible to produce something like this?
`Country %in% c("NA", "Germany", "Korea", "Mexico", "Sweden", "USA") & Weight >= 3167.5 then reliability = 3,7,4,0`
(note: I am also not sure why the countries are appearing as "befgh" instead of their actual names.
Thanks
@swaheera have you taken a look at the tidyrules package? The vignette contains an example using rpart and seems to give exactly what you are looking for.
Thanks! I'll need to check this out, too!
From: Brandon Greenwell [email protected] Sent: Wednesday, January 13, 2021 1:08 PM To: bethatkinson/rpart [email protected] Cc: Subscribed [email protected] Subject: [EXTERNAL] Re: [bethatkinson/rpart] Obtaining "Decision Rules" from rpart trees (#25)
@swaheerahttps://github.com/swaheera have you taken a look at the tidyruleshttps://CRAN.R-project.org/package=tidyrules package? The vignette contains an example using rpart and seems to give exactly what you are looking for.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/bethatkinson/rpart/issues/25#issuecomment-759657056, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACWQG56K44W3GKO6OLNHKZ3SZXVUNANCNFSM4WBBXOZQ.
@swaheera have you taken a look at the tidyrules package? The vignette contains an example using rpart and seems to give exactly what you are looking for.
I dont have access to this package due to admin restrictions on my computer (no usb or internet). Can this be done using only with rpart, party, partykit ot base R?
Thank you for your help!
I may have some code that you can use - I know I've done this before.
From: swaheera [email protected] Sent: Wednesday, January 13, 2021 1:13 PM To: bethatkinson/rpart [email protected] Cc: Atkinson, Elizabeth J. (Beth), M.S. [email protected]; Comment [email protected] Subject: [EXTERNAL] Re: [bethatkinson/rpart] Obtaining "Decision Rules" from rpart trees (#25)
@swaheerahttps://github.com/swaheera have you taken a look at the tidyruleshttps://CRAN.R-project.org/package=tidyrules package? The vignette contains an example using rpart and seems to give exactly what you are looking for.
I dont have access to this package due to admin restrictions on my computer (no usb or internet). Can this be done using only with rpart, party, partykit ot base R?
Thank you for your help!
— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/bethatkinson/rpart/issues/25#issuecomment-759659470, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACWQG52KQPNCRZOLWRIWKXTSZXWGJANCNFSM4WBBXOZQ.
@bethatkinson : thank you so much for your reply! (Here is the corresponding stackoverflow question: https://stackoverflow.com/questions/65679523/r-obtaining-rules-from-a-function) . Your help is greatly Appreciated!
Thank you for your reply!
From: Beth Atkinson [email protected] Sent: January 13, 2021 2:16 PM To: bethatkinson/rpart [email protected] Cc: swaheera [email protected]; Mention [email protected] Subject: Re: [bethatkinson/rpart] Obtaining "Decision Rules" from rpart trees (#25)
I may have some code that you can use - I know I've done this before.
From: swaheera [email protected] Sent: Wednesday, January 13, 2021 1:13 PM To: bethatkinson/rpart [email protected] Cc: Atkinson, Elizabeth J. (Beth), M.S. [email protected]; Comment [email protected] Subject: [EXTERNAL] Re: [bethatkinson/rpart] Obtaining "Decision Rules" from rpart trees (#25)
@swaheerahttps://github.com/swaheera have you taken a look at the tidyruleshttps://CRAN.R-project.org/package=tidyrules package? The vignette contains an example using rpart and seems to give exactly what you are looking for.
I dont have access to this package due to admin restrictions on my computer (no usb or internet). Can this be done using only with rpart, party, partykit ot base R?
Thank you for your help!
— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/bethatkinson/rpart/issues/25#issuecomment-759659470, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACWQG52KQPNCRZOLWRIWKXTSZXWGJANCNFSM4WBBXOZQ.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/bethatkinson/rpart/issues/25#issuecomment-759660879, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ASHV6NHZRGQ2FYB4MNTQP7LSZXWQXANCNFSM4WBBXOZQ.