library(rpart) # R package for decision Tree
library(caret) # R package for decision Tree
## Loading required package: lattice
## Loading required package: ggplot2
setwd("D:/Users/gkokate/Desktop/Markdown")
build <- read.csv(file = "Build .csv",sep = ",", header = TRUE)
test1 <- read.csv(file = "test.csv", sep = "," , header = TRUE)
# Sample observations
head(build)
## gponOntAniOpInfoOpticalSignalLevel gponOntAniOpInfoTxOpticalSignalLevel
## 1 -7711 1142
## 2 -7703 1288
## 3 -7703 1081
## 4 -7703 1207
## 5 -7688 1276
## 6 -7688 1282
## gponOntOltsideOpInfoRxOpticalSignalLevel X15MinDnFwdByteCounter
## 1 -171 8.888281
## 2 -170 9.544178
## 3 -170 8.915710
## 4 -171 7.555582
## 5 -170 7.475159
## 6 -169 7.236687
## X15MinUpFwdByteCounter bponOntOpInfoDistance ifOperStatus
## 1 7.245204 38 up
## 2 7.764763 38 up
## 3 7.648214 38 up
## 4 6.976296 38 up
## 5 6.646812 38 up
## 6 6.449259 38 up
#dependent variable as a factor (categorical)
build$ifOperStatus <- as.factor(build$ifOperStatus)
# Split data into training (70%) and validation (30%)
split <- sample(nrow(build),floor(nrow(build)*0.7))
train <- build[split,]
val <- build[-split,]
# Decision Tree Model
mtree <- rpart(ifOperStatus~ .,data=train,method = "class",parms = list(prior = c(0.3, 0.7)))
#parms = list(prior = c(0.5, 0.5)
#Confusion matrix
rpartpred <- predict(mtree,val,type="class")
confusionMatrix(rpartpred,val$ifOperStatus)
## Confusion Matrix and Statistics
##
## Reference
## Prediction down up
## down 185 112
## up 77 4190
##
## Accuracy : 0.9586
## 95% CI : (0.9524, 0.9642)
## No Information Rate : 0.9426
## P-Value [Acc > NIR] : 6.789e-07
##
## Kappa : 0.6399
## Mcnemar's Test P-Value : 0.01339
##
## Sensitivity : 0.70611
## Specificity : 0.97397
## Pos Pred Value : 0.62290
## Neg Pred Value : 0.98195
## Prevalence : 0.05741
## Detection Rate : 0.04053
## Detection Prevalence : 0.06507
## Balanced Accuracy : 0.84004
##
## 'Positive' Class : down
##
#Plot tree
#plot(mtree)
#Lable on Decision Tree
#text(mtree)
library(rattle)
## Rattle: A free graphical interface for data mining with R.
## Version 4.1.0 Copyright (c) 2006-2015 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library(rpart.plot)
library(RColorBrewer)
#plot
#prp(mtree, faclen = 0, cex = 0.8, extra = 1)
tot_count <- function(x, labs, digits, varlen)
{paste(labs, "\n\nn =", x$frame$n)}
# Decision Tree
prp(mtree, faclen = 0, cex = 0.8, node.fun=tot_count)

printcp(mtree)
##
## Classification tree:
## rpart(formula = ifOperStatus ~ ., data = train, method = "class",
## parms = list(prior = c(0.3, 0.7)))
##
## Variables actually used in tree construction:
## [1] gponOntOltsideOpInfoRxOpticalSignalLevel
##
## Root node error: 3194.4/10648 = 0.3
##
## n= 10648
##
## CP nsplit rel error xerror xstd
## 1 0.6231 0 1.0000 1.0000 0.037428
## 2 0.0100 1 0.3769 0.3769 0.021519
bestcp <- mtree$cptable[which.min(mtree$cptable[,"xerror"]),"CP"]
#Pruning & classification matrix of Pruning
pruned <- prune(mtree, cp = bestcp)
#prp(pruned, faclen = 0, cex = 0.8, extra = 1)
predictions <- predict(pruned, val, type="class")
confusionMatrix(predictions,val$ifOperStatus)
## Confusion Matrix and Statistics
##
## Reference
## Prediction down up
## down 185 112
## up 77 4190
##
## Accuracy : 0.9586
## 95% CI : (0.9524, 0.9642)
## No Information Rate : 0.9426
## P-Value [Acc > NIR] : 6.789e-07
##
## Kappa : 0.6399
## Mcnemar's Test P-Value : 0.01339
##
## Sensitivity : 0.70611
## Specificity : 0.97397
## Pos Pred Value : 0.62290
## Neg Pred Value : 0.98195
## Prevalence : 0.05741
## Detection Rate : 0.04053
## Detection Prevalence : 0.06507
## Balanced Accuracy : 0.84004
##
## 'Positive' Class : down
##
#Scoring
library(ROCR)
## Loading required package: gplots
##
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
##
## lowess
val1 = predict(pruned, val, type = "prob")
pred_val <-prediction(val1[,2],val$ifOperStatus)
perf_val <- performance(pred_val,"auc")
perf_val
## An object of class "performance"
## Slot "x.name":
## [1] "None"
##
## Slot "y.name":
## [1] "Area under the ROC curve"
##
## Slot "alpha.name":
## [1] "none"
##
## Slot "x.values":
## list()
##
## Slot "y.values":
## [[1]]
## [1] 0.8400362
##
##
## Slot "alpha.values":
## list()
plot(performance(pred_val, measure="lift", x.measure="rpp"), colorize=TRUE)

# Calculating True Positive and False Positive Rate
perf_val <- performance(pred_val, "tpr", "fpr")
#Plot the ROC curve
plot(perf_val, col = "green", lwd = 1.5)

