diff --git a/NAMESPACE b/NAMESPACE index 1588a9b3..944ae932 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -320,6 +320,7 @@ export(network_adhesion) export(network_assortativity) export(network_balance) export(network_betweenness) +export(network_brokerage_census) export(network_closeness) export(network_cohesion) export(network_components) @@ -351,6 +352,7 @@ export(node_attribute) export(node_automorphic_equivalence) export(node_betweenness) export(node_bridges) +export(node_brokerage_census) export(node_closeness) export(node_components) export(node_constraint) @@ -569,6 +571,7 @@ importFrom(rlang,":=") importFrom(rlang,.data) importFrom(rlang,enquo) importFrom(rlang,eval_tidy) +importFrom(sna,brokerage) importFrom(sna,gcor) importFrom(stats,as.dist) importFrom(stats,as.formula) diff --git a/R/motif_census.R b/R/motif_census.R index 72fe33f5..b3f6b0da 100644 --- a/R/motif_census.R +++ b/R/motif_census.R @@ -9,6 +9,10 @@ #' @name node_census #' @family motifs #' @inheritParams is +#' @param membership A vector of partition membership as integers. +#' @param standardized Whether the score should be standardized +#' into a _z_-score indicating how many standard deviations above +#' or below the average the score lies. #' @importFrom igraph vcount graph.neighborhood delete_vertices triad_census NULL @@ -118,12 +122,16 @@ node_triad_census <- function(object){ #' | 04 | C4 #' | Z42, Z43 | diamond #' | X4 | K4 +#' +#' See also [this list of graph classes](https://www.graphclasses.org/smallgraphs.html#nodes4). #' @importFrom tidygraph %E>% #' @references #' Ortmann, Mark, and Ulrik Brandes. 2017. #' “Efficient Orbit-Aware Triad and Quad Census in Directed and Undirected Graphs.” #' \emph{Applied Network Science} 2(1):13. #' \doi{10.1007/s41109-017-0027-2}. +#' @examples +#' node_quad_census(ison_southern_women) #' @export node_quad_census <- function(object){ if (!("oaqc" %in% rownames(utils::installed.packages()))) { @@ -150,6 +158,38 @@ node_quad_census <- function(object){ } } +#' #' @export +#' node_bmotif_census <- function(object, normalized = FALSE){ +#' if (!("bmotif" %in% rownames(utils::installed.packages()))) { +#' message("Please install package `{bmotif}`.") +#' out <- bmotif::node_positions(as_matrix(object), +#' weights_method = ifelse(is_weighted(object), +#' 'mean_motifweights', 'none'), +#' normalisation = ifelse(normalized, +#' 'levelsize_NAzero', 'none')) +#' make_node_motif(out, object) +#' } +#' } +#' +#' #' @export +#' node_igraph_census <- function(object, normalized = FALSE){ +#' out <- igraph::motifs(as_igraph(object), 4) +#' if(is_labelled(object)) +#' rownames(out) <- node_names(object) +#' colnames(out) <- c("co-K4", +#' "co-diamond", +#' "co-C4", +#' "co-paw", +#' "co-claw", +#' "P4", +#' "claw", +#' "paw", +#' "C4", +#' "diamond", +#' "K4") +#' make_node_motif(out, object) +#' } + #' @describeIn node_census Returns the shortest path lengths #' of each node to every other node in the network. #' @importFrom igraph distances @@ -176,17 +216,35 @@ node_path_census <- function(object){ make_node_motif(out, object) } -#' Censuses of graphs' motifs +#' @describeIn node_census Returns the Gould-Fernandez brokerage +#' roles played by nodes in a network. +#' @importFrom sna brokerage +#' @references +#' Gould, R.V. and Fernandez, R.M. 1989. +#' “Structures of Mediation: A Formal Approach to Brokerage in Transaction Networks.” +#' _Sociological Methodology_, 19: 89-126. +#' @examples +#' node_brokerage_census(ison_networkers, "Discipline") +#' @export +node_brokerage_census <- function(object, membership, standardized = FALSE){ + out <- sna::brokerage(as_network(object), node_attribute(object, membership)) + out <- if(standardized) out$z.nli else out$raw.nli + colnames(out) <- c("Coordinator", "Itinerant", "Gatekeeper", + "Representative", "Liaison", "Total") + make_node_motif(out, object) +} + +#' Censuses of motifs at the network level #' #' @name network_census #' @family motifs -#' @param object A migraph-consistent object. +#' @inheritParams node_census #' @param object2 A second, two-mode migraph-consistent object. NULL #' @describeIn network_census Returns a census of dyad motifs in a network #' @examples -#' network_dyad_census(ison_adolescents) +#' network_dyad_census(ison_algebra) #' @export network_dyad_census <- function(object) { if (is_twomode(object)) { @@ -271,3 +329,23 @@ network_mixed_census <- function (object, object2) { "00" = sum(onemode.null * bipartite.null) / 2) make_network_motif(res, object) } + +#' @describeIn network_census Returns the Gould-Fernandez brokerage +#' roles in a network. +#' @importFrom sna brokerage +#' @references +#' Gould, R.V. and Fernandez, R.M. 1989. +#' “Structures of Mediation: A Formal Approach to Brokerage in Transaction Networks.” +#' _Sociological Methodology_, 19: 89-126. +#' @examples +#' network_brokerage_census(ison_networkers, "Discipline") +#' @export +network_brokerage_census <- function(object, membership, standardized = FALSE){ + out <- sna::brokerage(as_network(object), node_attribute(object, membership)) + out <- if(standardized) out$z.gli else out$raw.gli + names(out) <- c("Coordinator", "Itinerant", "Gatekeeper", + "Representative", "Liaison", "Total") + make_network_motif(out, object) +} + + diff --git a/man/network_census.Rd b/man/network_census.Rd index fe0613c5..0131ea18 100644 --- a/man/network_census.Rd +++ b/man/network_census.Rd @@ -5,7 +5,8 @@ \alias{network_dyad_census} \alias{network_triad_census} \alias{network_mixed_census} -\title{Censuses of graphs' motifs} +\alias{network_brokerage_census} +\title{Censuses of motifs at the network level} \source{ Alejandro Espinosa 'netmem' } @@ -15,14 +16,29 @@ network_dyad_census(object) network_triad_census(object) network_mixed_census(object, object2) + +network_brokerage_census(object, membership, standardized = FALSE) } \arguments{ -\item{object}{A migraph-consistent object.} +\item{object}{An object of a migraph-consistent class: +\itemize{ +\item matrix (adjacency or incidence) from \code{{base}} R +\item edgelist, a data frame from \code{{base}} R or tibble from \code{{tibble}} +\item igraph, from the \code{{igraph}} package +\item network, from the \code{{network}} package +\item tbl_graph, from the \code{{tidygraph}} package +}} \item{object2}{A second, two-mode migraph-consistent object.} + +\item{membership}{A vector of partition membership as integers.} + +\item{standardized}{Whether the score should be standardized +into a \emph{z}-score indicating how many standard deviations above +or below the average the score lies.} } \description{ -Censuses of graphs' motifs +Censuses of motifs at the network level } \section{Functions}{ \itemize{ @@ -33,12 +49,16 @@ Censuses of graphs' motifs \item \code{network_mixed_census()}: Returns a census of triad motifs that span a one-mode and a two-mode network +\item \code{network_brokerage_census()}: Returns the Gould-Fernandez brokerage +roles in a network. + }} \examples{ -network_dyad_census(ison_adolescents) +network_dyad_census(ison_algebra) network_triad_census(ison_adolescents) marvel_friends <- to_unsigned(ison_marvel_relationships, "positive") (mixed_cen <- network_mixed_census(marvel_friends, ison_marvel_teams)) +network_brokerage_census(ison_networkers, "Discipline") } \references{ Davis, James A., and Samuel Leinhardt. 1967. @@ -48,6 +68,10 @@ Hollway, James, Alessandro Lomi, Francesca Pallotti, and Christoph Stadtfeld. 20 “Multilevel Social Spaces: The Network Dynamics of Organizational Fields.” \emph{Network Science} 5(2): 187–212. \doi{10.1017/nws.2017.8} + +Gould, R.V. and Fernandez, R.M. 1989. +“Structures of Mediation: A Formal Approach to Brokerage in Transaction Networks.” +\emph{Sociological Methodology}, 19: 89-126. } \seealso{ Other motifs: diff --git a/man/node_census.Rd b/man/node_census.Rd index 8e524fa8..4e928765 100644 --- a/man/node_census.Rd +++ b/man/node_census.Rd @@ -6,6 +6,7 @@ \alias{node_triad_census} \alias{node_quad_census} \alias{node_path_census} +\alias{node_brokerage_census} \title{Censuses of nodes' motifs} \usage{ node_tie_census(object) @@ -15,6 +16,8 @@ node_triad_census(object) node_quad_census(object) node_path_census(object) + +node_brokerage_census(object, membership, standardized = FALSE) } \arguments{ \item{object}{An object of a migraph-consistent class: @@ -25,6 +28,12 @@ node_path_census(object) \item network, from the \code{{network}} package \item tbl_graph, from the \code{{tidygraph}} package }} + +\item{membership}{A vector of partition membership as integers.} + +\item{standardized}{Whether the score should be standardized +into a \emph{z}-score indicating how many standard deviations above +or below the average the score lies.} } \description{ These functions include ways to take a census of the positions of nodes @@ -32,6 +41,25 @@ in a network. These include a triad census based on the triad profile of nodes, but also a tie census based on the particular tie partners of nodes. Included also are group census functions for summarising the profiles of clusters of nodes in a network. + +#' @export +node_igraph_census <- function(object, normalized = FALSE){ +out <- igraph::motifs(as_igraph(object), 4) +if(is_labelled(object)) +rownames(out) <- node_names(object) +colnames(out) <- c("co-K4", +"co-diamond", +"co-C4", +"co-paw", +"co-claw", +"P4", +"claw", +"paw", +"C4", +"diamond", +"K4") +make_node_motif(out, object) +} } \details{ The quad census uses the \code{{oaqc}} package to do @@ -56,6 +84,9 @@ For now, we offer a rough translation:\tabular{ll}{ Z42, Z43 \tab diamond \cr X4 \tab K4 \cr } + + +See also \href{https://www.graphclasses.org/smallgraphs.html#nodes4}{this list of graph classes}. } \section{Functions}{ \itemize{ @@ -71,13 +102,18 @@ in motifs of four nodes. \item \code{node_path_census()}: Returns the shortest path lengths of each node to every other node in the network. +\item \code{node_brokerage_census()}: Returns the Gould-Fernandez brokerage +roles played by nodes in a network. + }} \examples{ task_eg <- to_named(to_uniplex(ison_algebra, "task_tie")) (tie_cen <- node_tie_census(task_eg)) (triad_cen <- node_triad_census(task_eg)) +node_quad_census(ison_southern_women) node_path_census(ison_adolescents) node_path_census(ison_southern_women) +node_brokerage_census(ison_networkers, "Discipline") } \references{ Davis, James A., and Samuel Leinhardt. 1967. @@ -97,6 +133,10 @@ Opsahl, Tore, Filip Agneessens, and John Skvoretz. 2010. "Node centrality in weighted networks: Generalizing degree and shortest paths". \emph{Social Networks} 32(3): 245-51. \doi{10.1016/j.socnet.2010.03.006}. + +Gould, R.V. and Fernandez, R.M. 1989. +“Structures of Mediation: A Formal Approach to Brokerage in Transaction Networks.” +\emph{Sociological Methodology}, 19: 89-126. } \seealso{ Other motifs: