CART (2)
CART (2)
###############################################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"]