You are on page 1of 4

Classification Tree Code

# Attach the library


# Loading the library
library(ISLR)
library(tree)

# Attach the data set


attach(Default)

# Creating the Training and Test Data set


set.seed(2)
train=sample(1:10000, 8000)
Default.test=Default[-train,]
default.test=default[-train]

# Fitting the Classification Tree


tree.Default=tree(default~.,Default,subset=train)

# Plotting the tree


plot(tree.Default)
text(tree.Default,pretty=0)
# Deciding the Optimal Size using Cross Validation
set.seed(3)
cv.Default=cv.tree(tree.Default,FUN=prune.misclass)
names(cv.Default)

## [1] "size" "dev" "k" "method"

cv.Default

## $size
## [1] 5 4 1
##
## $dev
## [1] 247 247 270
##
## $k
## [1] -Inf 0.00000 15.66667
##
## $method
## [1] "misclass"
##
## attr(,"class")
## [1] "prune" "tree.sequence"

# Plotting Cross-Validation Error against Size


plot(cv.Default$size,cv.Default$dev,type="b")
# Pruning the Tree
prune.Default=prune.misclass(tree.Default,best=4)
plot(prune.Default)
text(prune.Default,pretty=0)

# Predition for the Test Data Set using Pruned Tree


tree.pred=predict(prune.Default,Default.test,type="class")

# Confusion Matrix
table(tree.pred,default.test)

## default.test
## tree.pred No Yes
## No 1931 40
## Yes 7 22

# ROC Curve for Test Data


tree.pred=predict(prune.Default,Default.test)

# ROC Plot for Test Data


library(pROC)

## Type 'citation("pROC")' for a citation.

##
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
##
## cov, smooth, var

R=roc(default.test,tree.pred[,2])

## Setting levels: control = No, case = Yes

## Setting direction: controls < cases

plot(R,col="blue",legacy.axes = TRUE)

You might also like