Search code examples
rregressionrpart

Regression tree in R


I am having trouble making a regression tree in R. I have a data frame with 17 attributes

library(rpart)
rt.model <- rpart(razlika ~ ., learn)

I get an error:

Error in `[.data.frame`(frame, predictors) : undefined columns selected

Seems weird because I did something like that with a very simillar database. You can dowload the dataframe on http://uploading.com/files/de8a966d/exa.Rda/ - then load with

load("exa.Rda")

Solution

  • The problem is not, I believe, that you have a matrix rather than a data frame. When I download and then load you data set, I get a data frame, not a matrix.

    The problem is that you have bad characters in the column names. Use gsub to remove the characters "-", " ", "(" and ")" from the column names. Or you can simply redefine the column names yourself entirely using colnames.

    Or do as ulvund does and simply call data.frame, which forces R to do the column name cleaning for you, by default.

    When I do this, rpart runs just fine.