Code
#import the data
<- "C:/Users/Bud/Desktop/DACSS_697E_Social Network Analysis_SP2023/Social_Networks_SP_2023/posts/_data/got/got_locations.csv"
got_locations
# Read the CSV file into a dataframe
<- read.csv(got_locations) got_locations_data
Cynthia Hester
March 20, 2023
Describe the many measures of centrality of at least one network of your choice. ******************************************************************************* For Challenge Four I will use the GOT Location data.
checking out first few rows of 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.
# Extracting matrix from data frame
adj_matrix <- as.matrix(got_locations_data[, -c(1:3)])
# Check if the matrix is square
if (nrow(adj_matrix) == ncol(adj_matrix)) {
got_locations_data.ig <- graph_from_adjacency_matrix(adj_matrix, mode = 'directed', weighted = TRUE)
} else {
print("Adjacency matrix is not square")
}
place region x y
1 abandoned Wilding Village beyond the wall 926 136
2 Acorn Hall Riverlands 777 1227
3 Andalos Essos 1517 1033
4 Astapor Slaver's Bay 2670 1978
5 West of the Iron Islands Iron Islands 161 1050
6 Barrowton the North 652 709
7 Bitterbridge the Reach 770 1524
8 Blackwater Bay Crownlands 1021 1409
9 Braavos Essos 1471 943
10 camp around the Twins Riverlands 775 997
11 camp near the Trident Riverlands 900 1158
12 Cape Wrath Stormlands 1114 1704
13 Castle Black the Wall 955 169
14 Castle Darry Riverlands 939 1193
15 Castle Hayford Crownlands 955 1370
16 Castle Rosby Crownlands 1017 1326
17 Cave of Beric Dondarrion Riverlands 987 1264
18 Cave of the Children of the Forest beyond the wall 858 128
19 cavern in haunted forest beyond the wall 822 108
20 Craster's Keep beyond the wall 907 131
21 Deepwood Motte the North 643 381
22 deserted wildling village beyond the wall 1031 109
23 Dorne Dorne 859 1876
24 Dothraki Sea Essos 2774 1531
25 Dragonstone Crownlands 1186 1295
26 Dragonstone (Essos) Essos 3021 1612
27 Dreadfort the North 1024 467
28 Duskendale Crownlands 1088 1284
29 Eastwatch the Wall 1038 178
30 Encounter with the Brave Companions Riverlands 904 1157
31 field outside of Pentos Essos 1524 1378
32 Fishing village east of God's eye Riverlands 926 1252
33 Fist of the First Men beyond the wall 840 92
34 Gates of the Moon the Vale 1022 1093
35 Ghoyan Drohe Essos 1678 1401
36 Green Fork Riverlands 938 1075
37 Greywater Watch the North 787 788
38 Gulf of Grief Slaver's Bay 2532 2169
39 Hammerhorn Iron Islands 442 1044
40 Hardhome beyond the wall 1075 58
41 Harrenhal Riverlands 893 1222
42 Haunted Forest beyond the wall 916 116
43 High Heart Riverlands 789 1213
44 High Road (in Mountains of the Moon) the Vale 953 1153
45 High Road (near the Eyrie) the Vale 1016 1101
46 Holdfast on west shore of God's eye Riverlands 873 1260
47 Inn of the Kneeling Man Riverlands 791 1141
48 Inn south of the God's eye Riverlands 900 1319
49 Isle of Cedars Slaver's Bay 2662 1883
50 Khal Drogo's manse Essos 1505 1385
51 King's Landing Crownlands 1277 1642
52 Kingsroad, a little north of King's Landing Crownlands 984 1325
53 kingsroad, north of Winterfell the North 938 297
54 kingsroad, south of Winterfell the North 813 670
55 Lhazareen village Essos 2868 1677
56 Little Finger the Vale 1208 924
57 Lordsport Iron Islands 484 1101
58 Lys Essos 1393 1944
59 Maidenpool Riverlands 1032 1251
60 Frostfangs beyond the wall 789 107
61 Meereen Slaver's Bay 2752 1752
62 Mill on Acorn Water the North 793 563
63 Moat Cailin the North 820 754
64 Mole's Town the North 958 204
65 Mountains of the Moon the Vale 1021 1117
66 Nagga's Hills Iron Islands 423 1033
67 New Ghis Slaver's Bay 2735 2350
68 Nightfort the Wall 1033 171
69 North of Harrenhal Riverlands 893 1195
70 North of Tumbledown tower the North 746 352
71 Ny Sar Essos 1753 1456
72 off the coast of Dorne Dorne 1318 1874
73 off the coast of Valyria Slaver's Bay 2309 2299
74 Old Crossroads Inn Riverlands 929 1162
75 Old Wyk Iron Islands 425 1035
76 Oldstones Riverlands 752 1060
77 Oldtown the Reach 485 1792
78 outside of Harrenhal Riverlands 897 1209
79 Pebbleton Iron Islands 458 1052
80 Pennytree Riverlands 773 1162
81 Pentos Essos 1487 1396
82 Pyke Iron Islands 504 1093
83 Qarth Essos 3535 2151
84 Queenscrown the Wall 920 206
85 Quiet Isle Riverlands 993 1207
86 Raventree Hall Riverlands 735 1139
87 Red Fork Riverlands 793 1145
88 Red Waste Essos 3004 1812
89 Riverrun Riverlands 732 1177
90 Saltpans Riverlands 969 1196
91 Seagard Riverlands 742 1024
92 Selhorys Essos 1826 1782
93 Shield Islands the Reach 479 1598
94 Skagos the North 1183 195
95 Skirling pass beyond the wall 801 94
96 Slaver's Bay Slaver's Bay 2564 1879
97 south of Craster's Keep beyond the wall 911 147
98 Stone Mill Riverlands 770 1160
99 Stoney Sept Riverlands 794 1313
100 Stony Shore the North 404 593
101 Storm's end Stormlands 1114 1585
102 Sunspear Dorne 1208 1923
103 Ten Towers Iron Islands 569 1070
104 The Crag Westerlands 526 1194
105 the Eyrie the Vale 1068 1062
106 the Greenblood Dorne 1082 1910
107 The Peach Inn Riverlands 790 1314
108 the Sorrows Essos 1813 1642
109 the Twins Riverlands 764 980
110 The Wall, spot of first invasion the Wall 911 166
111 the Whispers Crownlands 1197 1214
112 Town held by Bloody Mummers Riverlands 936 1181
113 Trident ferry Riverlands 917 1174
114 Tumbledown tower the North 714 472
115 Vaes Dothrak Essos 3182 1210
116 Vaes Tolorro Essos 3096 1826
117 Village between the lakes the North 914 327
118 Village of lord Goodbrook Riverlands 823 1199
119 Village that rests in the trees Riverlands 845 1210
120 Volantis Essos 1909 1926
121 Volon Therys Essos 1874 1917
122 Water gardens Dorne 1199 1932
123 Weirwood north of the Wall beyond the wall 960 156
124 Whispering Wood Riverlands 739 1190
125 White Harbor the North 913 717
126 Whitetree beyond the wall 930 156
127 Wilding ambush site beyond the wall 812 92
128 Winterfell the North 807 505
129 Wolfswood the North 678 453
130 Yunkai Slaver's Bay 2701 1812
131 Giant's stairs beyond the wall 807 77
132 Milkwater beyond the wall 788 71
133 Shadow Tower the Wall 861 171
134 Torrhen's Square the North 672 583
[[ suppressing 134 column names 'abandoned Wilding Village', 'Acorn Hall', 'Andalos' ... ]]
edges
nodes
dyad census
triad census
Warning in triad_census(got_graph): At core/misc/motifs.c:1165 : Triad census
called on an undirected graph.
library(igraph)
# betweenness centrality
V(got_graph)$betweenness <- igraph::betweenness(got_graph)
# closeness centrality
V(got_graph)$closeness <- igraph::closeness(got_graph)
# eigenvector centrality
V(got_graph)$eigen_centrality <- igraph::eigen_centrality(got_graph)$vector
# data frame for centrality measures
centrality_df <- data.frame(
Node = V(got_graph)$name,
Betweenness = V(got_graph)$betweenness,
Closeness = V(got_graph)$closeness,
Eigenvector = V(got_graph)$eigen_centrality
)
# Print the centrality table
print(centrality_df)
---
title: "Challenge Four "
author: "Cynthia Hester"
description: "Centrality of a Network"
date: "03/20/2023"
format:
html:
toc: true
code-fold: true
code-copy: true
code-tools: true
# editor: visual
categories:
- Cynthia Hester
- Challenge Four
---
```{r}
#| label: setup
#| include: false
#|
library(igraph)
library(network)
library(statnet)
library(tidyverse)
library(readr)
```
## Challenge Overview
Describe the many measures of centrality of at least one network of your choice.
*******************************************************************************
For Challenge Four I will use the **GOT Location** data.
```{r,results='hide'}
#import the data
got_locations <- "C:/Users/Bud/Desktop/DACSS_697E_Social Network Analysis_SP2023/Social_Networks_SP_2023/posts/_data/got/got_locations.csv"
# Read the CSV file into a dataframe
got_locations_data <- read.csv(got_locations)
```
checking out first few rows of data
```{r,results='hide'}
head(got_locations_data)
```
## 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.
************************************************************************
```{r,results='hide'}
# Extracting matrix from data frame
adj_matrix <- as.matrix(got_locations_data[, -c(1:3)])
# Check if the matrix is square
if (nrow(adj_matrix) == ncol(adj_matrix)) {
got_locations_data.ig <- graph_from_adjacency_matrix(adj_matrix, mode = 'directed', weighted = TRUE)
} else {
print("Adjacency matrix is not square")
}
```
```{r,results='hide'}
if (ncol(got_locations_data) == 2) {
# The data is an edge list
got_locations_data.ig <- graph_from_edgelist(as.matrix(got_locations_data), directed = TRUE)
} else {
print("The data format is not supported.")
}
```
```{r,results='hide'}
dim(got_locations_data)
```
```{r}
got_locations_data
```
```{r,results='hide'}
got_locations_data[1 : 4, 1 : 4]
```
```{r,results='hide'}
edge_list <- combn(got_locations_data$place, 2)
got_graph <- graph_from_edgelist(t(edge_list), directed = FALSE)
# Get the adjacency matrix of the graph
adj_matrix <- as_adjacency_matrix(got_graph)
# Print adjacency matrix
print(adj_matrix)
```
## Network Attributes
edges
```{r,results='hide'}
ecount(got_graph)
```
nodes
```{r,results='hide'}
# nodes of the graph
#print(V(got_graph)$name)
vcount(got_graph)
```
dyad census
```{r,results='hide'}
# Calculate dyad
dyad_census <- dyad_census(got_graph)
# Print dyad
print(dyad_census)
```
triad census
```{r,results='hide'}
# triad census
triad_census <- triad_census(got_graph)
# Print triad
print(triad_census)
```
****************************************************************************
```{r,results='hide'}
print(got_graph)
summary(got_graph)
```
## transitivity
```{r,results='hide'}
transitivity(got_graph, type = 'local')
```
```{r,results='hide'}
transitivity(got_graph, type = "global")
```
```{r,results='hide'}
transitivity(got_graph, type = "localaverage")
```
```{r,results='hide'}
#Directed or undirected edges verification
is_directed(got_graph)
```
```{r,results='hide'}
#Bipartite or single mode network verification got locations
is_bipartite(got_graph)
```
```{r,results='hide'}
# Weighted or unweighted edges verification in got locations
is_weighted(got_graph)
```
## Centrality
```{r,results='hide'}
library(igraph)
# betweenness centrality
V(got_graph)$betweenness <- igraph::betweenness(got_graph)
# closeness centrality
V(got_graph)$closeness <- igraph::closeness(got_graph)
# eigenvector centrality
V(got_graph)$eigen_centrality <- igraph::eigen_centrality(got_graph)$vector
# data frame for centrality measures
centrality_df <- data.frame(
Node = V(got_graph)$name,
Betweenness = V(got_graph)$betweenness,
Closeness = V(got_graph)$closeness,
Eigenvector = V(got_graph)$eigen_centrality
)
# Print the centrality table
print(centrality_df)
```