-
Notifications
You must be signed in to change notification settings - Fork 3
/
kmeans-funcs.R
498 lines (445 loc) · 13.2 KB
/
kmeans-funcs.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#' K-Means Functions
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description
#' Takes in a data.frame/tibble and transforms it into an aggregated/normalized
#' user-item tibble of proportions. The user will need to input the parameters
#' for the rows/user and the columns/items.
#'
#' @details This function should be used before using a k-mean model. This is
#' commonly referred to as a user item matrix because "users" tend to be on the
#' rows and "items" (e.g. orders) on the columns. You must supply a column that
#' can be summed for the aggregation and normalization process to occur.
#'
#' @param .data The data that you want to transform
#' @param .row_input The column that is going to be the row (user)
#' @param .col_input The column that is going to be the column (item)
#' @param .record_input The column that is going to be summed up for the aggregattion
#' and normalization process.
#'
#' @examples
#' library(healthyR.data)
#' library(dplyr)
#'
#' data_tbl <- healthyR_data%>%
#' filter(ip_op_flag == "I") %>%
#' filter(payer_grouping != "Medicare B") %>%
#' filter(payer_grouping != "?") %>%
#' select(service_line, payer_grouping) %>%
#' mutate(record = 1) %>%
#' as_tibble()
#'
#' kmeans_user_item_tbl(
#' .data = data_tbl
#' , .row_input = service_line
#' , .col_input = payer_grouping
#' , .record_input = record
#' )
#'
#' @return
#' A aggregated/normalized user item tibble
#'
#' @export
#'
kmeans_user_item_tbl <- function(.data, .row_input, .col_input, .record_input){
# * Tidyeval ----
row_input_var_expr <- rlang::enquo(.row_input)
col_input_var_expr <- rlang::enquo(.col_input)
rec_input_var_expr <- rlang::enquo(.record_input)
# * Checks ----
if(!is.data.frame(.data)){
stop(call. = FALSE, "(.data) is not a data.frame/tibble. Please supply.")
}
if(rlang::quo_is_missing(row_input_var_expr)){
stop(call. = FALSE, "You must supply a user/row input.")
}
if(rlang::quo_is_missing(col_input_var_expr)){
stop(call. = FALSE, "You must supply an item/column input")
}
if(rlang::quo_is_missing(rec_input_var_expr)){
stop(call. = FALSE, "You must supply a record column that can be summed up
for the aggregation and normalization process.")
}
# * Data ----
data_tbl <- tibble::as_tibble(.data)
# * Manipulate ----
data_summarized_tbl <- data_tbl %>%
dplyr::group_by({{ row_input_var_expr }}, {{ col_input_var_expr }}) %>%
dplyr::summarise(total_records = sum({{ rec_input_var_expr }}, na.rm = TRUE)) %>%
dplyr::ungroup() %>%
# Normalize proportions
dplyr::group_by({{ row_input_var_expr }}) %>%
dplyr::mutate(prop_of_total = total_records / sum(total_records)) %>%
dplyr::ungroup()
# User/Item format
user_item_tbl <- data_summarized_tbl %>%
dplyr::select({{ row_input_var_expr }}, {{ col_input_var_expr }}, prop_of_total) %>%
dplyr::mutate(prop_of_total = base::ifelse(
base::is.na(prop_of_total), 0, prop_of_total
)) %>%
tidyr::pivot_wider(
names_from = {{ col_input_var_expr }}
, values_from = prop_of_total
, values_fill = list(prop_of_total = 0)
)
# * Return ----
return(user_item_tbl)
}
#' K-Means Functions
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description
#' Takes the output of the [kmeans_user_item_tbl()] function and applies the
#' k-means algorithm to it using [stats::kmeans()]
#'
#' @details Uses the [stats::kmeans()] function and creates a wrapper around it.
#'
#' @param .data The data that gets passed from [kmeans_user_item_tbl()]
#' @param .centers How many initial centers to start with
#'
#' @examples
#' library(healthyR.data)
#' library(dplyr)
#'
#' data_tbl <- healthyR_data%>%
#' filter(ip_op_flag == "I") %>%
#' filter(payer_grouping != "Medicare B") %>%
#' filter(payer_grouping != "?") %>%
#' select(service_line, payer_grouping) %>%
#' mutate(record = 1) %>%
#' as_tibble()
#'
#' kmeans_user_item_tbl(
#' .data = data_tbl
#' , .row_input = service_line
#' , .col_input = payer_grouping
#' , .record_input = record
#' ) %>%
#' kmeans_obj()
#'
#' @return
#' A stats k-means object
#'
#' @export
#'
kmeans_obj <- function(.data, .centers = 5){
# * Tidyeval ----
centers_var_expr <- .centers
# * Checks ----
if(!is.data.frame(.data)){
stop(call. = FALSE("(.data) is missing. Please supply."))
}
# Default to 5
if(is.null(centers_var_expr)){centers_var_expr = 5}
# * Data ----
data <- tibble::as_tibble(.data)
# * k-means ----
kmeans_tbl <- data %>%
dplyr::select(-1)
kmeans_obj <- kmeans_tbl %>%
stats::kmeans(
centers = centers_var_expr
, nstart = 100
)
return(kmeans_obj)
}
#' K-Means tidy Functions
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description
#' K-Means tidy functions
#'
#' @details
#' Takes in a k-means object and its associated user item tibble and then
#' returns one of the items asked for. Either: [broom::tidy()], [broom::glance()]
#' or [broom::augment()]. The function defaults to [broom::tidy()].
#'
#' @param .kmeans_obj A [stats::kmeans()] object
#' @param .tidy_type "tidy","glance", or "augment"
#' @param .data The user item tibble created from [kmeans_user_item_tbl()]
#'
#' @examples
#' library(healthyR.data)
#' library(dplyr)
#' library(broom)
#'
#' data_tbl <- healthyR_data%>%
#' filter(ip_op_flag == "I") %>%
#' filter(payer_grouping != "Medicare B") %>%
#' filter(payer_grouping != "?") %>%
#' select(service_line, payer_grouping) %>%
#' mutate(record = 1) %>%
#' as_tibble()
#'
#' uit_tbl <- kmeans_user_item_tbl(
#' .data = data_tbl
#' , .row_input = service_line
#' , .col_input = payer_grouping
#' , .record_input = record
#' )
#'
#' km_obj <- kmeans_obj(uit_tbl)
#'
#' kmeans_tidy_tbl(
#' .kmeans_obj = km_obj
#' , .data = uit_tbl
#' , .tidy_type = "augment"
#' )
#'
#' kmeans_tidy_tbl(
#' .kmeans_obj = km_obj
#' , .data = uit_tbl
#' , .tidy_type = "glance"
#' )
#'
#' kmeans_tidy_tbl(
#' .kmeans_obj = km_obj
#' , .data = uit_tbl
#' , .tidy_type = "tidy"
#' ) %>%
#' glimpse()
#'
#' @return
#' A tibble
#'
#' @export
#'
kmeans_tidy_tbl <- function(.kmeans_obj, .data, .tidy_type = "tidy") {
# * Tidyeval ----
kmeans_obj <- .kmeans_obj
tidy_type <- .tidy_type
# * Checks ----
if(!is.data.frame(.data)){
stop(call. = FALSE, "(.user_item_data) is not a data.frame/tibble, please supply original user item tibble.")
}
if (!inherits(x = kmeans_obj, what = "kmeans")){
stop(call. = FALSE, "(.kmeans_obj) is not of class 'kmeans'")
}
# if (!class(kmeans_obj) == "kmeans") {
# stop(call. = FALSE, "(.kmeans_obj) is not of class 'kmeans'")
# }
if (!tidy_type %in% c("tidy", "augment", "glance")) {
stop(call. = FALSE,
"(.tidy_type) must be either tidy, glance, or augment")
}
# * Manipulate ----
uit_tbl <- tibble::as_tibble(.data)
row_col <- colnames(uit_tbl[1])
if (tidy_type == "tidy") {
km_tbl <- kmeans_obj %>% broom::tidy()
} else if (tidy_type == "glance") {
km_tbl <- kmeans_obj %>% broom::glance()
} else if (tidy_type == "augment") {
km_tbl <- kmeans_obj %>%
broom::augment(uit_tbl) %>%
dplyr::select(row_col, .cluster) %>%
dplyr::rename("cluster" = .cluster)
}
# * Return ----
return(km_tbl)
}
#' K-Means Mapper
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description Create a tibble that maps the [kmeans_obj()] using [purrr::map()]
#' to create a nested data.frame/tibble that holds n centers. This tibble will be
#' used to help create a scree plot.
#'
#' @seealso
#' \url{https://en.wikipedia.org/wiki/Scree_plot}
#'
#' @details Takes in a single parameter of .centers. This is used to create the tibble
#' and map the [kmeans_obj()] function down the list creating a nested tibble.
#'
#' @param .centers How many different centers do you want to try
#' @param .data You must have a tibble in the working environment from the
#' [kmeans_user_item_tbl()]
#'
#' @examples
#' library(healthyR.data)
#' library(dplyr)
#'
#' data_tbl <- healthyR_data%>%
#' filter(ip_op_flag == "I") %>%
#' filter(payer_grouping != "Medicare B") %>%
#' filter(payer_grouping != "?") %>%
#' select(service_line, payer_grouping) %>%
#' mutate(record = 1) %>%
#' as_tibble()
#'
#' ui_tbl <- kmeans_user_item_tbl(
#' .data = data_tbl
#' , .row_input = service_line
#' , .col_input = payer_grouping
#' , .record_input = record
#' )
#'
#' kmeans_mapped_tbl(ui_tbl)
#'
#' @return
#' A nested tibble
#'
#' @export
#'
kmeans_mapped_tbl <- function(.data, .centers = 15){
# * Tidyeval ----
centers_var_expr <- .centers
# * Checks ----
if(!is.data.frame(.data)){
stop(call. = FALSE, "(.data) is not a data.frame/tibble. Please supply.")
}
input_data <- tibble::as_tibble(.data)
km_mapper <- function(centers = 3){
input_data %>%
dplyr::select(-1) %>%
stats::kmeans(
centers = centers
, nstart = 100
)
}
# * Manipulate ----
data_tbl <- tibble::tibble(centers = 1:centers_var_expr) %>%
dplyr::mutate(k_means = centers %>%
purrr::map(km_mapper)
) %>%
dplyr::mutate(glance = k_means %>%
purrr::map(broom::glance))
# * Return ----
return(data_tbl)
}
#' K-Means Scree Plot Data Table
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description Take data from the [kmeans_mapped_tbl()] and unnest it into a
#' tibble for inspection and for use in the [kmeans_scree_plt()] function.
#'
#' @details Takes in a single parameter of .data from [kmeans_mapped_tbl()] and
#' transforms it into a tibble that is used for [kmeans_scree_plt()]. It will
#' show the values (tot.withinss) at each center.
#'
#' @param .data You must have a tibble in the working environment from the
#' [kmeans_mapped_tbl()]
#'
#' @examples
#' library(healthyR.data)
#' library(dplyr)
#'
#' data_tbl <- healthyR_data%>%
#' filter(ip_op_flag == "I") %>%
#' filter(payer_grouping != "Medicare B") %>%
#' filter(payer_grouping != "?") %>%
#' select(service_line, payer_grouping) %>%
#' mutate(record = 1) %>%
#' as_tibble()
#'
#' ui_tbl <- kmeans_user_item_tbl(
#' .data = data_tbl
#' , .row_input = service_line
#' , .col_input = payer_grouping
#' , .record_input = record
#' )
#'
#' kmm_tbl <- kmeans_mapped_tbl(ui_tbl)
#'
#' kmeans_scree_data_tbl(kmm_tbl)
#'
#' @return
#' A nested tibble
#'
#' @export
#'
kmeans_scree_data_tbl <- function(.data) {
# * Checks ----
if(!is.data.frame(.data)){
stop(call. = FALSE, "(.data) is not a data.frame/tibble. Please supply.")
}
# * Manipulate ----
data_tbl <- tibble::as_tibble(.data)
data_tbl <- data_tbl %>%
tidyr::unnest(glance) %>%
dplyr::select(centers, tot.withinss)
# * Return ----
return(data_tbl)
}
#' K-Means Scree Plot
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description Create a scree-plot from the [kmeans_mapped_tbl()] function.
#'
#' @details Outputs a scree-plot
#'
#' @seealso
#' \url{https://en.wikipedia.org/wiki/Scree_plot}
#'
#' @param .data The data from the [kmeans_mapped_tbl()] function
#'
#' @examples
#' library(healthyR.data)
#' library(dplyr)
#'
#' data_tbl <- healthyR_data%>%
#' filter(ip_op_flag == "I") %>%
#' filter(payer_grouping != "Medicare B") %>%
#' filter(payer_grouping != "?") %>%
#' select(service_line, payer_grouping) %>%
#' mutate(record = 1) %>%
#' as_tibble()
#'
#' ui_tbl <- kmeans_user_item_tbl(
#' .data = data_tbl
#' , .row_input = service_line
#' , .col_input = payer_grouping
#' , .record_input = record
#' )
#'
#' kmm_tbl <- kmeans_mapped_tbl(ui_tbl)
#'
#' kmeans_scree_plt(.data = kmm_tbl)
#'
#' @return
#' A ggplot2 plot
#'
#' @export
#'
kmeans_scree_plt <- function(.data){
# * Checks ----
if(!is.data.frame(.data)){
stop(call. = FALSE,"(.data) is not a data.frame/tibble. Please supply.")
}
# * Manipulate ----
data_tbl <- tibble::as_tibble(.data)
data_tbl <- data_tbl %>%
tidyr::unnest(glance) %>%
dplyr::select(centers, tot.withinss)
# * Plot
p <- data_tbl %>%
ggplot2::ggplot(
mapping = ggplot2::aes(
x = centers
, y = tot.withinss
)
) +
ggplot2::geom_point() +
ggplot2::geom_line() +
# Kaiser Line
ggplot2::geom_hline(yintercept = 1, color = "red", linetype = "dashed") +
ggrepel::geom_label_repel(
mapping = ggplot2::aes(
label = centers
)) +
ggplot2::theme_minimal() +
ggplot2::labs(
title = "Scree Plot"
, subtitle = "Measures the distance each of the users are from the closest k-means cluster"
, y = "Total Within Sum of Squares"
, x = "Centers"
)
# * Return ----
return(p)
}