Code
<- read.csv("C:/Users/18639/Social_Networks_Spring_2023/posts/_data/got/got_marriages.csv") got_marriages
Hannah Rosenbaum
March 6, 2023
Describe the many measures of centrality of at least one network of your choice.
If you have not done it before, always start by evaluating the basic structure of the network (number of edges and vertices, dyad and triad census, etc.). Create a data frame nodes
with the corresponding local attributes such as degree or local transitivity.
Compute at least one measure of centrality for each node, adding them as attributes to nodes
in the form of extra columns: eigencentrality, betweenness, closeness, etc. Compute the distribution of at least one of those measures. What do we learn from comparing these measures of centrality with an equivalent random network (i.e. with the same number of edges and vertices)?
nodes <- data.frame("Name"=c(), "degree"=c(), "transitivity"=c(), 'centrality'=c())
families <- unique.data.frame(as.data.frame(got_marriages[, c("To")]))
centrality <- eigen_centrality(graph)$vector
# Iterate across family names computing degrees
for (name in families) {
obs <- data.frame("Name"=name, "degree"=degree(graph, v = name, mode = "total"), "transitivity"=transitivity(graph, vids=name, type="local"), "centrality"=centrality[name])
nodes <- rbind(nodes, obs)
}
nodes
Name degree transitivity centrality
Stark Stark 34 0.4166667 0.113194498
Martell Martell 12 0.4000000 0.089478716
Essos Essos 15 0.6000000 0.192767892
Reach Reach 49 0.3636364 0.282784492
Septa Septa 1 NaN 0.001889578
Dorne Dorne 4 1.0000000 0.046014412
Targaryen Targaryen 85 0.3636364 1.000000000
Lannister Lannister 28 0.7000000 0.057273778
Vale Vale 37 0.4666667 0.151988690
Riverlands Riverlands 39 0.5714286 0.299362265
Crownlands Crownlands 22 0.5714286 0.272772860
Westerlands Westerlands 31 0.6666667 0.089262517
Tyrell Tyrell 12 1.0000000 0.055324132
North North 46 0.1666667 0.126568322
Beyond Wall Beyond Wall 1 NaN 0.002672823
Frey Frey 51 0.2888889 0.214820599
Tully Tully 5 0.5000000 0.018146747
Stormlands Stormlands 11 0.7000000 0.099655003
Arryn Arryn 10 0.6666667 0.079951432
Baratheon Baratheon 17 0.4181818 0.089383036
---
title: "Week 4 Challenge Instructions"
author: "Hannah Rosenbaum"
description: "Centrality of a Network"
date: "03/06/2023"
format:
html:
toc: true
code-fold: true
code-copy: true
code-tools: true
# editor: visual
categories:
- challenge_4
- instructions
# - railroads
# - faostat
# - wildbirds
---
```{r}
#| label: setup
#| include: false
library(tidyverse)
library(dplyr)
library(igraph)
```
## Challenge Overview
Describe the many measures of centrality of at least one network of your choice.
## Describe the Network Data
If you have not done it before, always start by evaluating the basic structure of the network (number of edges and vertices, dyad and triad census, etc.). Create a data frame `nodes` with the corresponding local attributes such as degree or local transitivity.
## Centrality
Compute at least one measure of centrality for each node, adding them as attributes to `nodes` in the form of extra columns: eigencentrality, betweenness, closeness, etc. Compute the distribution of at least one of those measures. What do we learn from comparing these measures of centrality with an equivalent random network (i.e. with the same number of edges and vertices)?
```{r}
got_marriages <- read.csv("C:/Users/18639/Social_Networks_Spring_2023/posts/_data/got/got_marriages.csv")
```
```{r}
graph <- graph_from_edgelist(as.matrix(got_marriages[, c("From", "To")]))
```
```{r}
nodes <- data.frame("Name"=c(), "degree"=c(), "transitivity"=c(), 'centrality'=c())
families <- unique.data.frame(as.data.frame(got_marriages[, c("To")]))
centrality <- eigen_centrality(graph)$vector
# Iterate across family names computing degrees
for (name in families) {
obs <- data.frame("Name"=name, "degree"=degree(graph, v = name, mode = "total"), "transitivity"=transitivity(graph, vids=name, type="local"), "centrality"=centrality[name])
nodes <- rbind(nodes, obs)
}
nodes
```
```{r}
random_network <- sample_gnp(length(V(graph)), graph.density(graph))
plot(random_network)
```
```{r}
hist(nodes$centrality)
```
```{r}
hist(eigen_centrality(random_network)$vector)
```