[R] Rpart description of tree groups

David Huffer David.Huffer at csosa.gov
Tue Jun 17 17:06:34 CEST 2008


I'm making a few functions to generate latex files describing
rpart objects that are then \input-ed into a larger document. So
far, the functions I have generate paragraphs containing
enumerations of the predictors in pruned trees and the number of
formed groups.

Its easy enough to recover these. For instance,

 R> print ( tree )
 n= 878

 node), split, n, loss, yval, (yprob)
       * denotes terminal node

  1) root 878 110 Absent (0.8747153 0.1252847)
    2) P3XMAR2=No 845  96 Absent (0.8863905 0.1136095) *
    3) P3XMAR2=Yes 33  14 Absent (0.5757576 0.4242424)
      6) PADV=Yes 7   0 Absent (1.0000000 0.0000000) *
      7) PADV=No 26  12 Present (0.4615385 0.5384615)
       14) ACPS=No 12   5 Absent (0.5833333 0.4166667) *
       15) ACPS=Yes 14   5 Present (0.3571429 0.6428571) *
 R> varsInTree <- as.vector (
 +   tree $ frame $ var [ tree $ frame $ var %nin% "<leaf>"]      #
%nin% from Hmisc
 + )
 R> varsInTree
 [1] "P3XMAR2" "PADV"    "ACPS"
 R> nGroupsInTree <- sum ( tree $ frame $ var  == "<leaf>" )
 R> nGroupsInTree
 [1] 4
 R>

What i havent been successful at so far is generating an
enumerated list of the groups formed. I wanted to do something
similar to the print.rpart but instead of enumerating nested
lists I wanted a description of the terminal nodes. For
instance, from the tree shown above i wanted to generate
something like

 The resultant model separated sampled respondents into 4 groups.
 These groups (and predicted values) included respondents having
   \begin{inparaenum}[(a)]
    \item P3XMAR2=No (Absent);
    \item P3XMAR2=Yes and PADV=Yes (Absent);
    \item P3XMAR2=Yes and PADV=No and ACPS=No (Absent); and
    \item P3XMAR2=Yes and PADV=No and ACPS=Yes (Present);
   \end{inparaenum}

So far, I've just generated placeholders for the \items and then
i edit them by hand.  Does anyone have advice on recovering the
formed groups?

--
 David



More information about the R-help mailing list