-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCost Comparsion.Rmd
More file actions
110 lines (80 loc) · 2.9 KB
/
Cost Comparsion.Rmd
File metadata and controls
110 lines (80 loc) · 2.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
---
title: "Cost and Comparison"
author: "Angel"
date: "2025-12-03"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
```
## Cost Calculation
The cost of contacting a customer and the revenue after client accpeting the campaign are given by the dataset which we will use.
The cost to contact: $30
The revenue after client accept campaign: $110
We will be using the sensitivity and specificity to calculate the potential revenue earn an individual customer.
```{r}
cost <- 30
revenue <- 110
extract_metrics_cost <- function(cm) {
sensitivity <- as.numeric(cm$byClass['Sensitivity'])
specificity <- as.numeric(cm$byClass['Specificity'])
expected_profit <- (revenue - cost) * sensitivity -
cost * (1-specificity)
data.frame(
Expected_profit = expected_profit
)
}
comparison_table <- rbind(
GLM = extract_metrics_cost(lr_cm_interactions),
KNN = extract_metrics_cost(knn_advanced_cm),
ANN = extract_metrics_cost(ann_base_matrix),
DecisionTree = extract_metrics_cost(dt_cm),
SVM = extract_metrics_cost(svm_cm),
RandomForest = extract_metrics_cost(rf_cm),
CostStack = extract_metrics_cost(cost_cm)
)
kable(comparison_table,
caption = "Comparison of All Models: Expected Profit",
digits = 2) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"),
full_width = F,
position = "center") %>%
row_spec(0, bold = TRUE, color = "white", background = "black")
```
Cost Combine Decision Tree Result
```{r}
library(ggplot2)
library(dplyr)
# Convert confusion matrix to data frame
cm_to_df <- function(cm) {
cm_table <- as.data.frame(cm$table)
total <- sum(cm_table$Freq)
cm_table <- cm_table %>%
mutate(Percentage = round((Freq / total) * 100, 1),
Label = paste(Freq, "\n(", Percentage, "%)", sep=""))
return(cm_table)
}
# Create plot
plot_confusion_matrix <- function(cm) {
cm_df <- cm_to_df(cm)
# Reverse the factor levels for Prediction to match your desired display
cm_df$Reference <- factor(cm_df$Reference, levels = c("0", "1"))
cm_df$Prediction <- factor(cm_df$Prediction, levels = c("1", "0")) # REVERSED!
ggplot(data = cm_df, aes(x = Reference, y = Prediction)) +
geom_tile(aes(fill = Freq), color = "black") +
geom_text(aes(label = Label), size = 6, color = "white") +
scale_fill_gradient(low = "lightblue", high = "blue") +
scale_x_discrete(limits = c("0", "1")) +
# No scale_y_discrete needed since factor levels already set correctly
labs(title = "Result Using Cost Combined Decision Tree Model",
x = "Reference",
y = "Prediction") +
theme_minimal() +
theme(axis.text = element_text(size = 12),
axis.title = element_text(size = 14),
plot.title = element_text(size = 16, hjust = 0.5))
}
# Generate the plot
plot_confusion_matrix(cost_cm)
```