3

I'm using rpart to make a decision tree. For example:

fit <- rpart(Kyphosis ~ Age + Number + Start, data=kyphosis)

How do I read in the formula part from a text file and get it in a format that rpart likes? I've tried:

predictor_variables <- c("Age", "Number", "Start")
rpart_formula <- Kyphosis ~ parse(text=paste(predictor_variables, collapse="+"))
fit <- rpart(rpart_formula, data=kyphosis)

but I get an error:

 invalid type (expression) for variable 'parse(text = paste(predictor_variables, collapse = "+"))'

How can I format rpart_formula so that rpart sees it correctly?

rcs
  • 67,191
  • 22
  • 172
  • 153
Dave
  • 5,436
  • 11
  • 48
  • 74

2 Answers2

5

Use as.formula:

rpart_formula <- as.formula(
    paste("Kyphosis ~ ", 
          paste(predictor_variables, collapse = " + "), 
          sep = ""
    )
)
Tomas
  • 57,621
  • 49
  • 238
  • 373
0

Try simply passing the formula as a character string:

rpart_formula <-paste("Kyphosis ~ ",paste(predictor_variables, collapse="+"))

that should be coerced to a formula by rpart.

Edit

As noted in the comments below, not all functions will do the coercion for you, so you should not rely on this behavior, but in this case rpart most certainly does.

joran
  • 169,992
  • 32
  • 429
  • 468
  • Yes, and it doesn't work - at least in `lm()` function. Try it in `lm()`. – Tomas Sep 21 '11 at 13:53
  • @TomasT. No. Why would I try it in `lm` when the question was about `rpart`? I will concede that `lm` doesn't do the coercion. But if you try, you'll see that I'm right and that `rpart` does. Go ahead...I'll wait! ;) – joran Sep 21 '11 at 14:10
  • I'm too lazy to install that package :-) OK, you are probably right it works for rpart, but in principle... in the robustness principle... he may try it next time with different package and it won't work... :-) – Tomas Sep 21 '11 at 14:16