-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem2.R
395 lines (294 loc) · 10.9 KB
/
problem2.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
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
## AMI22T Home Exercise 3, Problem 2
## Personality Analysis
## Saumya Gupta, DS
# set path of all-data export import directory
setwd('C:/Users/gupta/OneDrive/Documents/MS-DS/AMI22T/HomeExercises/HomeExercise3/')
# load required packages
library(data.table)
library(mvtnorm)
library(ggplot2)
library(dplyr)
# load data and store in a data table
survey.results <- fread("five-personality.txt")
survey.results <- data.frame(survey.results)
# check structure if any coding required
str(survey.results)
# check missing data points
sum(is.na(survey.results))
# check number of rows with missing data
sum(apply(survey.results, 1, anyNA))
# calculate percentage of rows with missing data
mean(is.na(survey.results))
# remove missing data
survey.results <- na.omit(survey.results)
# # save column-wise distributions
# for (question in names(survey.results)) {
# column.distribution.plot = ggplot(survey.results,
# aes_string(question)) + geom_histogram()
#
# ggsave(paste(question, "_distribution", ".png"),
# plot = column.distribution.plot)
# }
# set seed
set.seed(9899)
# Sampling Appropriate Subset (Analysis with K-Means) ----
# get variance-covariance matrix
dispersion <- var(survey.results)
# get mean vector
centroid <- colMeans(survey.results)
# get density heights using the statistics and the survey results
density.heights <-
dmvnorm(as.matrix(survey.results),
mean = centroid,
sigma = dispersion)
# check total within-cluster sum of squares for different k-values
# with sample size = 1000
# pass density heights calculated previously to get similarly distributed sample
sample.people <-
sample(1:nrow(survey.results), 1000, prob = density.heights)
survey.results.sample <- survey.results[sample.people, ]
total.within.ss = c()
#check with different k values
k.values = 1:15
for (k in k.values) {
km.output = kmeans(survey.results.sample, k, nstart = 50)
total.within.ss = c(total.within.ss, km.output$tot.withinss)
}
# plot within-cluster sum of squares score with different k (elbow plot)
scores.for.k = data.frame(k.values = k.values,
total.within.ss = total.within.ss)
ggplot(scores.for.k, aes(k.values, total.within.ss)) +
geom_point() +
ggtitle("Total Within-Cluster Sum of Squares for K-Values
Sample Size = 1000") +
theme(plot.title = element_text(hjust = 0.5))
# do same with sample size = 2000
sample.people <-
sample(1:nrow(survey.results), 2000, prob = density.heights)
survey.results.sample <- survey.results[sample.people, ]
total.within.ss = c()
k.values = 1:15
for (k in k.values) {
km.output = kmeans(survey.results.sample, k, nstart = 50)
total.within.ss = c(total.within.ss, km.output$tot.withinss)
}
scores.for.k = data.frame(k.values = k.values,
total.within.ss = total.within.ss)
ggplot(scores.for.k, aes(k.values, total.within.ss)) +
geom_point() +
ggtitle("Total Within-Cluster Sum of Squares for K-Values
Sample Size = 2000") +
theme(plot.title = element_text(hjust = 0.5))
# # check different sample sizes with K = 4 for K-Means
# total.within.ss = c()
#
# sample.sizes = seq(500, 2500, by = 50)
#
# for (sample_size in sample.sizes) {
# sample.people <-
# sample(1:nrow(survey.results), sample_size, prob = density.heights)
#
# survey.results.sample <- survey.results[sample.people, ]
#
# km.output = kmeans(survey.results.sample, 4, nstart = 50)
#
# total.within.ss = c(total.within.ss, km.output$tot.withinss)
# }
#
# scores.for.sample.size = data.frame(sample.sizes = sample.sizes,
# total.within.ss = total.within.ss)
#
# ggplot(scores.for.sample.size, aes(sample.sizes, total.within.ss)) +
# geom_point() +
# ggtitle("Total Within-Cluster Sum of Squares for Sample Sizes\nk = 4") +
# theme(plot.title = element_text(hjust = 0.5))
# Clustering (Sample Size = 1000, K = 4) ----
## K-Means Clustering ----
sample.people <-
sample(1:nrow(survey.results), 1000, prob = density.heights)
survey.results.sample <- survey.results[sample.people, ]
# perform K-Means
km.output = kmeans(survey.results.sample, 4, nstart = 50)
# bind cluster results with data
clustered = cbind(survey.results.sample, km.output$cluster)
names(clustered)[51] <- "k.means.cluster"
## Hierarchical Clustering ----
# perform hierarchical with complete linkage
hc.complete = hclust(dist(survey.results.sample), method = "complete")
# perform hierarchical with average linkage
hc.average = hclust(dist(survey.results.sample), method = "average")
# perform hierarchical with single linkage
hc.single = hclust(dist(survey.results.sample), method = "single")
# bind cluster results with data
clustered = cbind(clustered,
cutree(hc.complete, 4),
cutree(hc.average, 4),
cutree(hc.single, 4))
names(clustered)[52] <- "hierarchical.complete.cluster"
names(clustered)[53] <- "hierarchical.average.cluster"
names(clustered)[54] <- "hierarchical.single.cluster"
# Validations and Visualisations ----
# check for singleton clusters
table(clustered$k.means.cluster)
table(clustered$hierarchical.complete.cluster)
table(clustered$hierarchical.average.cluster)
table(clustered$hierarchical.single.cluster)
# remove cluster results from average and single linkage
clustered <- clustered %>% select(-c(53, 54))
# check agreement between results from hierarchical and K-Means clustering
table(clustered$k.means.cluster,
clustered$hierarchical.complete.cluster)
# in proportions from hierarchical clusters perspective
prop.table(
table(
clustered$k.means.cluster,
clustered$hierarchical.complete.cluster
),
margin = 1
)
# use first two PCs to accommodate visualising clusters
# find principal components
principal.components <-
prcomp(clustered %>% select(-c(51, 52)), scale = TRUE)
# next three code snippets are just for information
# calculate variances using standard deviation of the projected points
pc.variances <- (principal.components$sdev) ^ 2
# calculate proportion explained
pc.proportion.variances <- pc.variances / sum(pc.variances)
# get components collectively explaining 90% of input variance
number.of.components <-
length(pc.proportion.variances[cumsum(pc.proportion.variances) < .90]) + 1
# get first two components to represent points
# (although will not represent properly)
first.2.PCs = principal.components$x[, 1:2]
# plot K-Means results
plot(
first.2.PCs,
col = (clustered$k.means.cluster + 1),
main = "K-Means Clustering Results\n(K=4)",
xlab = "",
ylab = "",
pch = 20,
cex = 1
)
# plot hierarchical results
plot(
first.2.PCs,
col = (clustered$hierarchical.complete.cluster + 1),
main = "Hierarchical Clustering Results\n(K=4)",
xlab = "",
ylab = "",
pch = 20,
cex = 1
)
# cluster labelling algorithm according to K-Means results
# define reversed question from survey
negatives = c(
"mean_EXT2",
"mean_EXT4",
"mean_EXT6",
"mean_EXT8",
"mean_EXT10",
"mean_EST1",
"mean_EST3",
"mean_EST4",
"mean_EST5",
"mean_EST6",
"mean_EST7",
"mean_EST8",
"mean_EST9",
"mean_EST10",
"mean_AGR1",
"mean_AGR3",
"mean_AGR5",
"mean_AGR7",
"mean_CSN2",
"mean_CSN4",
"mean_CSN6",
"mean_CSN8",
"mean_OPN2",
"mean_OPN4",
"mean_OPN6"
)
# get cluster means
# multiply reversed statement with -1 for further grouping along row axis
k.means <- clustered %>%
group_by(k.means.cluster) %>%
summarise(across(1:50, list(mean = mean), .names = "mean_{.col}")) %>%
mutate(across(all_of(negatives), ~ .x * (-1)))
# get personality wise row sums (this is considering reversed statements)
k.means$sum_EXT = rowSums(k.means %>% select(contains("EXT")))
k.means$sum_EST = rowSums(k.means %>% select(contains("EST")))
k.means$sum_ARG = rowSums(k.means %>% select(contains("AGR")))
k.means$sum_CSN = rowSums(k.means %>% select(contains("CSN")))
k.means$sum_OPN = rowSums(k.means %>% select(contains("OPN")))
# analyse sums column for labelling
k.means = k.means %>% select(contains("sum"))
seq(-20, 20, by = 10)
seq(-44, 4, by = 12)
seq(-14, 26, by = 10)
seq(-14, 26, by = 10)
seq(-8, 32, by = 10)
# Extrovert, Stable, Agreeable, Conscious, Open
# "33333" = 'Average'
# "23434" = 'ReservedCompassionateCurious'
# "34434" = 'StableCompassionateCurious'
# "33444" = 'CompassionateCuriousOrganized'
# replace cluster numbers with labels
clustered$k.means.cluster[clustered$k.means.cluster == 1] <-
"Neutral"
clustered$k.means.cluster[clustered$k.means.cluster == 2] <-
"ReservedCompassionateCurious"
clustered$k.means.cluster[clustered$k.means.cluster == 3] <-
"StableCompassionateCurious"
clustered$k.means.cluster[clustered$k.means.cluster == 4] <-
"CompassionateCuriousOrganized"
# plot cluster sizes with new cluster labels
ggplot(clustered,
aes(k.means.cluster, fill = k.means.cluster)) +
geom_bar() +
ggtitle("Cluster Sizes") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "None")
# Prediction of Unseen Subset ----
# get sample of unseen people from population
all.people = 1:nrow(survey.results)
unseen.people = all.people[!all.people %in% sample.people]
# predictions on 100 people (randomly decided)
unseen.people <-
sample(unseen.people, 100)
survey.results.unseen <- survey.results[unseen.people, ]
# use Euclidean distance with cluster centroids to assign nearest cluster
predicted.cluster <- rep(0, 100)
survey.results.unseen <-
cbind(survey.results.unseen, predicted.cluster)
for (person in row.names(survey.results.unseen)) {
distance = c(dist(rbind(
survey.results.unseen[person,], km.output$centers[1,]
)),
dist(rbind(
survey.results.unseen[person,], km.output$centers[2,]
)),
dist(rbind(
survey.results.unseen[person,], km.output$centers[3,]
)),
dist(rbind(
survey.results.unseen[person,], km.output$centers[4,]
)))
survey.results.unseen[person, "predicted.cluster"] = which.min(distance)
}
survey.results.unseen$predicted.cluster[survey.results.unseen$predicted.cluster == 1] <-
"Neutral"
survey.results.unseen$predicted.cluster[survey.results.unseen$predicted.cluster == 2] <-
"ReservedCompassionateCurious"
survey.results.unseen$predicted.cluster[survey.results.unseen$predicted.cluster == 3] <-
"StableCompassionateCurious"
survey.results.unseen$predicted.cluster[survey.results.unseen$predicted.cluster == 4] <-
"CompassionateCuriousOrganized"
# plot cluster assignments
ggplot(survey.results.unseen,
aes(predicted.cluster, fill = predicted.cluster)) +
geom_bar() +
ggtitle("Label Assignments for Unseen Sample") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "None")