-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.Rmd
334 lines (230 loc) · 8.86 KB
/
project.Rmd
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
---
title: "IE5374Project"
author: "Dhaval Jariwala"
date: "2022-12-06"
output: html_document
---
## IE5374 PROJECT
## BACKORDER PREDICTION
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(tidyverse)
library(dplyr)
library(ROSE)
library(ROCR)
library(caret)
library(MASS)
library(gridExtra)
trainData <- read.csv("BackOrder_train.csv", stringsAsFactors = T)[,-1]
testData <- read.csv("BackOrder_test.csv", stringsAsFactors = T)[,-1]
data <- rbind(trainData,testData)
```
## CHECK THE DATASETS
```{r}
#TRAINING DATA
head(trainData)
# Check dimensions of the dataset
dim(trainData)
# check the datatype of each column
sapply(trainData,class)
# summary of all columns in the dataset
summary(trainData)
```
```{r}
# TESTING DATA
head(testData)
# Check dimensions of the dataset
dim(testData)
# check the datatype of each column
sapply(testData,class)
# summary of all columns in the dataset
summary(testData)
```
## EDA
```{r}
ggplot(data = trainData, aes(x = sales_3_month, y = forecast_3_month)) + geom_point(aes(color = factor(went_on_backorder)))
ggplot(data = trainData+ aes(x = sales_6_month, y = forecast_6_month)) + geom_point(aes(color = factor(went_on_backorder)))
ggplot(data = trainData ,aes(x = sales_9_month, y = forecast_9_month)) + geom_point(aes(color = factor(went_on_backorder)))
ggplot(data = trainData, aes(x = min_bank, y = forecast_3_month)) + geom_point(aes(color = factor(went_on_backorder)))
ggplot(data = trainData, aes(x = min_bank, y = forecast_6_month)) + geom_point(aes(color = factor(went_on_backorder)))
ggplot(data = trainData, aes(x = min_bank, y = forecast_9_month)) + geom_point(aes(color = factor(went_on_backorder)))
ggplot(data=PrductsData, aes(x=went_on_backorder, y=national_inv, colour = lead_time, size = in_transit_qty)) +
geom_point() +
xlab("went_on_backorder")+ylab("national_inv") +
ggtitle("went_on_backorder Vs. national_inv")
```
## DATA CLEANING
```{r}
# TRAINING DATA
#checking the outcome variable for mislabled entries
rawTrainData = trainData
p1 = ggplot(rawTrainData, aes(went_on_backorder)) + geom_bar() + ggtitle("Before Cleaning")
# from the summary it is evident that there is one row which is incorrectly entered in the dataset, it has either missing or null values
trainData <- trainData %>% filter(went_on_backorder != "")
# remove the rows with all NA
trainData <- trainData[apply(trainData, 1, function(y) !all(is.na(y))),]
# remove the Cols with all NA
trainData <- trainData[sapply(trainData, function(x) !all(is.na(x)))]
# drop unused levels induced by null row
trainData <- droplevels(trainData)
dim(trainData)
summary(trainData)
p2 = ggplot(trainData, aes(went_on_backorder)) + geom_bar() + ggtitle("After Cleaning")
grid.arrange(p1, p2, ncol = 2)
# rows and cols with all NA values and un-used levels are removed from the data set
```
```{r}
# TESTING DATA
#checking the outcome variable for mislabled entries
rawTestData = testData
p1 = ggplot(rawTestData, aes(went_on_backorder)) + geom_bar() + ggtitle("Before Cleaning")
# from the summary it is evident that there is one row which is incorrectly entered in the dataset, it has either missing or null values
testData <- testData %>% filter(went_on_backorder != "")
# remove the rows with all NA
testData <- testData[apply(testData, 1, function(y) !all(is.na(y))),]
# remove the Cols with all NA
testData <- testData[sapply(testData, function(x) !all(is.na(x)))]
# drop unused levels induced by null row
testData <- droplevels(testData)
dim(testData)
summary(testData)
p2 = ggplot(testData, aes(went_on_backorder)) + geom_bar() + ggtitle("After Cleaning")
grid.arrange(p1,p2, ncol = 2)
# rows and cols with all NA values and unsed levels are removed from the data set
```
## Outlier Detection
```{r}
## TRAINING DATA
continuousVariables <- select_if(trainData,is.numeric)
#continuousVariables <- as.data.frame(colnames(continuousVariables))
#colnames(continuousVariables) <- c("names")
continuousVariables <- colnames(continuousVariables)
continuousVariables
library(robustHD)
z = trainData
z[,continuousVariables] <- sapply(z[,continuousVariables], function(x) winsorize(x, threshold =1.5, method = "zscore", robust = TRUE))
#par(mfrow=c(1,2))
p1 = boxplot(trainData[,continuousVariables], xaxt = "n")
tick <- seq_along(p1$names)
axis(1, at = tick, labels = FALSE)
text(tick, par("usr")[3] - 0.45, p1$names, srt = 45, xpd = TRUE)
p2 = boxplot(z[,continuousVariables], xaxt = "n")
tick <- seq_along(p2$names)
axis(1, at = tick, labels = FALSE)
text(tick, par("usr")[3] - 0.45, p2$names, srt = 45, xpd = TRUE)
c = cor(trainData[,continuousVariables])
library('corrplot')
corrplot(c, method="color")
```
## Data Imputation (solve missing values problem)
```{r}
# TRAINING DATA
a<-sapply(trainData,function(x)sum(is.na(x)))
data.frame(a,variable=colnames(trainData))%>%
ggplot(aes(reorder(variable,-a),a))+
geom_bar(stat="identity")+
labs(x="variable")+
theme(axis.text.x = element_text(angle=90))
# it is seen that the column "lead_time" has a lot of missing values
# therefore we replace it with mean value of the column lead_time
trainData$lead_time[is.na(trainData$lead_time)] = mean(trainData$lead_time, na.rm=TRUE)
summary(trainData)
```
```{r}
# TESTING DATA
b<-sapply(testData,function(x)sum(is.na(x)))
data.frame(a,variable=colnames(testData))%>%
ggplot(aes(reorder(variable,-b),b))+
geom_bar(stat="identity")+
labs(x="variable")+
theme(axis.text.x = element_text(angle=90))
# it is seen that the column "lead_time" has a lot of missing values
# therefore we replace it with mean value of the column lead_time
testData$lead_time[is.na(testData$lead_time)] = mean(testData$lead_time, na.rm=TRUE)
summary(testData)
```
## CONVERTING ALL CHARCHTER VALUES TO NUMERIC
```{r}
#function to map yes and no values to 1 and 0
Mapvalue <- function(x) {
plyr::mapvalues(x, from = c("Yes", "No"), to = c(1,0))
}
# TRAINING DATA
categoricalData1<-select_if(trainData,is.factor)
categoricalData1[c(1:ncol(categoricalData1))] <- lapply(categoricalData1[c(1:ncol(categoricalData1))], Mapvalue)
NumericData1<-select_if(trainData,is.numeric)
trainData <- cbind(NumericData1, categoricalData1)
head(trainData)
# TESTING DATA
categoricalData2<-select_if(testData,is.factor)
categoricalData2[c(1:ncol(categoricalData2))] <- lapply(categoricalData2[c(1:ncol(categoricalData2))], Mapvalue)
NumericData2<-select_if(testData,is.numeric)
testData <- cbind(NumericData2, categoricalData2)
head(testData)
```
## SAMPLING
```{r}
# It is clearly seen that the outcome variable 'went_on_backorder' is biased, therefore it is necessary to oversample the data so as to get a better accuracy and fit on the model
sampledTrainData <- ovun.sample(went_on_backorder~., data = trainData, method = "both")$data
table(trainData$went_on_backorder)
table(sampledTrainData$went_on_backorder)
grid.arrange(
ggplot(trainData, aes(went_on_backorder)) + geom_bar() ,
ggplot(sampledTrainData, aes(went_on_backorder)) + geom_bar(),
ncol = 2)
```
## MODELING
```{r}
# LOGISTIC REGRESSION
fit.glm1 <- caret::train(went_on_backorder~ lead_time +sales_1_month+ national_inv + sales_3_month + forecast_9_month , data=trainData, method="glm",family= binomial)
varImp(fit.glm1)
fit.glm1$results
```
```{r}
fit.glm2 <- caret::train(went_on_backorder~ lead_time + national_inv , data=underSampledTrainData, method="glm",family= binomial)
varImp(fit.glm2)
fit.glm2$results
```
```{r}
# DECISION TREE
fit.dt1 <- caret::train(went_on_backorder~., data=underSampledTrainData, method="rpart")
varImp(fit.dt1)
fit.dt1$results
```
```{r}
fit.dt2 <- caret::train(went_on_backorder~ lead_time +sales_9_month+ national_inv + sales_6_month + forecast_3_month , data=underSampledTrainData, method="rpart")
varImp(fit.dt2)
fit.dt2$results
```
## RESULTS
```{r}
results <- resamples(list(GLM=fit.glm1,CART=fit.dt2))
summary(results)
```
```{r}
summary(fit.glm1)
```
```{r}
caret::confusionMatrix(underSampledTrainData$went_on_backorder,predict(fit.glm1),positive = "1")
```
## Prediction
```{r}
test.glm <- predict(fit.glm1, newdata = testData)
Conflog<-confusionMatrix(data = test.glm , reference =testData$went_on_backorder ,positive = "1")
Conflog
```
```{r}
caret::confusionMatrix(underSampledTrainData$went_on_backorder,predict(fit.dt1),positive = "1")
```
```{r}
test.DT <- predict(fit.dt1, newdata = testData)
#-- Confusion matrix of test data for Decision tree
Confdt<-confusionMatrix(data = test.DT , reference =testData$went_on_backorder ,positive = "1")
Confdt
```
## Result of Predicted Model
```{r}
Results1<-cbind(Conflog$byClass,Confdt$byClass)
Results1
```