bethatkinson / rpart

Recursive Partitioning and Regression Trees
43 stars 23 forks source link

Obtaining "Decision Rules" from rpart trees #25

Open swaheera opened 3 years ago

swaheera commented 3 years ago

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
bgreenwell commented 3 years ago

@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.

bethatkinson commented 3 years ago

Thanks! I'll need to check this out, too!


From: Brandon Greenwell notifications@github.com Sent: Wednesday, January 13, 2021 1:08 PM To: bethatkinson/rpart rpart@noreply.github.com Cc: Subscribed subscribed@noreply.github.com 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 commented 3 years ago

@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!

bethatkinson commented 3 years ago

I may have some code that you can use - I know I've done this before.


From: swaheera notifications@github.com Sent: Wednesday, January 13, 2021 1:13 PM To: bethatkinson/rpart rpart@noreply.github.com Cc: Atkinson, Elizabeth J. (Beth), M.S. atkinson@mayo.edu; Comment comment@noreply.github.com 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.

swaheera commented 3 years ago

@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!

swaheera commented 3 years ago

Thank you for your reply!


From: Beth Atkinson notifications@github.com Sent: January 13, 2021 2:16 PM To: bethatkinson/rpart rpart@noreply.github.com Cc: swaheera swaheera92@hotmail.com; Mention mention@noreply.github.com 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 notifications@github.com Sent: Wednesday, January 13, 2021 1:13 PM To: bethatkinson/rpart rpart@noreply.github.com Cc: Atkinson, Elizabeth J. (Beth), M.S. atkinson@mayo.edu; Comment comment@noreply.github.com 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.