Rpart decision tree

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Rpart decision tree

tudor
Dear useRs:

I try to plot an rpart object but cannot get a nice tree structure plot.  I am using plot.rpart and text.rpart (please see below) but the branches that connect the nodes overlap the text in the ellipses and rectangles.  Is there a way to get a clean nice tree plot (as in the Rpart Mayo report)?  I work under Windows and use R2.11.1 with rpart version 3.1-46.

Thank you.

Tudor

...
plot(cart, uniform=TRUE, branch=0.25, compress=TRUE, margin=0.25)
text(cart, use.n=TRUE, all=TRUE, fancy=TRUE, fheight=0.95)
...
Reply | Threaded
Open this post in threaded view
|

Re: Rpart decision tree

Tal Galili
Hi Tudor,
What type of tree did you create? (classification or regression?)
Could you give an explanation as to what is not looking nice?



----------------Contact
Details:-------------------------------------------------------
Contact me: [hidden email] |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------




On Fri, Apr 8, 2011 at 2:03 PM, tudor <[hidden email]> wrote:

> Dear useRs:
>
> I try to plot an rpart object but cannot get a nice tree structure plot.  I
> am using plot.rpart and text.rpart (please see below) but the branches that
> connect the nodes overlap the text in the ellipses and rectangles.  Is
> there
> a way to get a clean nice tree plot (as in the Rpart Mayo report)?  I work
> under Windows and use R2.11.1 with rpart version 3.1-46.
>
> Thank you.
>
> Tudor
>
> ...
> plot(cart, uniform=TRUE, branch=0.25, compress=TRUE, margin=0.25)
> text(cart, use.n=TRUE, all=TRUE, fancy=TRUE, fheight=0.95)
> ...
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Rpart-decision-tree-tp3435888p3435888.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Rpart decision tree

Achim Zeileis-4
In reply to this post by tudor
On Fri, 8 Apr 2011, tudor wrote:

> Dear useRs:
>
> I try to plot an rpart object but cannot get a nice tree structure plot.  I
> am using plot.rpart and text.rpart (please see below) but the branches that
> connect the nodes overlap the text in the ellipses and rectangles.  Is there
> a way to get a clean nice tree plot (as in the Rpart Mayo report)?  I work
> under Windows and use R2.11.1 with rpart version 3.1-46.

You could check out whether you like the plots generated in the "party"
package better. See example("ctree", package = "party").

If you like the plots but don't want to use ctree() instead of rpart():
Then you could install the "partykit" package from R-Forge and then do
plot(as.party(...)) for your "rpart" object.

hth,
Z

> Thank you.
>
> Tudor
>
> ...
> plot(cart, uniform=TRUE, branch=0.25, compress=TRUE, margin=0.25)
> text(cart, use.n=TRUE, all=TRUE, fancy=TRUE, fheight=0.95)
> ...
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Rpart-decision-tree-tp3435888p3435888.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Rpart decision tree

tudor
In reply to this post by Tal Galili
Hi Tal:

Thanks for your quick reply.

I tried to create a classification tree.  

The problem is that the branches overlap the text in the ellipses (inner nodes) and rectangles (terminal nodes) - At times this makes it difficult for the reader to correctly process the results.  One way to overcome this behavior would be to force the node connectors to show only outside of ellipses and rectangles - so far, however, I could not implement this behavior.

Tudor
Reply | Threaded
Open this post in threaded view
|

Re: Rpart decision tree

tudor
In reply to this post by Achim Zeileis-4
Hi Achim:

I will give it a try and let you know how it goes.

Thanks.

Tudor
Reply | Threaded
Open this post in threaded view
|

Re: Rpart decision tree

tudor
Dear Tal and Achim:

Thank you both for your assistance.

I followed Achim's suggestions and got what I needed on a Windows machine with R2.12.2 installed.  As I needed to run the task on a Windows machine with R2.11.1 I used the package rpart.plot (by Stephen Milborrow) to create my charts.

Best regards,

Tudor


Reply | Threaded
Open this post in threaded view
|

Re: Rpart decision tree

dlofaro
In reply to this post by Achim Zeileis-4
Dear R-Users,
I'm a R beginner and I have a similar problem:
I fitted a SurvivalTree with rpart and I'm tryng to plot with the partykit package by the funcion as.party but it sends me this error:

> plot(as.party(rpartSurv))
Error in plot.constparty(as.party(rpartSurv)) :
  node_surv not yet implemented

In effect after the library command the node_surv is not reported:

> library(partykit)

Attaching package: ‘partykit’
The following object(s) are masked from ‘package:party’:
    ctree, ctree_control, edge_simple, node_barplot, node_boxplot, node_inner, node_terminal

