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
3 changes: 3 additions & 0 deletions R/directed_factor_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ validate_directed_factor_model <- function(x) {
#' nodes should be allowed to form edges with themselves.
#' Defaults to `TRUE`. When `FALSE`, sampling proceeds allowing
#' self-loops, and these are then removed after the fact.
#' Ignored for rectangular graphs (`n != d`), whose rows
#' and columns are different sets of nodes, so that there are no
#' self-loops.
#'
#' @return A `directed_factor_model` S3 class based on a list
#' with the following elements:
Expand Down
7 changes: 6 additions & 1 deletion R/sample_edgelist.R
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ sample_edgelist.directed_factor_model <- function(
#' nodes should be allowed to form edges with themselves.
#' Defaults to `TRUE`. When `FALSE`, sampling proceeds allowing
#' self-loops, and these are then removed after the fact.
#' Ignored for rectangular graphs (`nrow(X) != nrow(Y)`), whose rows
#' and columns are different sets of nodes, so that there are no
#' self-loops.
#'
#' @param ... Ignored, for generic consistency only.
#'
Expand Down Expand Up @@ -470,7 +473,9 @@ sample_edgelist.matrix <- function(
edgelist <- dplyr::distinct(edgelist)
}

if (!allow_self_loops) {
# in rectangular graphs (n != d) rows and columns are different sets of
# nodes, so `from == to` is not a self-loop and must be kept
if (!allow_self_loops && n == d) {
edgelist <- dplyr::filter(edgelist, to != from)
}

Expand Down
5 changes: 5 additions & 0 deletions R/undirected_factor_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ validate_undirected_factor_model <- function(x) {
#'
#' @inheritParams directed_factor_model
#'
#' @param allow_self_loops Logical indicating whether or not
#' nodes should be allowed to form edges with themselves.
#' Defaults to `TRUE`. When `FALSE`, sampling proceeds allowing
#' self-loops, and these are then removed after the fact.
#'
#' @return An `undirected_factor_model` S3 class based on a list
#' with the following elements:
#'
Expand Down
5 changes: 4 additions & 1 deletion man/directed_dcsbm.Rd

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

5 changes: 4 additions & 1 deletion man/directed_erdos_renyi.Rd

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

5 changes: 4 additions & 1 deletion man/directed_factor_model.Rd

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

5 changes: 4 additions & 1 deletion man/directed_planted_partition.Rd

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

5 changes: 4 additions & 1 deletion man/sample_edgelist.matrix.Rd

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

37 changes: 36 additions & 1 deletion tests/testthat/test-allow_self_loops.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ test_that("directed graphs allow_self_loops = FALSE", {
k1 <- 5
k2 <- 3

d <- 500
# square, so that row i and column i are the same node. rectangular
# graphs have no self-loops, see the test below
d <- 1000

X <- matrix(rpois(n = n2 * k1, 1), nrow = n2)
S <- matrix(runif(n = k1 * k2, 0, .1), nrow = k1, ncol = k2)
Expand Down Expand Up @@ -67,3 +69,36 @@ test_that("directed graphs allow_self_loops = FALSE", {
tbl_graph <- sample_tidygraph(fm)
expect_false(any(diag(as_adjacency_matrix(tbl_graph)) > 0))
})

test_that("allow_self_loops = FALSE keeps all edges in rectangular models", {
# rows and columns are different node sets, so from == to is a real edge
set.seed(1)
m <- directed_factor_model(
matrix(1, 3, 1), matrix(2), matrix(1, 5, 1),
allow_self_loops = FALSE
)

reps <- 500
edges <- vapply(seq_len(reps), function(i) nrow(sample_edgelist(m)), numeric(1))
diagonal <- vapply(seq_len(reps), function(i) {
el <- sample_edgelist(m)
sum(el$from == el$to)
}, numeric(1))

# total edges are Poisson(30); z-score of the mean
mu <- expected_edges(m)
expect_equal(mu, 30)
expect_lt(abs(mean(edges) - mu) / sqrt(mu / reps), 5)

# the 3 "diagonal" pairs each have rate 2
expect_gt(sum(diagonal), 0)
expect_lt(abs(mean(diagonal) - 6) / sqrt(6 / reps), 5)

# square models still drop self-loops
sq <- directed_factor_model(
matrix(1, 3, 1), matrix(2), matrix(1, 3, 1),
allow_self_loops = FALSE
)
el <- sample_edgelist(sq)
expect_false(any(el$from == el$to))
})
Loading