{"id":16572615,"url":"https://github.com/knapply/homophily","last_synced_at":"2025-09-10T04:07:19.543Z","repository":{"id":105873372,"uuid":"207784482","full_name":"knapply/homophily","owner":"knapply","description":"Metrics for network homophily.","archived":false,"fork":false,"pushed_at":"2019-09-15T22:06:14.000Z","size":287,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-05T14:28:35.727Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://knapply.github.io/homophily/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/knapply.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-11T10:24:24.000Z","updated_at":"2024-04-17T20:55:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"3a334cff-b016-402e-8221-66097f31031a","html_url":"https://github.com/knapply/homophily","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/knapply/homophily","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knapply%2Fhomophily","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knapply%2Fhomophily/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knapply%2Fhomophily/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knapply%2Fhomophily/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/knapply","download_url":"https://codeload.github.com/knapply/homophily/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knapply%2Fhomophily/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265136988,"owners_count":23716778,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T21:28:06.263Z","updated_at":"2025-07-13T12:03:09.025Z","avatar_url":"https://github.com/knapply.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput:\n  github_document:\n    html_preview: true\nalways_allow_html: yes\neditor_options: \n  chunk_output_type: console\n---\n\n\u003c!-- README.Rmd generates README.md. --\u003e\n\n```{r, echo = FALSE}\nknitr::opts_chunk$set(\n  # collapse = TRUE,\n  fig.align = \"center\",\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/\",\n  message = FALSE,\n  warning = FALSE\n)\n\noptions(width = 100)\n```\n\n\n# `{homophily}`\n\n\u003c!-- badges: start --\u003e\n[![Lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)\n[![GitHub last commit](https://img.shields.io/github/last-commit/knapply/homophily.svg)](https://github.com/knapply/homophily/commits/master)\n\u003c!-- [![Codecov test coverage](https://codecov.io/gh/knapply/homophily/branch/master/graph/badge.svg)](https://codecov.io/gh/knapply/homophily?branch=master) --\u003e\n[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/knapply/homophily?branch=master\u0026svg=true)](https://ci.appveyor.com/project/knapply/homophily)\n[![Travis-CI Build Status](https://travis-ci.org/knapply/homophily.svg?branch=master)](https://travis-ci.org/knapply/homophily)\n[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)\n[![Depends](https://img.shields.io/badge/Depends-GNU_R\u003e=3.6-blue.svg)](https://www.r-project.org/)\n[![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/knapply/homophily.svg)](https://github.com/knapply/homophily)\n[![HitCount](http://hits.dwyl.io/knapply/homophily.svg)](http://hits.dwyl.io/knapply/homophily)\n\u003c!-- badges: end --\u003e\n\n\n\u003ccenter\u003e\n  \u003ch4\u003e\n    \u003ci\u003e\n      They say that \"birds of a feather flock together\", but why take their word for it?\n    \u003c/i\u003e\n  \u003c/h4\u003e\n\u003c/center\u003e\n\n\u003cbr\u003e\n\n# Introduction\n\nIn 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_.\n\nThe `{homophily}` package provides flexible routines to measure mixing patterns using generic methods that are compatible with `\u003cnetwork\u003e` and `\u003cigraph\u003e` objects, including `{tidygraph}`'s `\u003ctbl_graph\u003e` objects.\n\n\n# Installation\n\n```{r, eval=FALSE}\n# install.packages(\"remotes\")\nremotes::install_github(\"knapply/homophily\")\n```\n\n# Usage\n\n```{r}\nlibrary(homophily)\n```\n\n```{r}\ndata(\"jemmah_islamiyah\", package = \"homophily\") # undirected \u003cigraph\u003e\ndata(\"sampson\", package = \"ergm\")               # directed \u003cnetwork\u003e\n```\n\n\n## Mixing Matrices\n\nWe can easily build classical mixing matrices for undirected and directed graphs.\n\n```{r}\nas_mixing_matrix(jemmah_islamiyah, dim1 = \"role\")\nas_mixing_matrix(samplike, dim1 = \"group\")\n```\n\n\n## Remixing Mixing Matrices\n\nWe can also build _generalized_ mixing matrices to explore mixing patterns across different dimensions.\n\nFor example, if we want to explore ties between each individual node and a group attribute, we can provide arguments to both `dim1=` and `dim2=`.\n\nWe'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.\n\n```{r}\nas_mixing_matrix(samplike, dim1 = \"vertex.names\", dim2 = \"group\")\n```\n\nGoing further, we can also explore mixing patterns _across_  group attributes. `samplike`'s `\"cloisterville\"` attribute notes whether each individual attended the Cloisterville monastery.\n\n```{r}\nas_mixing_matrix(samplike, dim1 = \"cloisterville\", dim2 = \"group\")\n```\n\n\nFor directed graphs, the default behavior considers both outgoing and inbound ties, but you can provide `\"out\"` or `\"in\"` to `direction=` as desired.\n\n```{r}\nas_mixing_matrix(samplike, dim1 = \"cloisterville\", dim2 = \"group\",\n                 direction = \"out\")\nas_mixing_matrix(samplike, dim1 = \"cloisterville\", dim2 = \"group\",\n                 direction = \"in\")\n```\n\n## E-I Index\n\n```{r}\nei_index(jemmah_islamiyah, node_attr_name = \"role\")\nei_index(jemmah_islamiyah, node_attr_name = \"role\", scope = \"group\")\nei_index(jemmah_islamiyah, node_attr_name = \"role\", scope = \"node\")\n\nei_index(samplike, node_attr_name = \"group\")\nei_index(samplike, node_attr_name = \"group\", scope = \"group\")\nei_index(samplike, node_attr_name = \"group\", scope = \"node\")\n```\n\n## Assortativity\n\n```{r}\nassort_discrete(jemmah_islamiyah, node_attr_name = \"role\")\nassort_discrete(samplike, node_attr_name = \"group\")\n```\n\n```{r}\nassort_degree(samplike) \n```\n\n\n\n# Benchmarks\n\n```{r}\nlibrary(tidyr)\nlibrary(bench)\nlibrary(ggplot2)\nlibrary(igraph)\n\n\nbuild_it \u003c- function(n_nodes, prob = 0.25, dir = TRUE) {\n  g \u003c- random.graph.game(n_nodes, prob, directed = dir)\n  vertex_attr(g, name = \"group\") \u003c- sample(letters, n_nodes, replace = TRUE)\n  g\n}\n\nbench_it \u003c- function(bench_foo, seq_nodes = seq(10, 2000, by = 100), ...) {\n  all_res \u003c- lapply(seq_nodes, function(x) {\n    g \u003c- build_it(x)\n    res \u003c- mark(\n      bench_foo(build_it(x), node_attr_name = \"group\"),\n      iterations = 20\n    )\n    res[[\"n_nodes\"]] \u003c- x\n    res\n  })\n  do.call(rbind, all_res)\n}\n```\n\n```{r}\nset.seed(831)\nres \u003c- bench_it(ei_index)\n\nres %\u003e% \n  unnest() %\u003e% \n  ggplot(aes(x = n_nodes, y = time)) +\n  ggbeeswarm::geom_quasirandom(aes(color = gc)) +\n  coord_flip()\n```\n\n\n# `R CMD Check`\n\n```{r}\ndevtools::check(quiet = TRUE)\n```\n\n\n# Cite\n\n```{r}\ncitation(\"homophily\")\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknapply%2Fhomophily","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fknapply%2Fhomophily","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknapply%2Fhomophily/lists"}