How can I implement the node_surv plot?
Reply | Threaded
Open this post in threaded view
|

Re: Rpart decision tree

Achim Zeileis-4
On Wed, 15 Feb 2012, dlofaro wrote:

> Dear R-Users,
> I'm a R beginner and I have a similar problem:
> I fitted a SurvivalTree with rpart and I'm tryng to plot with the partykit
> package by the funcion as.party but it sends me this error:
>
>> plot(as.party(rpartSurv))
> Error in plot.constparty(as.party(rpartSurv)) :
>  node_surv not yet implemented/
>
> In effect after the library command the node_surv is not reported:
>
>> library(partykit)
>
> Attaching package: ?partykit?
> The following object(s) are masked from ?package:party?:
>    ctree, ctree_control, edge_simple, node_barplot, node_boxplot,
> node_inner, node_terminal
>
> How can I implement the node_surv plot?

One needs to adapt the old party:::node_surv code to the new
infrastructure in "partykit". We haven't done so yet, but I put together a
quick prototype for you. If you source the code below (including setting
the "grapcon_generator" class), then the following should work:
plot(as.party(rpartSurv), terminal_panel = node_surv)

Hope that helps,
Z

node_surv <- function(obj,
       ylines = 2,
       id = TRUE, ...)
{
     ## extract response
     y <- obj$fitted[["(response)"]]
     stopifnot(inherits(y, "Surv"))
     stopifnot(require("survival"))

     ## helper functions
     mysurvfit <- function(y, weights, ...) structure(
         survival:::survfitKM(x = gl(1, NROW(y)), y = y, casewt = weights, ...),
  class = "survfit")

     dostep <- function(x, y) {
         ### create a step function based on x, y coordinates
         ### modified from `survival:print.survfit'
         if (is.na(x[1] + y[1])) {
             x <- x[-1]
             y <- y[-1]
         }
         n <- length(x)
         if (n > 2) {
             # replace verbose horizonal sequences like
             # (1, .2), (1.4, .2), (1.8, .2), (2.3, .2), (2.9, .2), (3, .1)
             # with (1, .2), (3, .1).  They are slow, and can smear the
             # looks of the line type.
             dupy <- c(TRUE, diff(y[-n]) !=0, TRUE)
             n2 <- sum(dupy)

             #create a step function
             xrep <- rep(x[dupy], c(1, rep(2, n2-1)))
             yrep <- rep(y[dupy], c(rep(2, n2-1), 1))
             RET <- list(x = xrep, y = yrep)
         } else {
             if (n == 1) {
                 RET <- list(x = x, y = y)
             } else {
                 RET <- list(x = x[c(1,2,2)], y = y[c(1,1,2)])
             }
         }
         return(RET)
     }

     ### panel function for Kaplan-Meier curves in nodes
     rval <- function(node) {

         ## extract data
  nid <- id_node(node)
  dat <- data_party(obj, nid)
  yn <- dat[["(response)"]]
  wn <- dat[["(weights)"]]
  if(is.null(wn)) wn <- rep(1, length(yn))

         ## get Kaplan-Meier curver in node
         km <- mysurvfit(yn, weights = wn, ...)
         a <- dostep(km$time, km$surv)

         ## set up plot
         yscale <- c(0, 1)
         xscale <- c(0, max(y[,1]))

         top_vp <- viewport(layout = grid.layout(nrow = 2, ncol = 3,
                            widths = unit(c(ylines, 1, 1),
                                          c("lines", "null", "lines")),
                            heights = unit(c(1, 1), c("lines", "null"))),
                            width = unit(1, "npc"),
                            height = unit(1, "npc") - unit(2, "lines"),
    name = paste("node_surv", nid, sep = ""))

         pushViewport(top_vp)
         grid.rect(gp = gpar(fill = "white", col = 0))

         ## main title
         top <- viewport(layout.pos.col=2, layout.pos.row=1)
         pushViewport(top)
  mainlab <- paste(ifelse(id, paste("Node", nid, "(n = "), "n = "),
                  sum(wn), ifelse(id, ")", ""), sep = "")
         grid.text(mainlab)
         popViewport()

         plot <- viewport(layout.pos.col=2, layout.pos.row=2,
                          xscale=xscale, yscale=yscale,
  name = paste("node_surv", nid, "plot",
                          sep = ""))

         pushViewport(plot)
         grid.lines(a$x/max(a$x), a$y)
         grid.xaxis()
         grid.yaxis()
         grid.rect(gp = gpar(fill = "transparent"))
         upViewport(2)
     }

     return(rval)
}
class(node_surv) <- "grapcon_generator"

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.