R(27), Decision Tree

Published: by Creative Commons Licence

Click here to know more about Decision Tree.

We need to use the ctree() function in package named "party".

install.packages("party")

About the function ctree():

ctree(formula, data)
  • formula: a symbolic description of the model to be fit. Note that symbols like : and - will not work and the tree will make use of all variables listed on the rhs of formula.
  • data: a data frame containing the variables in the model.

Example

# Load the party package. It will automatically load other dependent packages.
library(party)

# Print some records from data set readingSkills.
print(head(readingSkills))
# Load the party package. It will automatically load other dependent packages.
library(party)

# Create the input data frame.
input.dat <- readingSkills[c(1:105),]

# Give the chart file a name.
png(file = "decision_tree.png")

# Create the tree.
output.tree <- ctree(
    nativeSpeaker ~ age + shoeSize + score,
    data = input.dat)

# Plot the tree.
plot(output.tree)

# Save the file.
dev.off()

decision tree