-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSNA_Script_ResBaz_2022.R
167 lines (101 loc) · 3.51 KB
/
SNA_Script_ResBaz_2022.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# ===============================================
## ResBaz Social Network Analysis Workshop
# Introduction to network analysis in R
# Laura W. Dozal
## May 2022
# ===============================================
### Follow Along Here ###
# ---------------
# Preparation
# ---------------
# rm(list = ls())
# Load the packages
# Please do NOT load igraph yet.
library(statnet)
library(GGally)
# Read the adjacency matrix
# Inspect the data
# head(...) shows the first rows of a dataset
# -----------------------
# Get a network object
# -----------------------
# Transform the data into network format
# Look at the network object
# -----------------------
# Visualize the network
# -----------------------
# Simple visualization
# ?ggnet2
# Add some color
#### Back to presentation ####
# -------------------------------
# Explore the network structure
# -------------------------------
# Save names of club members
# club_members <- network.vertex.names(graph_karate_net)
# Get components - how many clusters do we have?
# Find isolates
# isolate <- isolates(graph_karate_net)
# Get densitiy
# gden(graph_karate_net) #, mode = "graph")
# -------------------------
# Explore actor positions
# -------------------------
# Get degree centrality
# Explore degree distribution
# Get eigenvector centrality
#####
# Eigenvector Centrality is an algorithm that measures the transitive influence of nodes.
# Relationships originating from high-scoring nodes contribute more to the score of a node
# than connections from low-scoring nodes. A high eigenvector score means that a node is
# connected to many nodes who themselves have high scores.
####
# Get Closness centrality
# Get betweenness centrality
#### Back to presentation ####
# -----------------------
# Looking at subgroups
# -----------------------
# Get an igraph object
# -----------------------
# Note on package use: We can use packages by loading them
# and then use all the functions. This is most common.
# However, we can also access functions when packages are
# not loaded. For this, we write "packagename::function_name."
# This is especially useful when two packages use the same
# function names. igraph and sna use some of the same function
# names.Therefore, we will access igraph using the "::" method.
# Get the matrix (matrix is another object type, like network object)
# Get igraph object (this is another type of network object)
# Simplify igraph object
# Look at the igraph object
#
# igraph::V(graph_karate_net) # This shows you the vertex set.
# igraph::E(graph_karate_net) # This shows you the edge set.
# Plot the network
# -----------------------
# Find cliques
# -----------------------
# Find all cliques with a minimum of three members
# Number of cliques of size 3.
# Find all cliques with a minimum of four members
# Number of cliques of size 4.
# Find all cliques with a minimum of five members
# Number of cliques of size 5.
# -----------------------
# Find communities
# -----------------------
# Use the Girvan-Newman approach to detect communities
# Visualize the network with colors according to communities
# ----------------------------
# Visualize node attributes
# ----------------------------
# Get node attributes
# Inspect node attributes
# Add attributes to our network object
# Visualize the network with colors according to attributes
# Add some color
#
# corrdinates <- gplot.layout.fruchtermanreingold(karate_net, NULL)
# karate_net %V% "x" <- corrdinates[,1]
# karate_net %V% "y" <- corrdinates[,2]