Skip to content
Prev 248455 / 398502 Next

Extracting the terms from an rpart object

Note that all.vars(terms(fit)) only looks at the
formula in the terms object and throws away all
the analysis done by rpart's call to terms(formula,data).
Here is a contrived example of that approach failing:

  > ageThreshold <- 50
  > fit3 <- rpart(Kyphosis=="present" ~ (Age>ageThreshold) + log(Number, base=2) + Start, data=kyphosis)
  > all.vars(terms(fit3))
  [1] "Kyphosis"     "Age"          "ageThreshold" "Number"       "Start"

Looking at the attributes of the terms object
tells you what I think you want:

  > attr(terms(fit3), "response") # 1=>there is a response variable, 0=>no response
  [1] 1
  > as.list(attr(terms(fit3), "variables"))[-1]
  [[1]]
  Kyphosis == "present"

  [[2]]
  Age > ageThreshold

  [[3]]
  log(Number, base = 2)

  [[4]]
  Start

rpart doesn't allow interaction terms (x:y), but if it did
you would want to look at the "factors" attribute to see
which items in the "variables" lists are in each term of
the expanded formula.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com