| 1 |
#' @templateVar class Mclust |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x An `Mclust` object return from [mclust::Mclust()]. |
| 5 |
#' @template param_unused_dots |
| 6 |
#' |
| 7 |
#' @evalRd return_tidy( |
| 8 |
#' "size", |
| 9 |
#' "proportion", |
| 10 |
#' mean = "The mean for each component. In case of 2+ dimensional models, |
| 11 |
#' a column with the mean is added for each dimension. NA for noise |
| 12 |
#' component", |
| 13 |
#' variance = "In case of one-dimensional and spherical models, |
| 14 |
#' the variance for each component, omitted otherwise. NA for noise |
| 15 |
#' component", |
| 16 |
#' component = "Cluster id as a factor." |
| 17 |
#' ) |
| 18 |
#' |
| 19 |
#' @examplesIf rlang::is_installed("mclust") |
| 20 |
#' |
| 21 |
#' # load library for models and data |
| 22 |
#' library(mclust) |
| 23 |
#' |
| 24 |
#' # load data manipulation libraries |
| 25 |
#' library(dplyr) |
| 26 |
#' library(tibble) |
| 27 |
#' library(purrr) |
| 28 |
#' library(tidyr) |
| 29 |
#' |
| 30 |
#' set.seed(27) |
| 31 |
#' |
| 32 |
#' centers <- tibble( |
| 33 |
#' cluster = factor(1:3), |
| 34 |
#' # number points in each cluster |
| 35 |
#' num_points = c(100, 150, 50), |
| 36 |
#' # x1 coordinate of cluster center |
| 37 |
#' x1 = c(5, 0, -3), |
| 38 |
#' # x2 coordinate of cluster center |
| 39 |
#' x2 = c(-1, 1, -2) |
| 40 |
#' ) |
| 41 |
#' |
| 42 |
#' points <- centers |> |
| 43 |
#' mutate( |
| 44 |
#' x1 = map2(num_points, x1, rnorm), |
| 45 |
#' x2 = map2(num_points, x2, rnorm) |
| 46 |
#' ) |> |
| 47 |
#' select(-num_points, -cluster) |> |
| 48 |
#' unnest(c(x1, x2)) |
| 49 |
#' |
| 50 |
#' # fit model |
| 51 |
#' m <- Mclust(points) |
| 52 |
#' |
| 53 |
#' # summarize model fit with tidiers |
| 54 |
#' tidy(m) |
| 55 |
#' augment(m, points) |
| 56 |
#' glance(m) |
| 57 |
#' |
| 58 |
#' @export |
| 59 |
#' @aliases mclust_tidiers |
| 60 |
#' @seealso [tidy()], [mclust::Mclust()] |
| 61 |
#' @family mclust tidiers |
| 62 |
#' |
| 63 |
tidy.Mclust <- function(x, ...) { |
| 64 |
np <- max(x$G, length(table(x$classification))) |
| 65 |
ret <- data.frame(seq_len(np)) |
| 66 |
colnames(ret) <- c("component") |
| 67 |
if (x$G < np) ret$component <- ret$component - 1 |
| 68 |
ret$size <- sapply(seq(1, np), function(c) { |
| 69 |
sum(x$classification == c) |
| 70 |
}) |
| 71 |
ret$proportion <- x$parameters$pro |
| 72 |
if (x$modelName %in% c("E", "V", "EII", "VII")) { |
| 73 |
ret$variance <- rep_len(x$parameters$variance$sigmasq, length.out = x$G) |
| 74 |
} |
| 75 |
if (dim(as.matrix(x$parameters$mean))[2] > 1) { |
| 76 |
mean <- t(x$parameters$mean) |
| 77 |
} else if (is.null(dim(x$parameters$mean))) { |
| 78 |
mean <- as.matrix(x$parameters$mean) |
| 79 |
} else { |
| 80 |
mean <- t(as.matrix(x$parameters$mean)) |
| 81 |
} |
| 82 |
ret <- cbind(ret, mean = rbind(matrix(, np - nrow(mean), ncol(mean)), mean)) |
| 83 |
as_tibble(ret) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
#' @templateVar class Mclust |
| 88 |
#' @template title_desc_augment |
| 89 |
#' |
| 90 |
#' @inherit tidy.Mclust params examples |
| 91 |
#' @template param_data |
| 92 |
#' |
| 93 |
#' @evalRd return_augment( |
| 94 |
#' .fitted = FALSE, |
| 95 |
#' .resid = FALSE, |
| 96 |
#' ".class", |
| 97 |
#' ".uncertainty" |
| 98 |
#' ) |
| 99 |
#' |
| 100 |
#' @export |
| 101 |
#' @seealso [augment()], [mclust::Mclust()] |
| 102 |
#' @family mclust tidiers |
| 103 |
#' |
| 104 |
augment.Mclust <- function(x, data = NULL, ...) { |
| 105 |
check_ellipses("newdata", "augment", "Mclust", ...) |
| 106 |
|
| 107 |
if (is.null(data)) { |
| 108 |
data <- x$data |
| 109 |
} else if (!(is.data.frame(data) || is.matrix(data))) { |
| 110 |
cli::cli_abort("{.arg data} must be a data frame or matrix.") |
| 111 |
} |
| 112 |
|
| 113 |
as_augment_tibble(data) |> |
| 114 |
mutate( |
| 115 |
.class = as.factor(!!x$classification), |
| 116 |
.uncertainty = !!x$uncertainty |
| 117 |
) |
| 118 |
} |
| 119 |
|
| 120 |
#' @templateVar class Mclust |
| 121 |
#' @template title_desc_glance |
| 122 |
#' |
| 123 |
#' @inherit tidy.Mclust params examples |
| 124 |
#' |
| 125 |
#' @evalRd return_glance( |
| 126 |
#' "BIC", |
| 127 |
#' "logLik", |
| 128 |
#' "df", |
| 129 |
#' model = "A string denoting the model type with optimal BIC", |
| 130 |
#' G = "Number mixture components in optimal model", |
| 131 |
#' hypvol = "If the other model contains a noise component, the |
| 132 |
#' value of the hypervolume parameter. Otherwise `NA`.", |
| 133 |
#' "nobs" |
| 134 |
#' ) |
| 135 |
#' |
| 136 |
#' @export |
| 137 |
glance.Mclust <- function(x, ...) { |
| 138 |
as_glance_tibble( |
| 139 |
model = unname(x$modelName), |
| 140 |
G = unname(x$G), |
| 141 |
BIC = unname(x$bic), |
| 142 |
logLik = unname(x$loglik), |
| 143 |
df = unname(x$df), |
| 144 |
hypvol = unname(x$hypvol), |
| 145 |
nobs = stats::nobs(x), |
| 146 |
na_types = "cirriri" |
| 147 |
) |
| 148 |
} |