Skip to content

Commit 64d0391

Browse files
authored
add: extract_sbm (#62)
1 parent 7067d32 commit 64d0391

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ S3method(extract_pairwise_interactions,bgmCompare)
1515
S3method(extract_pairwise_interactions,bgms)
1616
S3method(extract_posterior_inclusion_probabilities,bgmCompare)
1717
S3method(extract_posterior_inclusion_probabilities,bgms)
18+
S3method(extract_sbm,bgms)
1819
S3method(print,bgmCompare)
1920
S3method(print,bgms)
2021
S3method(print,summary.bgmCompare)
@@ -32,6 +33,7 @@ export(extract_indicators)
3233
export(extract_pairwise_interactions)
3334
export(extract_pairwise_thresholds)
3435
export(extract_posterior_inclusion_probabilities)
36+
export(extract_sbm)
3537
export(mrfSampler)
3638
import(RcppParallel)
3739
importFrom(Rcpp,evalCpp)

R/extractor_functions.R

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#' - `extract_pairwise_interactions()` – Posterior mean of pairwise interactions
1111
#' - `extract_category_thresholds()` – Posterior mean of category thresholds
1212
#' - `extract_indicator_priors()` – Prior structure used for edge indicators
13-
#'
13+
#' - `extract_sbm` – Extract stochastic block model parameters (if applicable)
14+
#'
1415
#' @name extractor_functions
1516
#' @title Extractor Functions for bgms Objects
1617
#' @keywords internal
@@ -175,6 +176,67 @@ extract_posterior_inclusion_probabilities.bgms = function(bgms_object) {
175176
return(pip_matrix)
176177
}
177178

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+
178240
#' @rdname extractor_functions
179241
#' @export
180242
extract_posterior_inclusion_probabilities.bgmCompare = function(bgms_object) {

man/extractor_functions.Rd

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)