R/sample_igraph.R

Defines functions sample_igraph.directed_factor_model sample_igraph.undirected_factor_model sample_igraph

Documented in sample_igraph sample_igraph.directed_factor_model sample_igraph.undirected_factor_model

#' Sample a random dot product graph as an igraph graph
#'
#' @inherit sample_edgelist params details references examples description
#'
#' @return An [igraph::igraph()] object that is possibly a
#'   multigraph (that is, we take there to be multiple edges
#'   rather than weighted edges).
#'
#'   When `factor_model` is **undirected**:
#'
#'     - the graph is undirected and one-mode.
#'
#'   When `factor_model` is **directed** and **square**:
#'
#'     - the graph is directed and one-mode.
#'
#'   When `factor_model` is **directed** and **rectangular**:
#'
#'     - the graph is undirected and bipartite.
#'
#'  Note that working with bipartite graphs in `igraph` is more
#'  complex than working with one-mode graphs.
#'
#' @export
#' @family samplers
#'
sample_igraph <- function(
  factor_model,
  ...,
  poisson_edges = TRUE,
  allow_self_loops = TRUE) {

  ellipsis::check_dots_unnamed()

  if (!(requireNamespace("igraph", quietly = TRUE))) {
    stop(
      "Must install `igraph` package to return graphs as `igraph` ",
      "objects",
      call. = FALSE
    )
  }

  UseMethod("sample_igraph")
}

#' @rdname sample_igraph
#' @export
sample_igraph.undirected_factor_model <- function(
  factor_model,
  ...,
  poisson_edges = TRUE,
  allow_self_loops = TRUE) {

  edgelist <- sample_edgelist(
    factor_model,
    poisson_edges = poisson_edges,
    allow_self_loops = allow_self_loops
  )

  igraph::graph_from_data_frame(edgelist, directed = FALSE)
}

#' @rdname sample_igraph
#' @export
sample_igraph.directed_factor_model <- function(
  factor_model,
  ...,
  poisson_edges = TRUE,
  allow_self_loops = TRUE) {

  if (factor_model$n == factor_model$d) {

    edgelist <- sample_edgelist(
      factor_model,
      poisson_edges = poisson_edges,
      allow_self_loops = allow_self_loops
    )

    one_mode <- igraph::graph_from_data_frame(edgelist, directed = TRUE)
    return(one_mode)
  }

  # to represent a rectangular adjacency matrix igraph we have to make
  # a bipartite graph rather than using the usual one-mode tools

  A <- sample_sparse(
    factor_model,
    poisson_edges = poisson_edges,
    allow_self_loops = allow_self_loops
  )

  # TODO: consistency about multiple versus weighted igraphs throughout

  igraph::graph_from_incidence_matrix(A, directed = FALSE, multiple = TRUE)
}

Try the fastRG package in your browser

Any scripts or data that you put into this service are public.

fastRG documentation built on Feb. 26, 2021, 5:10 p.m.