knitr::opts_chunk$set( # collapse = TRUE, fig.align = "center", comment = "#>", fig.path = "man/figures/", message = FALSE, warning = FALSE ) options(width = 100)
{homophily}
In social networks, actors tend to associate with others who are similar in some way, such as race, language, creed, or class. This phenomenon is called homophily.
The {homophily}
package provides flexible routines to measure mixing patterns using generic methods that are compatible with <network>
and <igraph>
objects, including {tidygraph}
's <tbl_graph>
objects.
# install.packages("remotes") remotes::install_github("knapply/homophily")
library(homophily)
data("jemmah_islamiyah", package = "homophily") # undirected <igraph> data("sampson", package = "ergm") # directed <network>
We can easily build classical mixing matrices for undirected and directed graphs.
as_mixing_matrix(jemmah_islamiyah, dim1 = "role") as_mixing_matrix(samplike, dim1 = "group")
We can also build generalized mixing matrices to explore mixing patterns across different dimensions.
For example, if we want to explore ties between each individual node and a group attribute, we can provide arguments to both dim1=
and dim2=
.
We'll use the {network}
convention of node names being stored in an attribute called "vertex.names"
to see mixing patterns between each node and the "group"
attribute.
as_mixing_matrix(samplike, dim1 = "vertex.names", dim2 = "group")
Going further, we can also explore mixing patterns across group attributes. samplike
's "cloisterville"
attribute notes whether each individual attended the Cloisterville monastery.
as_mixing_matrix(samplike, dim1 = "cloisterville", dim2 = "group")
For directed graphs, the default behavior considers both outgoing and inbound ties, but you can provide "out"
or "in"
to direction=
as desired.
as_mixing_matrix(samplike, dim1 = "cloisterville", dim2 = "group", direction = "out") as_mixing_matrix(samplike, dim1 = "cloisterville", dim2 = "group", direction = "in")
ei_index(jemmah_islamiyah, node_attr_name = "role") ei_index(jemmah_islamiyah, node_attr_name = "role", scope = "group") ei_index(jemmah_islamiyah, node_attr_name = "role", scope = "node") ei_index(samplike, node_attr_name = "group") ei_index(samplike, node_attr_name = "group", scope = "group") ei_index(samplike, node_attr_name = "group", scope = "node")
assort_discrete(jemmah_islamiyah, node_attr_name = "role") assort_discrete(samplike, node_attr_name = "group")
assort_degree(samplike)
library(tidyr) library(bench) library(ggplot2) library(igraph) build_it <- function(n_nodes, prob = 0.25, dir = TRUE) { g <- random.graph.game(n_nodes, prob, directed = dir) vertex_attr(g, name = "group") <- sample(letters, n_nodes, replace = TRUE) g } bench_it <- function(bench_foo, seq_nodes = seq(10, 2000, by = 100), ...) { all_res <- lapply(seq_nodes, function(x) { g <- build_it(x) res <- mark( bench_foo(build_it(x), node_attr_name = "group"), iterations = 20 ) res[["n_nodes"]] <- x res }) do.call(rbind, all_res) }
set.seed(831) res <- bench_it(ei_index) res %>% unnest() %>% ggplot(aes(x = n_nodes, y = time)) + ggbeeswarm::geom_quasirandom(aes(color = gc)) + coord_flip()
R CMD Check
devtools::check(quiet = TRUE)
citation("homophily")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.