-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscript.R
More file actions
190 lines (155 loc) · 6.05 KB
/
script.R
File metadata and controls
190 lines (155 loc) · 6.05 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
library(magrittr)
library(d3heatmap)
library(ggplot2)
library(kmed) # k-medoids
library(kernlab) # spectral clustering
library(amap) # Kmeans
library(factoextra) #fviz_cluster
consKmeans <- function(X, K, method){
out.clust <- rep(0, nrow(X))
subs <- sample(x = 1:nrow(X), size = 0.8*nrow(X) %>% round(), replace = FALSE)
kmeans.out <- Kmeans(X[subs,], centers = K, iter.max = 100,
nstart = 5, method = method)
kmeans.clust <- kmeans.out$cluster
out.clust[subs] <- kmeans.clust
return(out.clust)
}
consHclust <- function(X, K, method, linkage){
out.clust <- rep(0, nrow(X))
subs <- sample(x = 1:nrow(X), size = 0.8*nrow(X) %>% round(), replace = FALSE)
dist.mat <- dist(X[subs,], method = method)
hier.out <- hclust(dist.mat, method = linkage)
hier.clust <- cutree(hier.out, k = K)
out.clust[subs] <- hier.clust
return(out.clust)
}
consKmed <- function(X, K, method){
out.clust <- rep(0, nrow(X))
subs <- sample(x = 1:nrow(X), size = 0.8*nrow(X) %>% round(), replace = FALSE)
dist.mat <- dist(X[subs, ], method = method)
kmed.out <- fastkmed(dist.mat, ncluster = K, iterate = 50)
kmed.clust <- kmed.out$cluster
out.clust[subs] <- kmed.clust
return(out.clust)
}
consSpec <- function(X, K){
out.clust <- rep(0, nrow(X))
subs <- sample(x = 1:nrow(X), size = 0.8*nrow(X) %>% round(), replace = FALSE)
spec.out <- specc(x = X[subs,], centers = K,
iterations = 50)
spec.clust <- spec.out@.Data
out.clust[subs] <- spec.clust
return(out.clust)
}
# Generates a 4x4 scatterplot matrix of the
# first four PCs of a numeric matrix and colors
# points by cluster assignment
genPCPlots <- function(mat, cluster){
pr.out <- prcomp(mat)
out <- data.frame(pr.out$x[,1:4], cluster = as.factor(cluster))
pairs(pr.out$x[,1:4], col = unlist(cluster))
}
# Generates a biplot for the first two
# PCs and the cluster assignments for
# all points
genBiPCPlot <- function(mat, cluster){
fviz_cluster(object = list(data = mat,
cluster = cluster),
main = "Clustered PC1 and PC2", subtitle = "Highlight and double-click to zoom")
}
consClustering <- function(X, K, func, nrs, ...){
df <- numeric(0)
for(i in 1:nrs){
df <- switch(func,
kmeans = cbind(df, consKmeans(X, K, ...)),
hier = cbind(df, consHclust(X, K, ...)),
kmed = cbind(df, consKmed(X, K, ...)),
spec = cbind(df, consSpec(X, K)))
}
n <- nrow(X)
# Normalized pairs divides all entries by number of iterations
norm_pairs <- matrix(0, nrow = n, ncol = n, dimnames = list(rownames(X), rownames(X)))
# Regular pairs just counts number of times clustered together
pairs <- matrix(0, nrow = n, ncol = n, dimnames = list(rownames(X), rownames(X)))
for (i in 1:n) {
for (j in 1:n) {
# Looking at two observations and their cluster assignments
pair <- df[c(i,j), df[i,]!=0 & df[j,]!=0, drop = FALSE]
# Total times the two obs were clustered similarly in the same iter
aggr <- sum(pair[1,] == pair[2,])
# Total times both obs existed in iter
denom <- ncol(pair)
# If neither of them appeared in iter together
if(denom == 0) denom <- 1
pairs[i,j] <- aggr
norm_pairs[i,j] <- aggr/ denom
}
}
# Clustering consensus matrix
dist <- as.dist(1-norm_pairs)
hier.out <- hclust(dist, method = "ward.D2")
hier.clust <- cutree(hier.out, k = K)
# normalized matrix reordered
reorder.npairs <- norm_pairs[order(hier.clust), order(hier.clust)]
# regular, counting matrix reordered
reorder.pairs <- pairs[order(hier.clust), order(hier.clust)]
return(list(norm.mat = reorder.npairs,
clusterings = hier.clust,
count.mat = reorder.pairs))
}
computeClustCons <- function(clusters, cons.mat){
num_clust <- unique(clusters) %>% length()
consensus_metrics <- rep(0, num_clust)
for(i in 1:num_clust){
clust_names <- names(clusters)[which(clusters == i)] # names of obs in current cluster
mat_idxs <- which(colnames(cons.mat) %in% clust_names) # matrix indeces of current
clust.mat <- cons.mat[mat_idxs, mat_idxs]
# Grabbing upper-triangle entries, excluding diagonal
up.t <- clust.mat[upper.tri(clust.mat, diag = FALSE)]
if(length(up.t) <= 1){
consensus_metrics[i] <- 0
}else{
consensus_metrics[i] <- mean(up.t)
}
}
return(ggplot() +
geom_bar(mapping = aes(x = as.factor(1:num_clust),
y = consensus_metrics),
fill = "steelblue",
stat = "identity") +
theme_light() +
labs(y = "Cluster Consensus", x = "Cluster Number",
title = "Cluster Consensus For Each Cluster Grouping"))
}
computeItemCons <- function(clusters, cons.mat){
obs_names <- colnames(cons.mat)
item_cons <- rep(0, length(obs_names))
num_clust <- unique(clusters) %>% length()
for(i in 1:num_clust){
clust_names <- names(clusters)[which(clusters == i)] # names of obs in current cluster
mat_idxs <- which(colnames(cons.mat) %in% clust_names) # matrix indeces of current
clust.mat <- cons.mat[mat_idxs, mat_idxs]
if(length(clust.mat) <=1){
item_cons[which(obs_names %in% clust_names)] <- 0
}else{
# zeroing matrix
diag(clust.mat) <- 0
for(col in 1:ncol(clust.mat)){
curr_name <- colnames(clust.mat)[col]
curr_mean <- clust.mat[,col] %>% mean()
item_cons[which(obs_names == curr_name)] <- curr_mean
}
}
}
out.df <- data.frame(Songs = obs_names,
Item_Consensus = item_cons)
return(out.df[order(out.df$Item_Consensus, decreasing = TRUE),])
}
computeDistribution <- function(cons.mat){
upper_t <- cons.mat[upper.tri(cons.mat, diag = FALSE)] %>% unlist()
return(ggplot() +
geom_density(mapping=aes(upper_t), fill = "steelblue") +
theme_light() +
labs(x = "Consensus Index Value", y = "Density",
title = "Consensus Distribution"))
}