Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions R/directed_dcsbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) {
#' This is subject to change, and may not be reproducible.
#' `k_out` defaults to `NULL`. You must specify either `k_out` and
#' `k_in` together, or `B`. You may specify all three at once, in which
#' case the number of outgoing blocks is taken from `B`.
#' case `k_out` must equal `nrow(B)`.
#'
#' @param k_in (mixing matrix) The number of incoming blocks in the blockmodel.
#' Use when you don't want to specify the mixing-matrix by hand.
#' When `k_in` is specified, the elements of `B` are drawn
#' randomly from a `Uniform(0, 1)` distribution.
#' This is subject to change, and may not be reproducible.
#' `k_in` defaults to `NULL`. You may specify all three at once, in which
#' case the number of incoming blocks is taken from `B`.
#' case `k_in` must equal `ncol(B)`.
#'
#' @param B (mixing matrix) A `k_out` by `k_in` matrix of block connection
#' probabilities. The probability that a node in block `i` connects
Expand Down Expand Up @@ -705,6 +705,29 @@ directed_dcsbm <- function(
)
}

# `k_out` and `k_in` may be given along with `B`, but must agree with it
if (
!is.null(B) &&
!is.null(k_out) &&
!isTRUE(all.equal(as.numeric(k_out), as.numeric(nrow(B))))
) {
stop(
"`k_out` must equal `nrow(B)` when both are given.",
call. = FALSE
)
}

if (
!is.null(B) &&
!is.null(k_in) &&
!isTRUE(all.equal(as.numeric(k_in), as.numeric(ncol(B))))
) {
stop(
"`k_in` must equal `ncol(B)` when both are given.",
call. = FALSE
)
}

if (is.null(B) && (is.null(k_out) || is.null(k_in))) {
stop(
"Must specify either `k_out` and `k_in` together, or `B`.",
Expand Down
2 changes: 2 additions & 0 deletions R/undirected_chung_lu.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ chung_lu <- function(
theta <- stats::rlnorm(n, meanlog = 2, sdlog = 1)
} else if (is.null(n)) {
n <- length(theta)
} else {
stop("Must specify only one of `n` and `theta`, not both.", call. = FALSE)
}

chung_lu <- dcsbm(
Expand Down
20 changes: 20 additions & 0 deletions R/undirected_mmsbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ mmsbm <- function(
theta <- stats::rlnorm(n, meanlog = 2, sdlog = 1)
} else if (is.null(n)) {
n <- length(theta)
} else {
stop("Must specify only one of `n` and `theta`, not both.", call. = FALSE)
}

### mixing matrix
Expand All @@ -235,6 +237,24 @@ mmsbm <- function(
}

k <- nrow(B)
} else {
stop("Must specify only one of `B` and `k`, not both.", call. = FALSE)
}

if (k < 2) {
stop(
"`mmsbm()` needs at least two blocks. For a single block, use ",
"`chung_lu()` or `dcsbm()`.",
call. = FALSE
)
}

if (force_pure && n < k) {
stop(
"`force_pure = TRUE` needs at least as many nodes as blocks ",
"(`n >= k`).",
call. = FALSE
)
}

### block membership
Expand Down
8 changes: 8 additions & 0 deletions R/undirected_overlapping_sbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,14 @@ overlapping_sbm <- function(
stop("Must specify only one of `B` and `k`, not both.", call. = FALSE)
}

if (force_pure && n < k) {
stop(
"`force_pure = TRUE` needs at least as many nodes as blocks ",
"(`n >= k`).",
call. = FALSE
)
}

### block membership

if (length(pi) != nrow(B) || length(pi) != ncol(B)) {
Expand Down
4 changes: 2 additions & 2 deletions man/directed_dcsbm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions tests/testthat/test-contradictory-args.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
test_that("directed_dcsbm() errors when k_out / k_in contradict B", {
B <- matrix(0.1, 2, 3)

expect_error(
directed_dcsbm(n = 10, B = B, k_out = 5, k_in = 3),
"`k_out` must equal `nrow(B)`",
fixed = TRUE
)
expect_error(
directed_dcsbm(n = 10, B = B, k_out = 2, k_in = 5),
"`k_in` must equal `ncol(B)`",
fixed = TRUE
)

# agreeing values are still fine, as documented
m <- suppressMessages(directed_dcsbm(n = 10, B = B, k_out = 2, k_in = 3))
expect_equal(c(m$k1, m$k2), c(2, 3))
})

test_that("mmsbm() rejects redundant arguments and degenerate k", {
B <- diag(2) * 0.5 + 0.1

expect_error(
mmsbm(theta = rep(1, 10), k = 3, B = B),
"Must specify only one of `B` and `k`, not both.",
fixed = TRUE
)
expect_error(
mmsbm(n = 100, theta = rep(1, 10), B = B),
"Must specify only one of `n` and `theta`, not both.",
fixed = TRUE
)
expect_error(
mmsbm(theta = rep(1, 10), B = matrix(0.5)),
"at least two blocks"
)
expect_error(
mmsbm(theta = rep(1, 2), B = diag(3) + 0.1),
"`force_pure = TRUE` needs at least as many nodes as blocks",
fixed = TRUE
)

# without force_pure, n < k is fine
m <- mmsbm(theta = rep(1, 2), B = diag(3) + 0.1, force_pure = FALSE)
expect_equal(m$n, 2)
})

test_that("overlapping_sbm() errors clearly when force_pure and n < k", {
expect_error(
overlapping_sbm(n = 2, B = diag(3) * 0.5 + 0.1),
"`force_pure = TRUE` needs at least as many nodes as blocks",
fixed = TRUE
)
})

test_that("chung_lu() errors when both n and theta are given", {
expect_error(
chung_lu(n = 100, theta = rep(1, 10)),
"Must specify only one of `n` and `theta`, not both.",
fixed = TRUE
)
})
Loading