R Code - Session 11
R Code - Session 11
set.seed(2)
train <- sample(1:nrow(Carseats), nrow(Carseats)/2)
test <- -train
training <- Carseats[train,]
testing <- Carseats[test,]
test_high <- testing$high
#Plot tree
plot(mtree)
text(mtree, preety=0)
############################
########Pruning#############
############################
set.seed(3)
cv_tree <- cv.tree(mtree,FUN=prune.misclass)
names(cv_tree)
plot(cv_tree$size, cv_tree$dev,type="b")
table(cv_tree$size, cv_tree$dev)
set.seed(2)
train <- sample(1:nrow(Carseats), nrow(Carseats)/2)
test <- -train
training <- Carseats[train,]
testing <- Carseats[test,]
test_high <- testing$high
library(rpart)
mtree <- rpart(high~., data = training, method="class",
control = rpart.control(minsplit = 20, minbucket = 7,
maxdepth = 10, usesurrogate = 2, xval =10 ))
#Beautify tree
#view1
prp(mtree, faclen = 0, cex = 0.8, extra = 1)
############################
########Pruning#############
############################
# Advanced Plot
prp(pruned, main="Beautiful Tree",
extra=106,
nn=TRUE,
fallen.leaves=TRUE,
branch=.5,
faclen=0,
trace=1,
shadow.col="gray",
branch.lty=3,
split.cex=1.2,
split.prefix="is ",
split.suffix="?",
split.box.col="lightgray",
split.border.col="darkgray",
split.round=.5)