cs0815
cs0815

Reputation: 17388

get string/character representation of rpart regression tree

I am using code like this:

library(datasets)
library(rpart)
library(caret)

options(warn=-1)
set.seed(42)


x <- subset(iris, select=-c(Species, Sepal.Length))

fitControl <- trainControl(
     method = "repeatedcv"
     , number = 10
     , repeats = 10
 )

fit_data <- caret::train(
     x = x
     , y = iris$Sepal.Length
     , method = 'rpart'
     , trControl = fitControl
     #, control=rpart.control(minsplit=3, minbucket=1, cp=0.001)
     #, metric = "ROC"
     #, tuneLength = 20
    , control = rpart.control(maxdepth = 3) #  minbucket=20
)

model <- fit_data$finalModel
model

The last line:

model

prints the model as string/character on the screen:

n= 150 

node), split, n, deviance, yval
      * denotes terminal node

1) root 150 102.1683000 5.843333  
  2) Petal.Length< 4.25 73  13.1391800 5.179452 *
  3) Petal.Length>=4.25 77  26.3527300 6.472727  
    6) Petal.Length< 6.05 68  13.4923500 6.326471 *
    7) Petal.Length>=6.05 9   0.4155556 7.577778 *

Is there a way to get the actual string/character representation explicitly? I tried something like this:

 df <- data.frame(test = as.character(model))

to write the model as string into a dataframe. It prints too much ...

Upvotes: 1

Views: 150

Answers (2)

cs0815
cs0815

Reputation: 17388

The following works for me:

model_text <- capture.output(print(model))
OutputDataSet <- data.frame(model_text = model_text, stringsAsFactors=FALSE)

Upvotes: 0

NelsonGon
NelsonGon

Reputation: 13319

Not exactly sure about the expected output but we can save the model as a character as follows:

model <- quote(fit_data$finalModel)

We can then simply call it whenever needed as follows:

eval(model)

Upvotes: 2

Related Questions