|
10 | 10 | #' - `extract_pairwise_interactions()` – Posterior mean of pairwise interactions |
11 | 11 | #' - `extract_category_thresholds()` – Posterior mean of category thresholds |
12 | 12 | #' - `extract_indicator_priors()` – Prior structure used for edge indicators |
13 | | -#' |
| 13 | +#' - `extract_sbm` – Extract stochastic block model parameters (if applicable) |
| 14 | + #' |
14 | 15 | #' @name extractor_functions |
15 | 16 | #' @title Extractor Functions for bgms Objects |
16 | 17 | #' @keywords internal |
@@ -175,6 +176,67 @@ extract_posterior_inclusion_probabilities.bgms = function(bgms_object) { |
175 | 176 | return(pip_matrix) |
176 | 177 | } |
177 | 178 |
|
| 179 | + |
| 180 | +#' @rdname extractor_functions |
| 181 | +#' @export |
| 182 | +extract_sbm = function(bgms_object) { |
| 183 | + UseMethod("extract_sbm") |
| 184 | +} |
| 185 | + |
| 186 | +#' @rdname extractor_functions |
| 187 | +#' @export |
| 188 | +extract_sbm.bgms = function(bgms_object) { |
| 189 | + if (!inherits(bgms_object, "bgms")) stop("Object must be of class 'bgms'.") |
| 190 | + |
| 191 | + # Checks |
| 192 | + ver = try(utils::packageVersion("bgms"), silent = TRUE) |
| 193 | + if (inherits(ver, "try-error") || is.na(ver)) { |
| 194 | + stop("Could not determine 'bgms' package version.") |
| 195 | + } |
| 196 | + if (utils::compareVersion(as.character(ver), "0.1.6.0") < 0) { |
| 197 | + stop(paste0("Extractor functions for the SBM prior are defined for bgms version 0.1.6.0. ", |
| 198 | + "The current installed version is ", as.character(ver), ".")) |
| 199 | + } |
| 200 | + |
| 201 | + arguments = extract_arguments(bgms_object) |
| 202 | + |
| 203 | + if (!isTRUE(arguments$edge_selection)) { |
| 204 | + stop("To extract SBM summaries, run bgm() with edge_selection = TRUE.") |
| 205 | + } |
| 206 | + if (!identical(arguments$edge_prior, "Stochastic-Block")) { |
| 207 | + stop(paste0("edge_prior must be 'Stochastic-Block' (got '", |
| 208 | + as.character(arguments$edge_prior), "').")) |
| 209 | + } |
| 210 | + |
| 211 | + posterior_num_blocks = try(bgms_object$posterior_num_blocks, silent = TRUE) |
| 212 | + posterior_mean_allocations = try(bgms_object$posterior_mean_allocations, silent = TRUE) |
| 213 | + posterior_mode_allocations = try(bgms_object$posterior_mode_allocations, silent = TRUE) |
| 214 | + posterior_mean_coclustering_matrix = try(bgms_object$posterior_mean_coclustering_matrix, silent = TRUE) |
| 215 | + |
| 216 | + if (inherits(posterior_num_blocks, "try-error")) posterior_num_blocks = NULL |
| 217 | + if (inherits(posterior_mean_allocations, "try-error")) posterior_mean_allocations = NULL |
| 218 | + if (inherits(posterior_mode_allocations, "try-error")) posterior_mode_allocations = NULL |
| 219 | + if (inherits(posterior_mean_coclustering_matrix, "try-error")) posterior_mean_coclustering_matrix = NULL |
| 220 | + |
| 221 | + if (is.null(posterior_num_blocks) || |
| 222 | + is.null(posterior_mean_allocations) || |
| 223 | + is.null(posterior_mode_allocations) || |
| 224 | + is.null(posterior_mean_coclustering_matrix)) { |
| 225 | + stop(paste0("SBM summaries not found in this object. Missing one or more of: ", |
| 226 | + "posterior_num_blocks, posterior_mean_allocations, ", |
| 227 | + "posterior_mode_allocations, posterior_mean_coclustering_matrix.")) |
| 228 | + } |
| 229 | + |
| 230 | + |
| 231 | + return(list( |
| 232 | + posterior_num_blocks = posterior_num_blocks, |
| 233 | + posterior_mean_allocations = posterior_mean_allocations, |
| 234 | + posterior_mode_allocations = posterior_mode_allocations, |
| 235 | + posterior_mean_coclustering_matrix = posterior_mean_coclustering_matrix |
| 236 | + )) |
| 237 | +} |
| 238 | + |
| 239 | + |
178 | 240 | #' @rdname extractor_functions |
179 | 241 | #' @export |
180 | 242 | extract_posterior_inclusion_probabilities.bgmCompare = function(bgms_object) { |
|
0 commit comments