0% found this document useful (0 votes)
3 views

CART (2)

Uploaded by

ptitodi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CART (2)

Uploaded by

ptitodi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

##############################CART

# Install and load required packages


install.packages("rpart")
install.packages("rpart.plot")
library(rpart)
library(rpart.plot)

# Load the data


data(mtcars)

# Explore the data


summary(mtcars)

# Build the CART model


#We'll predict the mpg (miles per gallon) based on other features.
cart_model <- rpart(mpg ~ ., data = mtcars, method = "anova")

# Visualize the tree


rpart.plot(cart_model)

# Evaluate the model


#Check the complexity parameter table to understand the pruning process.
printcp(cart_model)

# Prune the tree to avoid overfitting


pruned_model <- prune(cart_model, cp =
cart_model$cptable[which.min(cart_model$cptable[,"xerror"]),"CP"])
rpart.plot(pruned_model)

###############################################CART22222222222
###Example 1: Building a Regression Tree in R
#Step 1: Load the necessary packages
library(ISLR) #contains Hitters dataset
library(rpart) #for fitting decision trees
library(rpart.plot) #for plotting decision trees
#Step 2: Build the initial regression tree
head(Hitters)
#build the initial tree
tree <- rpart(Salary ~ Years + HmRun, data=Hitters,
control=rpart.control(cp=.0001))

#view results
printcp(tree)
#Step 3: Prune the tree
#identify best cp value to use
best <- tree$cptable[which.min(tree$cptable[,"xerror"]),"CP"]

#produce a pruned tree based on the best cp value


pruned_tree <- prune(tree, cp=best)

#plot the pruned tree


prp(pruned_tree,
faclen=0, #use full names for factor labels
extra=1, #display number of obs. for each terminal node
roundint=F, #don't round to integers in output
digits=5) #display 5 decimal places in output
#Step 4: Use the tree to make predictions
#define new player
new <- data.frame(Years=7, HmRun=4)

#use pruned tree to predict salary of this player


predict(pruned_tree, newdata=new)

###Example 2: Building a Classification Tree in R


#Step 1: Load the necessary packages
library(rpart) #for fitting decision trees
library(rpart.plot) #for plotting decision trees
#Step 2: Build the initial classification tree
#build the initial tree
tree <- rpart(survived~pclass+sex+age, data=ptitanic,
control=rpart.control(cp=.0001))
head(ptitanic)
#view results
printcp(tree)
#Step 3: Prune the tree
#identify best cp value to use
best <- tree$cptable[which.min(tree$cptable[,"xerror"]),"CP"]

#produce a pruned tree based on the best cp value


pruned_tree <- prune(tree, cp=best)

#plot the pruned tree


prp(pruned_tree,
faclen=0, #use full names for factor labels
extra=1, #display number of obs. for each terminal node
roundint=F, #don't round to integers in output
digits=5) #display 5 decimal places in output

You might also like