Repository navigation
Expand file tree
/
Copy pathuser_behavior.R
More file actions
129 lines (73 loc) · 2.75 KB
/
Copy pathuser_behavior.R
File metadata and controls
129 lines (73 loc) · 2.75 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
### User Behavior Dataset ###
setwd('E:/R_Datasets/R_projects_ML_cape_fear')
df <- read.csv('user_behavior/advertising.csv')
X <- data.frame(df$Age,df$Area.Income,df$Daily.Internet.Usage)
y <- data.frame(df$Clicked.on.Ad)
df_new <- data.frame(df$Age,df$Area.Income,df$Daily.Internet.Usage,df$Clicked.on.Ad)
pd <- sample(2,nrow(df_new),replace = TRUE,prob = c(0.85,0.15))
train <- df_new[pd==1,]
validate <- df_new[pd==2,]
library(partykit)
logitmod <- glm(df.Clicked.on.Ad~df.Daily.Internet.Usage+df.Area.Income+df.Age,data = train,family=binomial(link="logit") )
logitmod
plot(logitmod)
predicted <- plogis(predict(logitmod, validate))
print(predicted)
### Optimal Cutff ###
library(InformationValue)
optCutOff <- optimalCutoff(validate$df.Clicked.on.Ad, predicted)[1]
print(optCutOff)
### Accuracy ####
actuals_preds <- data.frame(cbind(actuals=validate$df.Clicked.on.Ad, predicteds=predicted))
print(actuals_preds)
correlation_accuracy <- cor(actuals_preds)
correlation_accuracy
#### Missclassification error
misClassError(validate$df.Clicked.on.Ad, predicted, threshold = optCutOff)*100
summary(logitmod)
plotROC(validate$df.Clicked.on.Ad, predicted)
### Min_max_accuracy #####
min_max_accuracy <- mean(apply(actuals_preds, 1, min) / apply(actuals_preds, 1, max))
print(min_max_accuracy)
### Confusion Matrix ###
conf <- confusionMatrix(validate$df.Clicked.on.Ad, predicted, threshold = optCutOff)
conf
accuracy <- ((conf$`0`[1]+conf$`1`[2])/(conf$`0`[1]+conf$`0`[2]+conf$`1`[1]+conf$`1`[2]))*100
accuracy
library(ggplot2)
autoplot(conf, type = "mosaic")
library(ggcorrplot)
ggcorrplot(conf/100)
library(ggplot2)
ggplot(df_new,
aes(x = df.Daily.Internet.Usage, y = df.Area.Income, size = df.Clicked.on.Ad)) +
geom_point(alpha = .5,fill=df_new$df.Age)
fit1 <- kmeans(df_new, 4)
fit2 <- kmeans(df_new, 5)
### correlation between centers ####
c1 <- fit1$centers
c2 <- fit2$centers
cor(c1)
cor(c2)
ggcorrplot(cor(c1))
library(superheat)
superheat(c1)
superheat(c2)
library(corrplot)
corrplot(cor(centers), method="number")
#########
library(cluster)
# Cluster Plot against 1st 2 principal components
# vary parameters for most readable graph
library(cluster)
clusplot(df_new, fit1$cluster, color=TRUE, shade=TRUE,
labels=4, lines=0)
# Centroid Plot against 1st 2 discriminant functions
library(fpc)
plotcluster(df_new, fit1$cluster)
#### Vlidating 2 cluster solutions
plot(cluster.stats(fit1$cluster,fit2$cluster),df_new$df.Clicked.on.Ad)
library(ggplot2)
ggplot(df_new,
aes(x = df.Daily.Internet.Usage, y = df.Area.Income, size = df.Clicked.on.Ad)) +
geom_point(alpha = .5,fill=fit2$cluster)