diff --git a/R/directed_factor_model.R b/R/directed_factor_model.R index 0e084cd..12d05fc 100644 --- a/R/directed_factor_model.R +++ b/R/directed_factor_model.R @@ -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: diff --git a/R/sample_edgelist.R b/R/sample_edgelist.R index 6c2edf2..e7f390b 100644 --- a/R/sample_edgelist.R +++ b/R/sample_edgelist.R @@ -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. #' @@ -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) } diff --git a/R/undirected_factor_model.R b/R/undirected_factor_model.R index dea099e..82b4db3 100644 --- a/R/undirected_factor_model.R +++ b/R/undirected_factor_model.R @@ -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: #' diff --git a/man/directed_dcsbm.Rd b/man/directed_dcsbm.Rd index de005df..a476cea 100644 --- a/man/directed_dcsbm.Rd +++ b/man/directed_dcsbm.Rd @@ -202,7 +202,10 @@ for some additional details.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. Defaults to \code{TRUE}. When \code{FALSE}, sampling proceeds allowing -self-loops, and these are then removed after the fact.} +self-loops, and these are then removed after the fact. +Ignored for rectangular graphs (\code{n != d}), whose rows +and columns are different sets of nodes, so that there are no +self-loops.} } \value{ A \code{directed_dcsbm} S3 object, a subclass of the diff --git a/man/directed_erdos_renyi.Rd b/man/directed_erdos_renyi.Rd index 9cf9941..cc4d663 100644 --- a/man/directed_erdos_renyi.Rd +++ b/man/directed_erdos_renyi.Rd @@ -45,7 +45,10 @@ for some additional details.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. Defaults to \code{TRUE}. When \code{FALSE}, sampling proceeds allowing -self-loops, and these are then removed after the fact.} +self-loops, and these are then removed after the fact. +Ignored for rectangular graphs (\code{n != d}), whose rows +and columns are different sets of nodes, so that there are no +self-loops.} } \value{ A \code{directed_factor_model} S3 class based on a list diff --git a/man/directed_factor_model.Rd b/man/directed_factor_model.Rd index d4ef91e..b042bbc 100644 --- a/man/directed_factor_model.Rd +++ b/man/directed_factor_model.Rd @@ -58,7 +58,10 @@ for some additional details.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. Defaults to \code{TRUE}. When \code{FALSE}, sampling proceeds allowing -self-loops, and these are then removed after the fact.} +self-loops, and these are then removed after the fact. +Ignored for rectangular graphs (\code{n != d}), whose rows +and columns are different sets of nodes, so that there are no +self-loops.} } \value{ A \code{directed_factor_model} S3 class based on a list diff --git a/man/directed_planted_partition.Rd b/man/directed_planted_partition.Rd index 762c2cc..5bcee67 100644 --- a/man/directed_planted_partition.Rd +++ b/man/directed_planted_partition.Rd @@ -119,7 +119,10 @@ for some additional details.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. Defaults to \code{TRUE}. When \code{FALSE}, sampling proceeds allowing -self-loops, and these are then removed after the fact.} +self-loops, and these are then removed after the fact. +Ignored for rectangular graphs (\code{n != d}), whose rows +and columns are different sets of nodes, so that there are no +self-loops.} } \value{ A \code{directed_planted_partition} S3 object, which is a subclass diff --git a/man/sample_edgelist.matrix.Rd b/man/sample_edgelist.matrix.Rd index 1888717..9c91f29 100644 --- a/man/sample_edgelist.matrix.Rd +++ b/man/sample_edgelist.matrix.Rd @@ -55,7 +55,10 @@ for some additional details. Defaults to \code{TRUE}.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. Defaults to \code{TRUE}. When \code{FALSE}, sampling proceeds allowing -self-loops, and these are then removed after the fact.} +self-loops, and these are then removed after the fact. +Ignored for rectangular graphs (\code{nrow(X) != nrow(Y)}), whose rows +and columns are different sets of nodes, so that there are no +self-loops.} \item{...}{Ignored, for generic consistency only.} diff --git a/tests/testthat/test-allow_self_loops.R b/tests/testthat/test-allow_self_loops.R index f29cd57..d203734 100644 --- a/tests/testthat/test-allow_self_loops.R +++ b/tests/testthat/test-allow_self_loops.R @@ -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) @@ -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)) +})