https://github.com/knapply/homophily
Metrics for network homophily.
https://github.com/knapply/homophily
Last synced: 10 months ago
JSON representation
Metrics for network homophily.
- Host: GitHub
- URL: https://github.com/knapply/homophily
- Owner: knapply
- License: gpl-3.0
- Created: 2019-09-11T10:24:24.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-09-15T22:06:14.000Z (almost 7 years ago)
- Last Synced: 2025-03-05T14:28:35.727Z (over 1 year ago)
- Language: R
- Homepage: https://knapply.github.io/homophily/
- Size: 280 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output:
github_document:
html_preview: true
always_allow_html: yes
editor_options:
chunk_output_type: console
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
# collapse = TRUE,
fig.align = "center",
comment = "#>",
fig.path = "man/figures/",
message = FALSE,
warning = FALSE
)
options(width = 100)
```
# `{homophily}`
[](https://www.tidyverse.org/lifecycle/#experimental)
[](https://github.com/knapply/homophily/commits/master)
[](https://ci.appveyor.com/project/knapply/homophily)
[](https://travis-ci.org/knapply/homophily)
[](https://www.gnu.org/licenses/gpl-3.0)
[](https://www.r-project.org/)
[](https://github.com/knapply/homophily)
[](http://hits.dwyl.io/knapply/homophily)
They say that "birds of a feather flock together", but why take their word for it?
# Introduction
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 `` and `` objects, including `{tidygraph}`'s `` objects.
# Installation
```{r, eval=FALSE}
# install.packages("remotes")
remotes::install_github("knapply/homophily")
```
# Usage
```{r}
library(homophily)
```
```{r}
data("jemmah_islamiyah", package = "homophily") # undirected
data("sampson", package = "ergm") # directed
```
## Mixing Matrices
We can easily build classical mixing matrices for undirected and directed graphs.
```{r}
as_mixing_matrix(jemmah_islamiyah, dim1 = "role")
as_mixing_matrix(samplike, dim1 = "group")
```
## Remixing Mixing Matrices
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.
```{r}
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.
```{r}
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.
```{r}
as_mixing_matrix(samplike, dim1 = "cloisterville", dim2 = "group",
direction = "out")
as_mixing_matrix(samplike, dim1 = "cloisterville", dim2 = "group",
direction = "in")
```
## E-I Index
```{r}
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")
```
## Assortativity
```{r}
assort_discrete(jemmah_islamiyah, node_attr_name = "role")
assort_discrete(samplike, node_attr_name = "group")
```
```{r}
assort_degree(samplike)
```
# Benchmarks
```{r}
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)
}
```
```{r}
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`
```{r}
devtools::check(quiet = TRUE)
```
# Cite
```{r}
citation("homophily")
```