How to drawing a decision tree?

This is my script for a decision tree in R:

    library(caret)
    library(rpart.plot)
    library(plyr)
    library(dplyr)
    
    data("iris")
    
    names(iris) = tolower(names(iris))
    
    table(iris$species)
    suppressMessages(library(caret))
    
    index = createDataPartition(y=iris$species, p=0.7, list=FALSE)
    
    train = iris[index,]
    test = iris[-index,]
    
    trainctrl <- trainControl(method = "cv", number = 5, verboseIter = FALSE)

dt.model <- train(species~., data=train, method = "rpart", 
                  tuneLength = 10,
                  preProcess = c("center", "scale"),
                  trControl = trainctrl,
                  metric="Kappa")

dt.predict <-predict(dt.model, test)
confusionMatrix(dt.predict, test$species)

How can I make the tree drawing with nodes?

rpart.plot(dt.model$finalModel)

But I have this error:

Error in plot.new() : figure margins too large

is there another way to know which characteristics the model considers more important?

You can use caret varImp function to get variables importance.
Anyway the following code runs smoothly on my PC

library(caret)
library(rpart.plot)
library(plyr)
library(dplyr)

data("iris")

names(iris) = tolower(names(iris))

table(iris$species)
# suppressMessages(library(caret))

index = createDataPartition(y=iris$species, p=0.7, list=FALSE)

train = iris[index,]
test = iris[-index,]

trainctrl <- trainControl(method = "cv", number = 5, verboseIter = FALSE)

dt.model <- train(species~., data=train, method = "rpart", 
                  tuneLength = 11,
                  preProcess = c("center", "scale"),
                  trControl = trainctrl,
                  metric="Kappa")

dt.predict <-predict(dt.model, test)
dt.model
confusionMatrix(dt.predict, test$species)

rpart.plot(dt.model$finalModel)

varImp(dt.model)

Ok, now works thanks!

What is the meaning of Overall in this table?

            
> varImp(dt.model)
rpart variable importance
              Overall
petal.length  100.00
petal.width   100.00
sepal.length   37.59
sepal.width     0.00

It is not class specific, for details have a look at https://forum.posit.co/t/varimp-for-rpart-when-using/18584
HTH

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.