-
Notifications
You must be signed in to change notification settings - Fork 14
/
predict_helpers.R
154 lines (122 loc) · 4.21 KB
/
predict_helpers.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
make_predictions <- function(x, prefix, n_clusters) {
levels <- seq_len(n_clusters)
factor(x, levels = levels, labels = paste0(prefix, levels))
}
.k_means_predict_stats <- function(object, new_data, prefix = "Cluster_") {
res <- object$centers
res <- flexclust::dist2(res, new_data)
res <- apply(res, 2, which.min)
make_predictions(res, prefix, length(object$size))
}
.k_means_predict_ClusterR <- function(object, new_data, prefix = "Cluster_") {
clusters <- predict(object, new_data)
n_clusters <- length(object$obs_per_cluster)
make_predictions(clusters, prefix, n_clusters)
}
.k_means_predict_clustMixType <- function(object, new_data, prefix = "Cluster_") {
clusters <- predict(object, new_data)$cluster
n_clusters <- length(object$size)
make_predictions(clusters, prefix, n_clusters)
}
.k_means_predict_klaR <- function(object, new_data, prefix = "Cluster_",
ties = c("first", "last", "random")) {
ties <- rlang::arg_match(ties)
modes <- object$modes
n_modes <- nrow(modes)
clusters <- integer(nrow(new_data))
modes <- as.matrix(modes)
new_data <- as.matrix(new_data)
for (i in seq_along(clusters)) {
misses <- rowSums(new_data[rep(i, n_modes), ] != modes)
which_min <- which(misses == min(misses))
if (length(which_min) == 1) {
clusters[i] <- which_min
} else {
clusters[i] <- switch(
ties,
first = which_min[1],
last = which_min[length(which_min)],
random = sample(which_min, 1)
)
}
}
make_predictions(clusters, prefix, n_modes)
}
.hier_clust_predict_stats <- function(object, new_data, ..., prefix = "Cluster_") {
linkage_method <- object$method
new_data <- as.matrix(new_data)
training_data <- as.matrix(attr(object, "training_data"))
clusters <- extract_cluster_assignment(
object,
...,
prefix = prefix,
call = call("predict")
)
if (linkage_method %in% c("single", "complete", "average", "median")) {
## complete, single, average, and median linkage_methods are basically the
## same idea, just different summary distance to cluster
cluster_dist_fun <- switch(linkage_method,
"single" = min,
"complete" = max,
"average" = mean,
"median" = stats::median
)
# need this to be obs on rows, dist to new data on cols
dists_new <- Rfast::dista(xnew = training_data, x = new_data, trans = TRUE)
cluster_dists <- dplyr::bind_cols(data.frame(dists_new), clusters) %>%
dplyr::group_by(.cluster) %>%
dplyr::summarize_all(cluster_dist_fun)
pred_clusts_num <- cluster_dists %>%
dplyr::select(-.cluster) %>%
map_dbl(which.min)
} else if (linkage_method == "centroid") {
## Centroid linkage_method, dist to center
cluster_centers <- extract_centroids(object) %>% dplyr::select(-.cluster)
dists_means <- Rfast::dista(new_data, cluster_centers)
pred_clusts_num <- apply(dists_means, 1, which.min)
} else if (linkage_method %in% c("ward.D", "ward", "ward.D2")) {
## Ward linkage_method: lowest change in ESS
## dendrograms created from already-squared distances
## use Ward.D2 on these plain distances for Ward.D
cluster_centers <- extract_centroids(object)
n_clust <- nrow(cluster_centers)
cluster_names <- cluster_centers[[1]]
cluster_centers <- as.matrix(cluster_centers[, -1])
d_means <- map(
seq_len(n_clust),
~ t(
t(training_data[clusters$.cluster == cluster_names[.x], ]) -
cluster_centers[.x, ]
)
)
d_new_list <- map(
seq_len(nrow(new_data)),
function(new_obs) {
map(
seq_len(n_clust),
~ t(t(training_data[clusters$.cluster == cluster_names[.x], ])
- new_data[new_obs, ])
)
}
)
n <- nrow(training_data)
change_in_ess <- map(
d_new_list,
function(v) {
map2_dbl(
d_means, v,
~ sum((n * .x + .y)^2 / (n + 1)^2 - .x^2)
)
}
)
pred_clusts_num <- map_dbl(change_in_ess, which.min)
} else {
rlang::abort(
glue::glue(
"linkage_method {linkage_method} is not supported for prediction."
)
)
}
pred_clusts <- unique(clusters$.cluster)[pred_clusts_num]
pred_clusts
}