| 1 |
#' @templateVar class pam |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x An `pam` object returned from [cluster::pam()] |
| 5 |
#' @param col.names Column names in the input data frame. |
| 6 |
#' Defaults to the names of the variables in x. |
| 7 |
#' @template param_unused_dots |
| 8 |
#' |
| 9 |
#' @evalRd return_tidy( |
| 10 |
#' size = "Size of each cluster.", |
| 11 |
#' max.diss = "Maximal dissimilarity between the observations in the |
| 12 |
#' cluster and that cluster's medoid.", |
| 13 |
#' avg.diss = "Average dissimilarity between the observations in the |
| 14 |
#' cluster and that cluster's medoid.", |
| 15 |
#' diameter = "Diameter of the cluster.", |
| 16 |
#' separation = "Separation of the cluster.", |
| 17 |
#' avg.width = "Average silhouette width of the cluster.", |
| 18 |
#' cluster = "A factor describing the cluster from 1:k." |
| 19 |
#' ) |
| 20 |
#' |
| 21 |
#' @details For examples, see the pam vignette. |
| 22 |
#' |
| 23 |
#' @aliases pam_tidiers |
| 24 |
#' @export |
| 25 |
#' @seealso [tidy()], [cluster::pam()] |
| 26 |
#' @family pam tidiers |
| 27 |
# skip running examples - occasionally over CRAN check time limit |
| 28 |
#' @examplesIf rlang::is_installed(c("cluster", "modeldata", "ggplot2")) && identical(Sys.getenv("NOT_CRAN"), "true") |
| 29 |
#' |
| 30 |
#' # load libraries for models and data |
| 31 |
#' library(dplyr) |
| 32 |
#' library(ggplot2) |
| 33 |
#' library(cluster) |
| 34 |
#' library(modeldata) |
| 35 |
#' data(hpc_data) |
| 36 |
#' |
| 37 |
#' x <- hpc_data[, 2:5] |
| 38 |
#' p <- pam(x, k = 4) |
| 39 |
#' |
| 40 |
#' # summarize model fit with tidiers + visualization |
| 41 |
#' tidy(p) |
| 42 |
#' glance(p) |
| 43 |
#' augment(p, x) |
| 44 |
#' |
| 45 |
#' augment(p, x) |> |
| 46 |
#' ggplot(aes(compounds, input_fields)) + |
| 47 |
#' geom_point(aes(color = .cluster)) + |
| 48 |
#' geom_text(aes(label = cluster), data = tidy(p), size = 10) |
| 49 |
#' |
| 50 |
tidy.pam <- function(x, col.names = paste0("x", 1:ncol(x$medoids)), ...) { |
| 51 |
as_tibble(x$clusinfo) |> |
| 52 |
mutate( |
| 53 |
avg.width = x$silinfo$clus.avg.widths, |
| 54 |
cluster = as.factor(row_number()) |
| 55 |
) |> |
| 56 |
bind_cols(as_tibble(x$medoids)) |> |
| 57 |
rename( |
| 58 |
"max.diss" = "max_diss", |
| 59 |
"avg.diss" = "av_diss" |
| 60 |
) |
| 61 |
} |
| 62 |
|
| 63 |
#' @templateVar class pam |
| 64 |
#' @template title_desc_augment |
| 65 |
#' |
| 66 |
#' @inherit tidy.pam params examples |
| 67 |
#' @template param_data |
| 68 |
#' |
| 69 |
#' @evalRd return_augment(".cluster") |
| 70 |
#' |
| 71 |
#' @export |
| 72 |
#' @seealso [augment()], [cluster::pam()] |
| 73 |
#' @family pam tidiers |
| 74 |
#' |
| 75 |
augment.pam <- function(x, data = NULL, ...) { |
| 76 |
check_ellipses("newdata", "augment", "pam", ...) |
| 77 |
|
| 78 |
if (is.null(data)) { |
| 79 |
data <- x$data |
| 80 |
} |
| 81 |
|
| 82 |
as_augment_tibble(data) |> |
| 83 |
mutate(.cluster = as.factor(!!x$clustering)) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
#' @templateVar class pam |
| 88 |
#' @template title_desc_glance |
| 89 |
#' |
| 90 |
#' @inherit tidy.pam params examples |
| 91 |
#' |
| 92 |
#' @evalRd return_glance("avg.silhouette.width") |
| 93 |
#' |
| 94 |
#' @export |
| 95 |
#' @seealso [glance()], [cluster::pam()] |
| 96 |
#' @family pam tidiers |
| 97 |
glance.pam <- function(x, ...) { |
| 98 |
as_glance_tibble( |
| 99 |
avg.silhouette.width = x$silinfo$avg.width, |
| 100 |
na_types = "r" |
| 101 |
) |
| 102 |
} |