-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKNN-Violin-Box plots.R
233 lines (196 loc) · 9.2 KB
/
KNN-Violin-Box plots.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
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
#-----------------------------------------------------------------
# 1. Missing values imputation using KNN and Violin plots/Boxplots
#-----------------------------------------------------------------
# 1. Missing values imputation
# load libraries and create artificial dataset
library(caret)
library(missForest)
library(tidyverse)
library(RANN)
library(gridExtra)
set.seed(2023) # for reproducibility
var1 <- rpois(900,2)
var2 <- rpois(900,4)
var3 <- rpois(900,6)
category <- c(rep('cat1', 300), rep('cat2', 300), rep('cat3', 300))
subcategory <- sample(LETTERS[1:6], size = 900, replace = TRUE,
prob = c(0.25, 0.3, 0.2, 0.1, 0.1, 0.05))
dataset <- data.frame(var1, var2, var3, subcategory, category)
# prodNA produce 5% missing data
dataset.mis = data.frame(prodNA(dataset[,1:3], noNA = 0.05), subcategory, category)
# save a copy of the dataset in .csv
write.csv(dataset.mis,
"path/dataset.mis.csv",
row.names = FALSE)
dataset.mis = read.csv("path/dataset.mis.csv",
header = TRUE)
dataset.mis[298:301, ] # excerpt of the dataset contains NA values
# var1 var2 var3 subcategory category
# 298 0 10 5 C cat1
# 299 NA 1 5 E cat1
# 300 2 3 5 A cat1
# 301 2 NA 8 B cat2
# knn imputation using caret and 5 'neighbours'
set.seed(2023)
dataset.mis.model = preProcess(dataset.mis %>%
dplyr::select(names(dataset.mis)),
"knnImpute", k = 5, knnSummary = mean)
dataset.mis.model
dataset.mis.pred = predict(dataset.mis.model, dataset.mis) # variables are normalized
dataset.mis.pred[298:301, ]
# var1 var2 var3 subcategory category
# 298 -1.37892580 3.0235674 -0.4137490 C cat1
# 299 0.70767513 -1.4093204 -0.4137490 E cat1
# 300 0.01214149 -0.4242342 -0.4137490 A cat1
# 301 0.01214149 -0.1287083 0.8754844 B cat2
# values in original scale
complete.dataset <- data.frame(col = names(dataset.mis[,1:3]),
mean = dataset.mis.model$mean,
sd = dataset.mis.model$std)
for(i in complete.dataset$col){
dataset.mis.pred[i] <- dataset.mis.pred[i]*dataset.mis.model$std[i] + dataset.mis.model$mean[i]
}
# now the dataset is complete
complete.data..dataset <- dataset.mis.pred
complete.data.dataset[298:301, ]
# var1 var2 var3 subcategory category
# 298 0 10.0 5 C cat1
# 299 3 1.0 5 E cat1
# 300 2 3.0 5 A cat1
# 301 2 3.6 8 B cat2
# 2. Visualization using Violin plots
dataset <- complete.data.dataset
# compute the mean of the variable 'var1' for each 'subcategory' group
dataset2 <- dataset %>%
group_by(subcategory) %>%
mutate(Mean_var1 = mean(var1))
# compute the mean of the variable 'var2' for each 'subcategory' group
dataset3 <- dataset %>%
group_by(subcategory) %>%
mutate(Mean_var2 = mean(var2))
# compute the mean of the variable 'var3' for each 'subcategory' group
dataset4 <- dataset %>%
group_by(subcategory) %>%
mutate(Mean_var3 = mean(var3))
#-------------------------------------------------------------------------------
# minimal code violin plot
ggplot(dataset, aes(x = subcategory, y = var1)) +
geom_violin()
# multiple violin plots
ggplot(dataset2, aes(x = subcategory, y = var1)) +
geom_violin(aes(fill=Mean_var1)) +
geom_point(aes(x = subcategory, y = var1), position = 'jitter', size = 0.4) +
scale_fill_gradient2('mean(var1)', low = "blue4",
mid = "white", high = "firebrick4",
midpoint = mean(dataset2$Mean_var1)) +
facet_wrap(~category, scales="free") +
labs(title = 'Boxplot for var1 x subcategory, for each category group',
subtitle = "Color gradient indicate the mean of the variable 'var1'",
caption = "Artificial dataset") +
theme(axis.text=element_text(size=5),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=6, face="italic", color="darkred"))
#-------------------------------------------------------------------------------
# Violin plot with facet_wrap
p1 <- ggplot(dataset2, aes(x = subcategory, y = var1)) +
geom_violin(aes(fill=Mean_var1)) +
geom_point(aes(x = subcategory, y = var1), position = 'jitter', size = 0.4) +
scale_fill_gradient2('mean(var1)', low = "blue4",
mid = "white", high = "firebrick4",
midpoint = mean(dataset2$Mean_var1)) +
facet_wrap(~category, scales="free") +
labs(title = 'Violin plot for var1 x subcategory, for each category group',
subtitle = "Color gradient indicate the mean of the variable 'var1'",
caption = "Artificial dataset") +
theme(axis.text=element_text(size=5),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=6, face="italic", color="darkred"))
# Violin plot with facet_wrap
p2 <- ggplot(dataset3, aes(x = subcategory, y = var2)) +
geom_violin(aes(fill= Mean_var2)) +
geom_point(aes(x = subcategory, y = var2), position = 'jitter', size = 0.4) +
scale_fill_gradient2('mean(var2)', low = "blue4",
mid = "white", high = "firebrick4",
midpoint = mean(dataset3$Mean_var2)) +
facet_wrap(~category, scales="free") +
labs(title = 'Violin plot for var2 x subcategory, for each category group',
subtitle = "Color gradient indicate the mean of the variable 'var2'",
caption = "Artificial dataset") +
theme(axis.text=element_text(size=5),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=6, face="italic", color="darkred"))
# Violin plot with facet_wrap
p3 <- ggplot(dataset4, aes(x = subcategory, y = var3)) +
geom_violin(aes(fill= Mean_var3)) +
geom_point(aes(x = subcategory, y = var3), position = 'jitter', size = 0.4) +
scale_fill_gradient2('mean(var3)', low = "blue4",
mid = "white", high = "firebrick4",
midpoint = mean(dataset4$Mean_var3)) +
facet_wrap(~category, scales="free") +
labs(title = 'Violin plot for var3 x subcategory, for each category group',
subtitle = "Color gradient indicate the mean of the variable 'var3'",
caption = "Artificial dataset") +
theme(axis.text=element_text(size=5),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=6, face="italic", color="darkred"))
final.plot <- grid.arrange(p1, p2, p3)
# 3. Visualization using Boxplots
dataset <- complete.data.dataset
# compute the mean of the variable 'var1' for each 'subcategory' group
dataset2 <- dataset %>%
group_by(subcategory) %>%
mutate(Mean_var1 = mean(var1))
# compute the mean of the variable 'var2' for each 'subcategory' group
dataset3 <- dataset %>%
group_by(subcategory) %>%
mutate(Mean_var2 = mean(var2))
# compute the mean of the variable 'var3' for each 'subcategory' group
dataset4 <- dataset %>%
group_by(subcategory) %>%
mutate(Mean_var3 = mean(var3))
#-------------------------------------------------------------------------------
# minimal code violin plot
ggplot(dataset, aes(x = subcategory, y = var1)) +
geom_boxplot()
# multiple violin plots
ggplot(dataset, aes(x = subcategory, y = var1)) +
geom_boxplot() +
geom_point(aes(x = subcategory, y = var1)) +
facet_wrap(~category, scales="free") +
labs(title = 'Boxplot for var1 x subcategory, for each category group') +
theme(axis.text=element_text(size=5),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=6, face="italic", color="darkred"))
#-------------------------------------------------------------------------------
# Violin plot with facet_wrap
p2 <- ggplot(dataset3, aes(x = subcategory, y = var2)) +
geom_boxplot(aes(fill= Mean_var2)) +
geom_point(aes(x = subcategory, y = var2), position = 'jitter', size = 0.4) +
scale_fill_gradient2('mean(var2)', low = "blue4",
mid = "white", high = "firebrick4",
midpoint = mean(dataset3$Mean_var2)) +
facet_wrap(~category, scales="free") +
labs(title = 'Boxplot for var2 x subcategory, for each category group',
subtitle = "Color gradient indicate the mean of the variable 'var2'",
caption = "Artificial dataset") +
theme(axis.text=element_text(size=5),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=6, face="italic", color="darkred"))
# Violin plot with facet_wrap
p3 <- ggplot(dataset4, aes(x = subcategory, y = var3)) +
geom_boxplot(aes(fill= Mean_var3)) +
geom_point(aes(x = subcategory, y = var3), position = 'jitter', size = 0.4) +
scale_fill_gradient2('mean(var3)', low = "blue4",
mid = "white", high = "firebrick4",
midpoint = mean(dataset4$Mean_var3)) +
facet_wrap(~category, scales="free") +
labs(title = 'Boxplot for var3 x subcategory, for each category group',
subtitle = "Color gradient indicate the mean of the variable 'var3'",
caption = "Artificial dataset") +
theme(axis.text=element_text(size=5),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=6, face="italic", color="darkred"))
final.plot <- grid.arrange(p1, p2, p3)
#----
# end
#----