Repository navigation
Expand file tree
/
Copy pathe-commerce-shipping-delay-prediction.Rmd
More file actions
936 lines (640 loc) · 31.9 KB
/
Copy pathe-commerce-shipping-delay-prediction.Rmd
File metadata and controls
936 lines (640 loc) · 31.9 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
---
title: "Data_Mining_project"
author: "Pratiksha Thakkar"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, include=FALSE}
library(viridis)
library(dplyr)
library(knitr)
library(kableExtra)
url<-"/Users/newuser/Downloads/E Commerce Shipping Dataset.csv"
library(readr)
dmdata<-read_delim(file=url,delim=",")
```
### Analyzing the historical data of E-Commerce Shipping to identify patterns and factors contributing to late deliveries
```{r}
names(dmdata)
```
```{r}
dmdatadf<- as.data.frame(dmdata)
head(dmdatadf)
```
```{r}
total_observations <- nrow(dmdatadf)
total_observations
```
```{r}
str(dmdatadf)
```
```{r}
info_data <- data.frame(missing = colSums(is.na(dmdatadf)))
info_data$percent_missing <- (info_data$missing/nrow(dmdatadf)) * 100
info_data$unique <- lapply(dmdatadf, function(column) length(unique(column)))
info_data$dtype <- sapply(dmdatadf, class)
info_data
```
As we can see from the table above, there are no null values in the entire dataset
```{r}
warehouse_summary <- dmdatadf %>%
group_by(Warehouse_block) %>%
summarize(count = n(), percentage = round(n() / nrow(dmdatadf), 3))
# Display summary using kable
warehouse_summary %>%
kable(caption = "Warehouse Block Summary") %>%
kable_styling(full_width = FALSE) %>%
column_spec(1:3, bold = TRUE, color = viridis(7), background = "white")
```
```{r}
reached_on_time_summary <- dmdatadf %>%
group_by(Reached.on.Time_Y.N) %>%
summarize(count = n(), percentage = round(n() / nrow(dmdatadf), 3))
# Display summary using kable
reached_on_time_summary %>%
kable(caption = "Reached on Time Summary") %>%
kable_styling(full_width = FALSE) %>%
column_spec(1:3, bold = TRUE, color = viridis(7), background = "white")
```
```{r}
shipment_summary <- dmdatadf %>%
group_by(Mode_of_Shipment) %>%
summarize(count = n(), percentage = round(n() / nrow(dmdatadf), 3))
# Display summary using kable
shipment_summary %>%
kable(caption = "Mode of Shipment Summary") %>%
kable_styling(full_width = FALSE) %>%
column_spec(1:3, bold = TRUE, color = viridis(7), background = "white")
```
```{r}
library(kableExtra)
# Assuming data_dict is your data frame
dmdatadf %>% select(c(Customer_rating,Cost_of_the_Product,Discount_offered,Weight_in_gms,Discount_offered)) %>% summary() %>%
kable(caption = "data_summary") %>%
kable_styling(full_width = FALSE) %>%
column_spec(1:5, bold = TRUE, color = viridis(1), background = "white")
```
```{r}
data_dict <- data.frame(
Variable = names(dmdata),
Description = c(
"customer ID",
"Warehouse_block",
"Mode of Shipment",
"No. of calls made to customer care",
"Customer ratings out of 5",
"Cost of the product",
"Number of Prior purchases ",
"Product_importance",
"Gender ",
"Discount offered ",
"Weight of the product in grams",
"If the product reached on time or not"),
Example = c("1,2,3 ...", "A,B,C,","Ship,Flight,road" ," <2 low , 3-4 medium, >5 high", "0, 1, 2 ...", "1 to 5", "96 -310 USD ..", "1,2,3...", "Male/Female", "1-65 USD", "1001 gms to 7846 gms", "Yes/No")
)
library(kableExtra)
# Assuming data_dict is your data frame
data_dict %>%
kable(caption = "Data Dictionary") %>%
kable_styling(full_width = FALSE) %>%
column_spec(1:3, bold = TRUE, color = viridis(1), background = "white")
```
#### data visualization
```{r}
library(ggplot2)
library(viridis)
ggplot(dmdatadf, aes(x = Warehouse_block)) +
geom_bar(fill =magma(5)) +
labs(title = "Distribution of Warehouse Blocks")
```
```{r}
ggplot(dmdatadf, aes(x = Mode_of_Shipment)) +
geom_bar(fill = magma(3)) +
labs(title = "Distribution of Shipment Modes")
```
```{r}
ggplot(dmdatadf, aes(x =Customer_care_calls )) +
geom_histogram(fill = "salmon", bins = 5) +
labs(title = "Distribution of Customer Care Calls")
```
```{r}
ggplot(dmdatadf, aes(x = Customer_care_calls)) +
geom_bar(fill = "salmon") +
labs(title = "Distribution of Customer Care Calls")
```
```{r}
```
```{r}
ggplot(dmdatadf, aes(x = Customer_rating)) +
geom_bar(fill = "salmon") +
labs(title = "Distribution of Customer Ratings")
```
```{r}
ggplot(dmdatadf, aes(x = Cost_of_the_Product)) +
geom_histogram(fill = "salmon", bins = 15) +
labs(title = "Distribution of Product Costs")
```
```{r}
ggplot(dmdatadf, aes(x = Prior_purchases)) +
geom_histogram(fill = "salmon", bins = 10) +
labs(title = "Distribution of Prior Purchases")
```
```{r}
ggplot(dmdatadf, aes(x = Prior_purchases)) +
geom_bar(fill = "salmon", bins = 10) +
labs(title = "Distribution of Prior Purchases")
```
```{r}
ggplot(dmdatadf, aes(x = Product_importance)) +
geom_bar(fill = "salmon") +
labs(title = "Distribution of Product Importance")
```
```{r}
ggplot(dmdatadf, aes(x = Gender)) +
geom_bar(fill = "salmon") +
labs(title = "Distribution of Gender")
```
```{r}
ggplot(dmdatadf, aes(x = Discount_offered)) +
geom_histogram(fill = "salmon", bins = 10) +
labs(title = "Distribution of Discounts Offered")
```
```{r}
ggplot(dmdatadf, aes(x = Weight_in_gms)) +
geom_histogram(fill = "salmon", bins = 40) +
labs(title = "Distribution of Product Weights")
```
```{r}
library(ggplot2)
ggplot(dmdatadf, aes(x = factor(Reached.on.Time_Y.N), fill = factor(Reached.on.Time_Y.N))) +
geom_bar() +
scale_x_discrete(labels = c("0" = "On Time", "1" = "Not On Time")) +
labs(title = "Distribution of On-Time Delivery",
x = "Reached on Time", y = "Frequency")
```
```{r}
library(ggplot2)
total_reviews <- aggregate(Customer_rating ~ Reached.on.Time_Y.N, data = dmdatadf, FUN = length)
ggplot(dmdatadf, aes(x = Reached.on.Time_Y.N, fill = factor(Customer_rating))) +
geom_bar(position = "stack", alpha = 0.7) +
geom_text(data = total_reviews, aes(label = Customer_rating, y = Customer_rating, group = Reached.on.Time_Y.N),
position = position_stack(vjust = 0.5), color = "black", size = 3) +
labs(title = "Distribution of Customer Ratings by On-Time Delivery",
x = "Reached on Time", y = "Frequency", fill = "Customer Rating")
```
which mode of shipment is used as per the weight of product and does it affect the time it reaches.
```{r}
ggplot(dmdatadf, aes(x = factor(Mode_of_Shipment), fill = factor(Reached.on.Time_Y.N))) +
geom_bar() +
scale_x_discrete(labels = c("0" = "Flight", "1" = "Road" ,"2" = "Ship")) +
labs(title = "Distribution of On-Time Delivery",
x = "Mode of shipment", y = "Weight of product in gms")
```
```{r}
min_rating <- min(dmdatadf$Customer_rating)
max_rating <- max(dmdatadf$Customer_rating)
# Create a histogram with dynamically calculated breaks
hist(dmdatadf$Customer_rating,
main = "Histogram of Customer Ratings",
xlab = "Rating",
ylab = "Frequency",
col = "skyblue",
border = "black",
breaks = seq(min_rating - 0.5, max_rating + 0.5, by = 1), # Adjust breaks to cover the range of values
ylim = c(0, max(table(dmdatadf$Customer_rating)) + 1) # Set y-axis limits
)
str(dmdatadf)
```
##### labelling.
```{r}
library(caret)
library(fastDummies)
categorical_cols <- c("Warehouse_block", "Mode_of_Shipment")
dmdatadf <- dummy_cols(dmdatadf,select_columns = categorical_cols)
#dmdatadf = data.frame(predict(dmy, newdata=dmdatadf))
#data_combined <- cbind(dmdatadf[, setdiff(names(dmdatadf), categorical_cols), drop = TRUE])
dmdatadf <- dmdatadf[, !names(dmdatadf) %in% categorical_cols]
data_combined <- subset(dmdatadf, select=-c(ID))
data_combined[, grep(paste(categorical_cols, collapse="|"), names(data_combined))] <- lapply(data_combined[, grep(paste(categorical_cols, collapse="|"), names(data_combined))], factor)
data_combined$Product_importance = factor(ifelse(data_combined$Product_importance == "low", 0,ifelse (data_combined$Product_importance == "medium", 1, ifelse (data_combined$Product_importance == "high",2,3))))
data_combined$Prior_purchases = factor(ifelse(data_combined$Prior_purchases == 2, 0,ifelse (data_combined$Prior_purchases == 3, 1, 2 )))
data_combined$Weight_in_gms <- cut(data_combined$Weight_in_gms,
breaks = c(-Inf, 2000, 4000, Inf),
labels = c("0", "1", "2"),
right = FALSE)
str(data_combined)
```
```{r}
library(ggplot2)
# Apply logarithmic transformation to Discount_offered variable
data_combined$log_discount <- log(data_combined$Discount_offered + 1) # Adding 1 to handle zero values if present
# Ploing histogram of the transformed variable
ggplot(data_combined, aes(x = log_discount)) +
geom_histogram(fill = "salmon", bins = 10) +
labs(title = "Distribution of Log-transformed Discounts Offered")
```
```{r}
# Load necessary libraries
library(caret)
library(lattice)
# Set seed for reproducibility
set.seed(123)
# Split the data into partition (50%), validation (30%), and holdout (20%)
partitionIndex <- createDataPartition(data_combined$Reached.on.Time_Y.N, p = 0.5, list = FALSE)
partition_data <- data_combined[partitionIndex, ]
remaining_data <- data_combined[-partitionIndex, ]
validationIndex <- createDataPartition(data_combined$Reached.on.Time_Y.N, p = 0.3, list = FALSE)
validation_data <- remaining_data[validationIndex, ]
holdout_data <- remaining_data[-validationIndex, ]
# Print the dimensions of each subset
cat("Partition data:", dim(partition_data), "\n")
cat("Validation data:", dim(validation_data), "\n")
cat("Holdout data:", dim(holdout_data), "\n")
```
```{r}
# Load the rpart package
library(rpart)
library(partykit)
library(rpart.plot)
partition_data$Reached.on.Time_Y.N <- factor(partition_data$Reached.on.Time_Y.N)
validation_data$Reached.on.Time_Y.N <- factor(validation_data$Reached.on.Time_Y.N)
holdout_data$Reached.on.Time_Y.N <- factor(holdout_data$Reached.on.Time_Y.N)
# Train the classification tree using partition_data
tree_model <- rpart(Reached.on.Time_Y.N ~ ., data = partition_data, method = "class")
# plotting tree
prp(tree_model, type = 1, extra = 1, under = TRUE, split.font = 1, varlen = -10)
# Make predictions on the validation data
tree_predictions <- predict(tree_model, validation_data, type = "class")
# Evaluate the performance on the validation data
accuracy_validation <- sum(tree_predictions == validation_data$Reached.on.Time_Y.N, na.rm = TRUE) / length(tree_predictions)
cat("Accuracy on validation data:", accuracy_validation, "\n")
# Make predictions on the holdout data
tree_holdout_predictions <- predict(tree_model, holdout_data, type = "class")
# Evaluate the performance on the holdout data
accuracy_holdout <- sum(tree_holdout_predictions == holdout_data$Reached.on.Time_Y.N, na.rm = TRUE) / length(tree_holdout_predictions)
cat("Accuracy on holdout data:", accuracy_holdout, "\n")
```
```{r}
# Train the Naive Bayes classifier using partition_data
nb_model <- e1071::naiveBayes(Reached.on.Time_Y.N ~ ., data = partition_data)
# Make predictions on the validation data
nb_predictions <- predict(nb_model, validation_data)
# Evaluate the performance on the validation data
accuracy_validation <- mean(nb_predictions == validation_data$Reached.on.Time_Y.N, na.rm = TRUE)
cat("Accuracy on validation data:", accuracy_validation, "\n")
# Make predictions on the holdout data
nb_holdout_predictions <- predict(nb_model, holdout_data)
# Evaluate the performance on the holdout data
accuracy_holdout <- mean(nb_holdout_predictions == holdout_data$Reached.on.Time_Y.N, na.rm = TRUE)
cat("Accuracy on holdout data:", accuracy_holdout, "\n")
```
```{r}
# Train the logistic regression model using partition_data
partition_data <- subset(partition_data, select=-c(Warehouse_block_F, Mode_of_Shipment_Ship))
logistic_model <- glm(Reached.on.Time_Y.N ~ ., data = partition_data, family = binomial)
#trControl <- caret::trainControl(method="cv", number=5, allowParallel=TRUE)
#logit.reg <- caret::train(Reached.on.Time_Y.N ~ ., data=partition_data,trcontrol=trcontrol,
#fit logistic regression with a generalized linear model
#method="glm", family="binomial")
options(scipen = 999)
summary(logistic_model)
# Make predictions on the validation data
logistic_predictions <- predict(logistic_model, validation_data, type = "response")
# Convert predicted probabilities to binary predictions (0 or 1)
logistic_predictions_binary <- ifelse(logistic_predictions > 0.5, 1, 0)
# Evaluate the performance on the validation data
accuracy_validation <- mean(logistic_predictions_binary == validation_data$Reached.on.Time_Y.N, na.rm = TRUE)
cat("Accuracy on validation data:", accuracy_validation, "\n")
# Make predictions on the holdout data
logistic_holdout_predictions <- predict(logistic_model, holdout_data, type = "response")
# Convert predicted probabilities to binary predictions (0 or 1)
logistic_holdout_predictions_binary <- ifelse(logistic_holdout_predictions > 0.5, 1, 0)
# Evaluate the performance on the holdout data
accuracy_holdout <- mean(logistic_holdout_predictions_binary == holdout_data$Reached.on.Time_Y.N, na.rm = TRUE)
cat("Accuracy on holdout data:", accuracy_holdout, "\n")
```
```{r}
# confusion matrix before stepwise
# Confusion matrix for validation data
confusion_matrix_validation <- confusionMatrix(factor(logistic_predictions_binary), factor(validation_data$Reached.on.Time_Y.N))
print(confusion_matrix_validation)
#Confusion matrix for holdout data
confusion_matrix_holdout <- confusionMatrix(factor(logistic_holdout_predictions_binary), factor(holdout_data$Reached.on.Time_Y.N))
print(confusion_matrix_holdout)
```
```{r}
#THIS IS AFTER STEPWISE VALIDATION AND HOLDOUT CONFUSION MATRIX
final_model <- glm(Reached.on.Time_Y.N ~ Customer_care_calls + Customer_rating + Cost_of_the_Product + Prior_purchases + Product_importance + Discount_offered + Weight_in_gms + log_discount, data=partition_data, family = binomial)
summary(final_model)
prediction_validate <- predict(final_model, newdata = validation_data, type = "response")
# Convert predicted probabilities to predicted classes (0 or 1) based on a threshold (e.g., 0.5)
threshold <- 0.5
predicted_classes <- ifelse(prediction_validate > 0.5, 1, 0)
validation_data$predicted <- ifelse(predicted_classes >= threshold, 1, 0)
# Confusion matrix for validation data
confusion_matrix_validation <- confusionMatrix(factor(predicted_classes), factor(validation_data$Reached.on.Time_Y.N))
print(confusion_matrix_validation)
# Confusion matrix for holdout data
holdout_validate <- predict(final_model, newdata = holdout_data, type = "response")
# Convert predicted probabilities to predicted classes (0 or 1) based on a threshold (e.g., 0.5)
threshold <- 0.5
holdout_classes <- ifelse(holdout_validate > 0.5, 1, 0)
holdout_data$predicted <- ifelse(holdout_validate >= threshold, 1, 0)
#Confusion matrix for holdout data
confusion_matrix_holdout <- confusionMatrix(factor(holdout_classes), factor(holdout_data$Reached.on.Time_Y.N))
print(confusion_matrix_holdout)
```
```{r}
# Fit the logistic regression model
final_model <- glm(Reached.on.Time_Y.N ~ Customer_care_calls + Customer_rating + Cost_of_the_Product + Prior_purchases + Product_importance + Discount_offered + Weight_in_gms + log_discount, data = partition_data, family = binomial)
# Summary of the final model
summary(final_model)
# Predict probabilities for the validation data
prediction_validate <- predict(final_model, newdata = validation_data, type = "response")
# Initialize vectors to store threshold and specificity values
threshold_values <- c()
specificity_values <- c()
# Loop through different threshold values
for (threshold in seq(0.1, 0.9, by = 0.1)) {
# Convert predicted probabilities to predicted classes based on the threshold
predicted_classes <- ifelse(prediction_validate > threshold, 1, 0)
# Calculate confusion matrix for validation data
confusion_matrix_validation <- confusionMatrix(factor(predicted_classes), factor(validation_data$Reached.on.Time_Y.N))
# Calculate specificity
specificity <- confusion_matrix_validation$byClass["Specificity"]
# Store threshold and specificity values
threshold_values <- c(threshold_values, threshold)
specificity_values <- c(specificity_values, specificity)
}
# Combine threshold and specificity values into a data frame
threshold_specificity_df <- data.frame(Threshold = threshold_values, Specificity = specificity_values)
# Find the threshold value corresponding to the highest specificity
best_threshold <- threshold_specificity_df$Threshold[which.max(threshold_specificity_df$Specificity)]
# Print the best threshold
cat("Best Threshold (Highest Specificity):", best_threshold, "\n")
# Predict probabilities for the holdout data
holdout_validate <- predict(final_model, newdata = holdout_data, type = "response")
# Convert predicted probabilities to predicted classes based on the best threshold
holdout_classes <- ifelse(holdout_validate > best_threshold, 1, 0)
# Confusion matrix for holdout data using the best threshold
confusion_matrix_holdout <- confusionMatrix(factor(holdout_classes), factor(holdout_data$Reached.on.Time_Y.N))
print(confusion_matrix_holdout)
print(best_threshold)
```
```{r}
#THIS IS BEFORE STEPWISE NAIVE BAYES VALIDATION AND HOLDOUT CONFUSION MATRIX
# Train the Naive Bayes classifier using partition_data
#data_combined$Discount_offered <- exp(data_combined$log_discount) - 1
nb_model <- e1071::naiveBayes(Reached.on.Time_Y.N ~ ., data = partition_data)
# Make predictions on the validation data
nb_predictions <- predict(nb_model, validation_data)
# Confusion matrix for validation data
confusion_matrix_validation_nb <- confusionMatrix(nb_predictions, validation_data$Reached.on.Time_Y.N)
print(confusion_matrix_validation_nb)
#summary(nb_model)
# Specify a custom threshold for classification (e.g., 0.6)
prediction_holdout_log_model <- predict(nb_model, newdata = holdout_data, threshold = 0.5)
confusion_matrix_validation_nb <- confusionMatrix(prediction_holdout_log_model, holdout_data$Reached.on.Time_Y.N)
print(confusion_matrix_validation_nb)
```
```{r}
library(e1071)
library(caret)
# Train the Naive Bayes classifier using partition_data
nb_model <- naiveBayes(Reached.on.Time_Y.N ~ ., data = partition_data)
# Make predictions on the validation data
nb_predictions_val <- predict(nb_model, newdata = validation_data)
# Confusion matrix for validation data
confusion_matrix_validation_nb <- confusionMatrix(nb_predictions_val, validation_data$Reached.on.Time_Y.N)
print("Validation Confusion Matrix:")
print(confusion_matrix_validation_nb)
# Initialize vectors to store results for different thresholds
threshold_values <- seq(0.1, 0.9, by = 0.1) # Adjust the step size as needed
sensitivity_values <- numeric(length(threshold_values))
specificity_values <- numeric(length(threshold_values))
accuracy_values <- numeric(length(threshold_values))
# Loop through different threshold values
for (i in seq_along(threshold_values)) {
# Make predictions on the holdout data with the current threshold
prediction_holdout <- predict(nb_model, newdata = holdout_data, threshold = threshold_values[i])
# Calculate confusion matrix for the holdout data
cm_holdout <- confusionMatrix(prediction_holdout, holdout_data$Reached.on.Time_Y.N)
# Store sensitivity, specificity, and accuracy for the current threshold
sensitivity_values[i] <- cm_holdout$byClass["Sensitivity"]
specificity_values[i] <- cm_holdout$byClass["Specificity"]
accuracy_values[i] <- cm_holdout$overall["Accuracy"]
# Print confusion matrix for the holdout data with the current threshold
cat("\nThreshold:", threshold_values[i], "\n")
print("Holdout Confusion Matrix:")
print(cm_holdout)
}
# Find the best threshold based on specificity
best_threshold_spec <- threshold_values[which.max(specificity_values)]
# Print the best threshold based on specificity
cat("\nBest Threshold (Highest Specificity):", best_threshold_spec, "\n")
```
```{r}
#THIS IS NAIVE BAYES AFTER STEPWISE CONFUSION MATRIX FOR VALIDATION AND HOLDOUT
# Train the Naive Bayes classifier using partition_data
nb_final_model <- e1071::naiveBayes(Reached.on.Time_Y.N ~ Customer_care_calls + Customer_rating + Cost_of_the_Product + Prior_purchases + Product_importance + Discount_offered + Weight_in_gms + log_discount, data = partition_data)
# Make predictions on the validation data
nb_predictions <- predict(nb_final_model, validation_data)
confusion_matrix_validation_nb <- confusionMatrix(nb_predictions, validation_data$Reached.on.Time_Y.N)
print(confusion_matrix_validation_nb)
#summary(nb_model)
# Convert predicted probabilities to predicted classes (0 or 1) based on a threshold (e.g., 0.5)
prediction_holdout_log_model <- predict(nb_final_model, newdata = holdout_data)
confusion_matrix_validation_nb <- confusionMatrix(prediction_holdout_log_model, holdout_data$Reached.on.Time_Y.N)
print(confusion_matrix_validation_nb)
```
```{r}
library(e1071)
library(caret)
# Train the Naive Bayes classifier using partition_data
nb_final_model <- e1071::naiveBayes(Reached.on.Time_Y.N ~ Customer_care_calls + Customer_rating + Cost_of_the_Product + Prior_purchases + Product_importance + Discount_offered + Weight_in_gms + log_discount, data = partition_data)
# Make predictions on the validation data
nb_predictions <- predict(nb_final_model, validation_data)
# Confusion matrix for validation data
confusion_matrix_validation_nb <- confusionMatrix(nb_predictions_val, validation_data$Reached.on.Time_Y.N)
print("Validation Confusion Matrix:")
print(confusion_matrix_validation_nb)
# Initialize vectors to store results for different thresholds
threshold_values <- seq(0.1, 0.9, by = 0.1) # Adjust the step size as needed
sensitivity_values <- numeric(length(threshold_values))
specificity_values <- numeric(length(threshold_values))
accuracy_values <- numeric(length(threshold_values))
# Loop through different threshold values
for (i in seq_along(threshold_values)) {
# Make predictions on the holdout data with the current threshold
prediction_holdout <- predict(nb_model, newdata = holdout_data, threshold = threshold_values[i])
# Calculate confusion matrix for the holdout data
cm_holdout <- confusionMatrix(prediction_holdout, holdout_data$Reached.on.Time_Y.N)
# Store sensitivity, specificity, and accuracy for the current threshold
sensitivity_values[i] <- cm_holdout$byClass["Sensitivity"]
specificity_values[i] <- cm_holdout$byClass["Specificity"]
accuracy_values[i] <- cm_holdout$overall["Accuracy"]
# Print confusion matrix for the holdout data with the current threshold
cat("\nThreshold:", threshold_values[i], "\n")
print("Holdout Confusion Matrix:")
print(cm_holdout)
}
# Find the best threshold based on specificity
best_threshold_spec <- threshold_values[which.max(specificity_values)]
# Print the best threshold based on specificity
cat("\nBest Threshold (Highest Specificity):", best_threshold_spec, "\n")
```
```{r}
#THIS IS CLASSIFICATION TREE BEFORE STEPWISE VALIDATION AND HOLDOUT CONFUSION MATRIX:
library(rpart)
partition_data$Reached.on.Time_Y.N <- factor(partition_data$Reached.on.Time_Y.N)
validation_data$Reached.on.Time_Y.N <- factor(validation_data$Reached.on.Time_Y.N)
holdout_data$Reached.on.Time_Y.N <- factor(holdout_data$Reached.on.Time_Y.N)
# Train the classification tree using partition_data
tree_model <- rpart(Reached.on.Time_Y.N ~ ., data = partition_data, method = "class")
# Make predictions on the validation data
tree_predictions_val <- predict(tree_model, validation_data, type = "class")
confusion_matrix_validation_class <- confusionMatrix(tree_predictions_val, validation_data$Reached.on.Time_Y.N)
print(confusion_matrix_validation_class)
tree_predictions_hold <- predict(tree_model, holdout_data, type = "class")
confusion_matrix_validation_class <- confusionMatrix(tree_predictions_hold, holdout_data$Reached.on.Time_Y.N)
print(confusion_matrix_validation_class)
```
```{r}
#THIS IS CLASSIFICATION TREE AFTER STEPWISE VALIDATION AND HOLDOUT CONFUSION MATRIX:
tree_model <- rpart(Reached.on.Time_Y.N ~ Customer_care_calls + Customer_rating + Cost_of_the_Product + Prior_purchases + Product_importance + Discount_offered + Weight_in_gms + log_discount, data = partition_data, method = "class")
# Make predictions on the validation data
tree_predictions_val <- predict(tree_model, validation_data, type = "class")
confusion_matrix_validation_class <- confusionMatrix(tree_predictions_val, validation_data$Reached.on.Time_Y.N)
print(confusion_matrix_validation_class)
tree_predictions_hold <- predict(tree_model, holdout_data, type = "class")
confusion_matrix_validation_class <- confusionMatrix(tree_predictions_hold, holdout_data$Reached.on.Time_Y.N)
print(confusion_matrix_validation_class)
```
```{r}
library(rpart)
library(caret)
# Fit the classification tree model using partition_data
tree_model <- rpart(Reached.on.Time_Y.N ~ Customer_care_calls + Customer_rating + Cost_of_the_Product + Prior_purchases + Product_importance + Discount_offered + Weight_in_gms + log_discount, data = partition_data, method = "class")
# Make predictions on the validation data
tree_predictions_val <- predict(tree_model, newdata = validation_data, type = "class")
# Ensure consistency in factor levels
levels(tree_predictions_val) <- levels(validation_data$Reached.on.Time_Y.N)
# Confusion matrix for validation data
confusion_matrix_validation_tree <- confusionMatrix(tree_predictions_val, validation_data$Reached.on.Time_Y.N)
print("Validation Confusion Matrix:")
print(confusion_matrix_validation_tree)
# Initialize vectors to store results for different thresholds
threshold_values <- seq(0.1, 0.9, by = 0.1) # Adjust the step size as needed
sensitivity_values <- numeric(length(threshold_values))
specificity_values <- numeric(length(threshold_values))
accuracy_values <- numeric(length(threshold_values))
# Loop through different threshold values
for (i in seq_along(threshold_values)) {
# Make predictions on the holdout data with the current threshold
tree_predictions_hold <- predict(tree_model, newdata = holdout_data, type = "prob")[, 2]
prediction_holdout <- ifelse(tree_predictions_hold > threshold_values[i], levels(holdout_data$Reached.on.Time_Y.N)[2], levels(holdout_data$Reached.on.Time_Y.N)[1])
# Ensure consistency in factor levels
prediction_holdout <- factor(prediction_holdout, levels = levels(holdout_data$Reached.on.Time_Y.N))
# Calculate confusion matrix for the holdout data
cm_holdout <- confusionMatrix(prediction_holdout, holdout_data$Reached.on.Time_Y.N)
# Store sensitivity, specificity, and accuracy for the current threshold
sensitivity_values[i] <- cm_holdout$byClass["Sensitivity"]
specificity_values[i] <- cm_holdout$byClass["Specificity"]
accuracy_values[i] <- cm_holdout$overall["Accuracy"]
# Print confusion matrix for the holdout data with the current threshold
cat("\nThreshold:", threshold_values[i], "\n")
print("Holdout Confusion Matrix:")
print(cm_holdout)
}
# Find the best threshold based on specificity
best_threshold_spec <- threshold_values[which.max(specificity_values)]
# Print the best threshold based on specificity
cat("\nBest Threshold (Highest Specificity):", best_threshold_spec, "\n")
```
#### AIC and step wise
```{r}
data_combined$Reached.on.Time_Y.N <- factor(data_combined$Reached.on.Time_Y.N, levels = c(0, 1))
trControl <- caret::trainControl(method="none")
model <- caret::train(Reached.on.Time_Y.N ~ ., data=data_combined, trControl=trControl,
# select backward elmination
method="glmStepAIC", direction='backward')
coef(model$finalModel)
model <- caret::train(Reached.on.Time_Y.N ~ ., data=data_combined, trControl=trControl,
method="glmStepAIC", direction='forward')
coef(model$finalModel)
model <- caret::train(Reached.on.Time_Y.N ~ ., data=data_combined, trControl=trControl,
method="glmStepAIC", direction='both')
coef(model$finalModel)
```
##### Cross-validation
```{r}
set.seed(1)
library(caret)
# define 5-fold
trControl <- caret::trainControl(method="cv", number=5, allowParallel=TRUE)
model <- caret::train(Reached.on.Time_Y.N ~ ., data=data_combined,
method="glm", # specify the model
trControl=trControl)
model
```
#### Exaustive
```{r}
library(fastDummies)
library(leaps)
search <- regsubsets(Reached.on.Time_Y.N ~ ., data=data_combined, nbest=1,
nvmax=ncol(data_combined), method="exhaustive")
sum <- summary(search)
# show models
sum$which
sum$rsq
sum$adjr2
sum$cp
optimal <- which.min(sum$cp)
# determine the variable names for the optimal model
X <- summary(search)$which[, -1] # information about included predictors
xvars <- dimnames(X)[[2]] ## column names (all covariates except intercept)
xvars <- xvars[X[optimal,]]
optimal <- which.min(sum$cp)
# determine the variable names for the optimal model
X <- summary(search)$which[, -1] # information about included predictors
xvars <- dimnames(X)[[2]] ## column names (all covariates except intercept)
xvars <- xvars[X[optimal,]]
xvars <- c("Customer_care_calls","Customer_rating","Cost_of_the_Product","Prior_purchases","Product_importance","Discount_offered","Weight_in_gms")
# rebuild model for best predictor set
set.seed(1)
trControl <- caret::trainControl(method="cv", number=5, allowParallel=TRUE)
model <- caret::train(Reached.on.Time_Y.N ~ ., data=data_combined[, c("Reached.on.Time_Y.N", xvars)],
method="glm", # specify the model
trControl=trControl)
model
```
```{r}
```
```{r}
anova(logistic_model, test = 'Chisq')
```
<!-- Recommended Model: Decision Tree (or Random Forest)For this e-commerce shipping dataset, the Decision Tree (or an ensemble tree like Random Forest) is the best choice.Why Decision Tree is the Best Choice1. Superior Predictive AccuracyDecision Tree Holdout Accuracy: 68.36%Logistic Regression Holdout Accuracy: 65.82%Naïve Bayes Holdout Accuracy: 66.68%The tree model consistently outperforms linear and probabilistic baselines on unseen holdout test data.2. Non-Linear Feature InteractionsLinear models like Logistic Regression struggle with this dataset because key predictors—specifically Discount_offered and Weight_in_gms—do not have a strictly linear relationship with shipping delays.For instance:Discounts: Shipments with low to moderate discounts behave normally, but high discounts ($\ge 10\%$) trigger sharp, non-linear surges in shipping delays due to promotional volume spikes.Product Weights: Deliveries experience non-linear risk spikes in specific weight ranges (e.g., packages around 2,000–4,000g vs. light items under 2,000g).Decision trees handle these sharp step-function cutoffs naturally by creating conditional branches (e.g., if Discount > 10 AND Weight between 2000-4000g).3. Operational InterpretabilityUnlike "black-box" models, decision trees produce clear rule sets that logistics managers can easily turn into operational decisions (e.g., "Flag any promotional order over $200 with a discount above 10% for priority warehouse dispatch").Summary Recommendation MatrixMetric / NeedLogistic RegressionNaïve BayesDecision TreeAccuracy65.8%66.7%68.4%Handles Non-Linear Features❌ Poor⚠️ ModerateYES ExcellentActionable Business Rules⚠️ Coefficients only❌ Probability tablesYES Visual tree rules -->
```{r}
library(pROC)
# 1. Compute probabilities on your holdout data
prob_logistic <- predict(final_model, newdata = holdout_data, type = "response")
prob_tree <- predict(tree_model, newdata = holdout_data, type = "prob")[, 2]
# 2. Calculate ROC curves
roc_glm <- roc(holdout_data$Reached.on.Time_Y.N, prob_logistic)
roc_tree <- roc(holdout_data$Reached.on.Time_Y.N, prob_tree)
# 3. Save directly to PNG (without needing a 'roc_plot' object)
dir.create("images", showWarnings = FALSE)
png("images/roc_curve_comparison.png", width = 2100, height = 1800, res = 300, bg = "white")
# Plot standard ROC curve
plot(roc_tree, col = "#21908CFF", main = "ROC Curve Comparison (Holdout Set)", lwd = 2)
plot(roc_glm, col = "#440154FF", add = TRUE, lwd = 2)
legend("bottomright",
legend = c(paste("Decision Tree (AUC =", round(auc(roc_tree), 3), ")"),
paste("Logistic Reg (AUC =", round(auc(roc_glm), 3), ")")),
col = c("#21908CFF", "#440154FF"),
lwd = 2)
dev.off()
```