-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCART.R
175 lines (122 loc) · 5.01 KB
/
CART.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
library(data.table)
library(caret)
library(mltools)
library(factoextra)
library(plyr)
library(rpart)
library(rpart.plot)
#Read the data
data <- fread("product.csv", header = TRUE, data.table = FALSE)
#Drop id
data <- data[2:198]
#data <- data[,c("L1_S24_F1723", "L1_S24_F1846", "L1_S24_F1695", "L1_S24_F1758", "L2_S26_F3077", "Response")]
#trainSize size
trainSize <- round(nrow(data)*0.7)
#random selection of training and test data
set.seed(123)
training_indices <- sample(seq_len(nrow(data)), size = trainSize)
trainData <- data[training_indices,]
testData <- data[-training_indices,]
#Drop Response column of test Data
Response_test <-testData$Response
testData$Response <- NULL
#Apply PCA
pca_data <- trainData
Response <- trainData$Response
pca_data$Response <- NULL
#Apply PCA first,
pca <- prcomp(pca_data, scale = TRUE)
#Explore output of PCA
fviz_eig(pca)
#Calculate predicted variances
pr_var = (pca$sdev)^2
pro_var_ex = pr_var/sum(pr_var)
#Plot predicted variance for each component
plot(pro_var_ex, xlim=c(0,60), type = "b")
plot(cumsum(pro_var_ex), xlim = c(0,60), ylab = "Cumulated explained variance", xlab = "Principal Components")
cumsum(pro_var_ex)
#create the dataframes with the principal components
trainData$Response <- NULL
trainData <- data.frame(Response = Response, pca$x)
testData <- as.data.frame(predict(pca, newdata = testData))
rm(pca_data)
#Only take 106 features, to cover 99% of explained varaince
trainData <- trainData[,1:107]
testData <- testData[,1:106]
###### Apply Decision Tree
#Transform the Response variable into a vector
trainData$Response <- factor(trainData$Response)
#Define our metrics which should be optimized, here Matthew correlation coefficient
mccSummary <- function (data, lev = NULL, model = NULL){
tp <- as.numeric(sum(data$obs == 1 & data$pred == 1))
tn <- as.numeric(sum(data$obs == 0 & data$pred == 0))
fp <- as.numeric(sum(data$obs == 0 & data$pred == 1))
fn <- as.numeric(sum(data$obs == 1 & data$pred == 0))
numer <- (tp * tn) - (fp * fn)
denom <- ((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)) ^ 0.5
out <- numer/denom
names(out) <-"mcc"
out
}
#Define False negative rate
fnrSummary <- function (data, lev = NULL, model = NULL){
tp <- as.numeric(sum(data$obs == 1 & data$pred == 1))
tn <- as.numeric(sum(data$obs == 0 & data$pred == 0))
fp <- as.numeric(sum(data$obs == 0 & data$pred == 1))
fn <- as.numeric(sum(data$obs == 1 & data$pred == 0))
out <- fn/(tp+tn+fp+fn)
names(out) <-"fnr"
out
}
#For ROC in twoClassSummary level names should not be 0 1
levels(trainData$Response)
#trainData$Response <- mapvalues(trainData$Response, from = c("0", "1"), to = c("level0", "level1"))
#mccSummary only works if levels have name 0 and 1
#trainData$Response <- mapvalues(trainData$Response, from = c("level0", "level1"), to = c("0", "1"))
# Use Caret package with glm method and cross validation on the trainData, we will then evaluate the model with our testData
# define traininControl with 10-fold-cross validation
train_control<- trainControl(method="cv", number=10, summaryFunction = mccSummary, savePredictions = T)
#train_control<- trainControl(method="cv", number=10, summaryFunction = fnrSummary)
#train_control<- trainControl(method="cv", number=10, summaryFunction = twoClassSummary, classProbs = T)
# train the model, define family as binomial for logistic regression
model<- train(Response~., data=trainData, metric = "mcc", trControl=train_control, method="rpart", minsplit=5, maximize = T)
#model<- train(Response~., data=trainData, metric = "fnr", trControl=train_control, method="rpart", maximize = F)
#model<- train(Response~., data=trainData, metric ="ROC", trControl=train_control, method="rpart", maximize = T)
# print cv scores
model
rpart.plot(model$finalModel)
varImp(model)
# Make Predictions and Calculate MCC
predictions <- predict(model, newdata = testData)
predictions <- as.numeric(predictions)
predictions[predictions == 1] <- 0
predictions[predictions == 2] <- 1
table(predictions, Response_test)
mcc(predictions,Response_test)
####### Rpart Without Cross Validation
model <- rpart(formula = Response~., data = trainData, minsplit=20, method = "class")
rpart.plot(model)
#VarImp
varImp(model)
#Make Predictions and clalulate mcc
predictions <- predict(model, newdata = testData, type = "class")
predictions <- as.numeric(predictions)
predictions[predictions == 1] <- 0
predictions[predictions == 2] <- 1
table(predictions, Response_test)
mcc(predictions,Response_test)
####### Rpart with missclassificationcost adjustments
#Define lossMatrix
lossMatrix <- matrix(c(0,6,1,0), nrow = 2)
(t(lossMatrix))
model <- rpart(formula = Response~., data = trainData, method = "class", parms=list(split = "gini", loss = lossMatrix))
rpart.plot(model)
#VarImp
varImp(model)
#Make Predictions and clalulate mcc
predictions <- predict(model, newdata = testData, type = "class")
predictions <- as.numeric(predictions)
predictions[predictions == 1] <- 0
predictions[predictions == 2] <- 1
table(predictions, Response_test)
mcc(predictions,Response_test)