#Calculating KS statistics
ks1.tree <- max(attr(perf_val, "y.values")[[1]] - (attr(perf_val, "x.values")[[1]]))
ks1.tree
## [1] 0.6800725
# Cross Validation Method1
library(ROSE)
## Loaded ROSE 0.0-3
ROSE.BOOT <- ROSE.eval(ifOperStatus ~ ., data = train, learner = rpart,method.assess = "BOOT", extr.pred = function(obj)obj[,2], seed = 1)
# Cross Validation Method2
library(caret)
tc <- trainControl("cv",10)
rpart.grid <- expand.grid(.cp=0.2)
(train.rpart <- train(ifOperStatus ~., data= train, method="rpart",trControl=tc,tuneGrid=rpart.grid))
## CART
##
## 10648 samples
## 6 predictor
## 2 classes: 'down', 'up'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 9583, 9583, 9583, 9583, 9584, 9583, ...
## Resampling results
##
## Accuracy Kappa Accuracy SD Kappa SD
## 0.9529486 0.6249707 0.008441247 0.05945686
##
## Tuning parameter 'cp' was held constant at a value of 0.2
##
# Model Perfomance on test data
ptest <- predict(mtree, test1)
answers <- as.vector(ptest)
pml_write_files = function(x) {
n = length(x)
for (i in 1:n) {
filename = paste0("problem_id_", i, ".txt")
write.table(x[i], file = filename, quote = FALSE, row.names = FALSE,
col.names = FALSE)
}
}
pml_write_files(answers)
# Prediction of probabilites new data
ptest
## down up
## 1 0.119708 0.880292
## 2 0.119708 0.880292
## 3 0.119708 0.880292
## 4 0.119708 0.880292
## 5 0.119708 0.880292
## 6 0.119708 0.880292
## 7 0.119708 0.880292
## 8 0.119708 0.880292
## 9 0.119708 0.880292
## 10 0.119708 0.880292
## 11 0.119708 0.880292
## 12 0.119708 0.880292
## 13 0.119708 0.880292
## 14 0.119708 0.880292
## 15 0.119708 0.880292
## 16 0.119708 0.880292
## 17 0.119708 0.880292
## 18 0.119708 0.880292
## 19 0.119708 0.880292
## 20 0.119708 0.880292
## 21 0.119708 0.880292
## 22 0.119708 0.880292
## 23 0.119708 0.880292
## 24 0.119708 0.880292
## 25 0.119708 0.880292
## 26 0.119708 0.880292
## 27 0.119708 0.880292
## 28 0.119708 0.880292
## 29 0.119708 0.880292
## 30 0.119708 0.880292
## 31 0.119708 0.880292
## 32 0.119708 0.880292
## 33 0.119708 0.880292
## 34 0.119708 0.880292
## 35 0.119708 0.880292
## 36 0.119708 0.880292
## 37 0.119708 0.880292
## 38 0.119708 0.880292
## 39 0.119708 0.880292
## 40 0.119708 0.880292
## 41 0.119708 0.880292
## 42 0.119708 0.880292
## 43 0.119708 0.880292
## 44 0.119708 0.880292
## 45 0.119708 0.880292
## 46 0.119708 0.880292
## 47 0.119708 0.880292
## 48 0.119708 0.880292
## 49 0.119708 0.880292
## 50 0.119708 0.880292
## 51 0.119708 0.880292
## 52 0.119708 0.880292
## 53 0.119708 0.880292
## 54 0.119708 0.880292
## 55 0.119708 0.880292
## 56 0.119708 0.880292
## 57 0.119708 0.880292
## 58 0.119708 0.880292
## 59 0.119708 0.880292
## 60 0.119708 0.880292
## 61 0.119708 0.880292
## 62 0.119708 0.880292
## 63 0.119708 0.880292
## 64 0.119708 0.880292
## 65 0.119708 0.880292
## 66 0.119708 0.880292
## 67 0.119708 0.880292
## 68 0.119708 0.880292
## 69 0.119708 0.880292
## 70 0.119708 0.880292
## 71 0.119708 0.880292
## 72 0.119708 0.880292
## 73 0.119708 0.880292
## 74 0.119708 0.880292
## 75 0.119708 0.880292
## 76 0.119708 0.880292
## 77 0.119708 0.880292
## 78 0.119708 0.880292
## 79 0.119708 0.880292
## 80 0.119708 0.880292
## 81 0.119708 0.880292
## 82 0.119708 0.880292
## 83 0.119708 0.880292
## 84 0.119708 0.880292
## 85 0.119708 0.880292
## 86 0.119708 0.880292
## 87 0.119708 0.880292
## 88 0.119708 0.880292
## 89 0.119708 0.880292
## 90 0.119708 0.880292
## 91 0.119708 0.880292
## 92 0.119708 0.880292
## 93 0.119708 0.880292
## 94 0.119708 0.880292
## 95 0.119708 0.880292
## 96 0.119708 0.880292
## 97 0.119708 0.880292
## 98 0.119708 0.880292
## 99 0.119708 0.880292
## 100 0.119708 0.880292
## 101 0.119708 0.880292
## 102 0.119708 0.880292
## 103 0.119708 0.880292
## 104 0.119708 0.880292
## 105 0.119708 0.880292
## 106 0.119708 0.880292
## 107 0.119708 0.880292
## 108 0.119708 0.880292
## 109 0.119708 0.880292
## 110 0.119708 0.880292
## 111 0.119708 0.880292
## 112 0.119708 0.880292
## 113 0.119708 0.880292
## 114 0.119708 0.880292
## 115 0.119708 0.880292
## 116 0.119708 0.880292
## 117 0.119708 0.880292
## 118 0.119708 0.880292
## 119 0.119708 0.880292
## 120 0.119708 0.880292