0% found this document useful (0 votes)
266 views

Social Network Analysis in R PDF

This document discusses using R for social network analysis (SNA). It begins by reviewing other SNA software and comparing R to Python for SNA. It then covers the pros and cons of using R, including the diversity of SNA tools available in R and its built-in visualization capabilities. Examples of basic SNA and network visualization in R are provided. The document concludes by mentioning additional online tutorials and experts available for help with SNA in R.

Uploaded by

Md Rezaul Karim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
266 views

Social Network Analysis in R PDF

This document discusses using R for social network analysis (SNA). It begins by reviewing other SNA software and comparing R to Python for SNA. It then covers the pros and cons of using R, including the diversity of SNA tools available in R and its built-in visualization capabilities. Examples of basic SNA and network visualization in R are provided. The document concludes by mentioning additional online tutorials and experts available for help with SNA in R.

Uploaded by

Md Rezaul Karim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Social Network Analysis in R

Drew Conway

New York University - Department of Politics

August 6, 2009
Introduction

Why use R to do SNA?


I Review of SNA software
I Pros and Cons of SNA in R
I Comparison of SNA in R vs. Python
Examples of SNA in R
I Basic SNA - computing centrality metrics and identifying key actors
I Visualization - examples using igraph’s built-in viz functions
Additional Resources
I Online Tutorials
I Helpful experts
Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

SNA software landscape

The number of software suites and packages available for conducting social
network analysis has exploded over the past ten years
I In general, this software can be categorized in two ways:
I Type - many SNA tools are developed to be standalone applications, while
others are language specific packages
I Intent - consumers and producer of SNA come from a wide range of
technical expertise and/or need, therefore, there exist simple tools for data
collection and basic analysis, as well as complex suites for advanced research

Standalone Apps Modules & Packages


- ORA (Windows) - libSNA (Python)
Basic - Analyst Notebook (Windows) - UrlNet (Python)
- KrakPlot (Windows) - NodeXL (MS Excel)
- UCINet (Windows) - NetworkX (Python)
Advanced - Pajek (Multi) - JUNG (Java)
- Network Workbench (Multi) - igraph (Python, R & Ruby)

Drew Conway Social Network Analysis in R


Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Pros and Cons of SNA in R


Pros Cons

Drew Conway Social Network Analysis in R


Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Pros and Cons of SNA in R


Pros Cons
Diversity of tools available in R
I Analysis - sna: sociometric data;
RBGL: Binding to Boost Graph Lib
I Simulation - ergm: exponential
random graph; networksis: bipartite
networks
I Specific use - degreenet: degree
distribution; tnet: weighted networks

Drew Conway Social Network Analysis in R


Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Pros and Cons of SNA in R


Pros Cons
Diversity of tools available in R
I Analysis - sna: sociometric data;
RBGL: Binding to Boost Graph Lib
I Simulation - ergm: exponential
random graph; networksis: bipartite
networks
I Specific use - degreenet: degree
distribution; tnet: weighted networks
Built-in visualization tools
I Take advantage of R’s built-in
graphics tools

Drew Conway Social Network Analysis in R


Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Pros and Cons of SNA in R


Pros Cons
Diversity of tools available in R
I Analysis - sna: sociometric data;
RBGL: Binding to Boost Graph Lib
I Simulation - ergm: exponential
random graph; networksis: bipartite
networks
I Specific use - degreenet: degree
distribution; tnet: weighted networks
Built-in visualization tools
I Take advantage of R’s built-in
graphics tools

Immediate access to more statistical


analysis
I Perform SNA and network based econometrics
“under the same roof”
Drew Conway Social Network Analysis in R
Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Pros and Cons of SNA in R


Pros Cons
Steep learning curve for SNA novices
Diversity of tools available in R I As with most things in R, the network
I Analysis - sna: sociometric data; analysis packages were designed by
RBGL: Binding to Boost Graph Lib analysts for analysts
I Simulation - ergm: exponential I These tools require at least a
random graph; networksis: bipartite moderate familiarity with network
networks structures and basic metrics
I Specific use - degreenet: degree Structural Holes
distribution; tnet: weighted networks
Burt’s constraint is higher if ego has less, or mutually stronger
Built-in visualization tools related (i.e. more redundant) contacts. Burt’s measure of constraint,
C[i], of vertex i’s ego network V[i]
I Take advantage of R’s built-in
graphics tools

Immediate access to more statistical


analysis
I Perform SNA and network based econometrics
“under the same roof”
Drew Conway Social Network Analysis in R
Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Pros and Cons of SNA in R


Pros Cons
Steep learning curve for SNA novices
Diversity of tools available in R I As with most things in R, the network
I Analysis - sna: sociometric data; analysis packages were designed by
RBGL: Binding to Boost Graph Lib analysts for analysts
I Simulation - ergm: exponential I These tools require at least a
random graph; networksis: bipartite moderate familiarity with network
networks structures and basic metrics
I Specific use - degreenet: degree Structural Holes
distribution; tnet: weighted networks
Burt’s constraint is higher if ego has less, or mutually stronger
Built-in visualization tools related (i.e. more redundant) contacts. Burt’s measure of constraint,
C[i], of vertex i’s ego network V[i]
I Take advantage of R’s built-in
graphics tools Duplication and Interoperability
I Large variety of packages creates
unnecessary duplication, which can be
confusing
I Users may have to switch between
packages because some function is
Immediate access to more statistical supported in one but not the other
analysis
I Perform SNA and network based econometrics
I Ex. blockmodeling built into sna
“under the same roof” but not igraph
Drew Conway Social Network Analysis in R
Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Direct Comparison of NetworkX (Python) vs. igraph

Using a randomly generated Barabasi-Albert network with 2,500 nodes and


4,996 edges we perform a side-by-side comparison of these two network analysis
packages.1

1
All tests performed on a 2.5 GHz Intel Core 2 Duo MacBook Pro with 4GB 667 MHz DDR2
Drew Conway Social Network Analysis in R
Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Direct Comparison of NetworkX (Python) vs. igraph

Using a randomly generated Barabasi-Albert network with 2,500 nodes and


4,996 edges we perform a side-by-side comparison of these two network analysis
packages.1
Test 1: Betweenness centrality

NX Code 1
igraph Code 1
def betweenness_test(G):
betweenness_test<-function(graph) {
start=time.clock()
return(betweenness(graph)) }
B=networkx.brandes_betweenness_centrality(G)
system.time(B<-betweenness_test(G))
return time.clock()-start

1
All tests performed on a 2.5 GHz Intel Core 2 Duo MacBook Pro with 4GB 667 MHz DDR2
Drew Conway Social Network Analysis in R
Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Direct Comparison of NetworkX (Python) vs. igraph

Using a randomly generated Barabasi-Albert network with 2,500 nodes and


4,996 edges we perform a side-by-side comparison of these two network analysis
packages.1
Test 1: Betweenness centrality

NX Code 1
igraph Code 1
def betweenness_test(G):
betweenness_test<-function(graph) {
start=time.clock()
return(betweenness(graph)) }
B=networkx.brandes_betweenness_centrality(G)
system.time(B<-betweenness_test(G))
return time.clock()-start

Runtime: 55.89 sec Runtime: 1.12 sec X

1
All tests performed on a 2.5 GHz Intel Core 2 Duo MacBook Pro with 4GB 667 MHz DDR2
Drew Conway Social Network Analysis in R
Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Direct Comparison of NetworkX (Python) vs. igraph

Using a randomly generated Barabasi-Albert network with 2,500 nodes and


4,996 edges we perform a side-by-side comparison of these two network analysis
packages.1
Test 1: Betweenness centrality

NX Code 1
igraph Code 1
def betweenness_test(G):
betweenness_test<-function(graph) {
start=time.clock()
return(betweenness(graph)) }
B=networkx.brandes_betweenness_centrality(G)
system.time(B<-betweenness_test(G))
return time.clock()-start

Runtime: 55.89 sec Runtime: 1.12 sec X

Test 2: Fruchterman-Reingold force-directed layout

NX Code 2
igraph Code 2
def layout_test(G,i=50):
layout_test<-function(graph,i=50) {
start=time.clock()
return(layout.fruchterman.reingold(graph,niter=i)) }
v=networkx.layout.spring_layout(G,iterations=i)
system.time(v<-layout_test(G))
return time.clock()-start

1
All tests performed on a 2.5 GHz Intel Core 2 Duo MacBook Pro with 4GB 667 MHz DDR2
Drew Conway Social Network Analysis in R
Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Direct Comparison of NetworkX (Python) vs. igraph

Using a randomly generated Barabasi-Albert network with 2,500 nodes and


4,996 edges we perform a side-by-side comparison of these two network analysis
packages.1
Test 1: Betweenness centrality

NX Code 1
igraph Code 1
def betweenness_test(G):
betweenness_test<-function(graph) {
start=time.clock()
return(betweenness(graph)) }
B=networkx.brandes_betweenness_centrality(G)
system.time(B<-betweenness_test(G))
return time.clock()-start

Runtime: 55.89 sec Runtime: 1.12 sec X

Test 2: Fruchterman-Reingold force-directed layout

NX Code 2
igraph Code 2
def layout_test(G,i=50):
layout_test<-function(graph,i=50) {
start=time.clock()
return(layout.fruchterman.reingold(graph,niter=i)) }
v=networkx.layout.spring_layout(G,iterations=i)
system.time(v<-layout_test(G))
return time.clock()-start

Runtime: 1 min 6.13 sec Runtime: 9.03 sec X


1
All tests performed on a 2.5 GHz Intel Core 2 Duo MacBook Pro with 4GB 667 MHz DDR2
Drew Conway Social Network Analysis in R
Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Direct Comparison of NetworkX (Python) vs. igraph

Test 3: Graph diameter (maximum shortest path)

Drew Conway Social Network Analysis in R


Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Direct Comparison of NetworkX (Python) vs. igraph

Test 3: Graph diameter (maximum shortest path)

NX Code 3
igraph Code 3

def diameter_test(G):
diameter_test<-function(graph) {
start=time.clock()
return(diameter(graph)) }
D=networkx.distance.diameter(G)
system.time(D<-diameter_test(G))
return time.clock()-start

Drew Conway Social Network Analysis in R


Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Direct Comparison of NetworkX (Python) vs. igraph

Test 3: Graph diameter (maximum shortest path)

NX Code 3
igraph Code 3

def diameter_test(G):
diameter_test<-function(graph) {
start=time.clock()
return(diameter(graph)) }
D=networkx.distance.diameter(G)
system.time(D<-diameter_test(G))
return time.clock()-start

Runtime: 15.66 sec Runtime: 0.42 sec X

Drew Conway Social Network Analysis in R


Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Direct Comparison of NetworkX (Python) vs. igraph

Test 3: Graph diameter (maximum shortest path)

NX Code 3
igraph Code 3

def diameter_test(G):
diameter_test<-function(graph) {
start=time.clock()
return(diameter(graph)) }
D=networkx.distance.diameter(G)
system.time(D<-diameter_test(G))
return time.clock()-start

Runtime: 15.66 sec Runtime: 0.42 sec X

Test 4: Find maximal cliques

NX Code 4
igraph Code 4

def max_clique_test(G):
max_clique_test<-function(graph) {
start=time.clock()
return(maximal.cliques(graph)) }
C=networkx.clique.find_cliques(G)
system.time(M<-max_clique_test(G))
return time.clock()-start

Drew Conway Social Network Analysis in R


Why use R to do SNA? SNA Software Landscape
Examples of SNA in R Pros and Cons of R
Additional Resources Comparison of SNA in R vs. Python

Direct Comparison of NetworkX (Python) vs. igraph

Test 3: Graph diameter (maximum shortest path)

NX Code 3
igraph Code 3

def diameter_test(G):
diameter_test<-function(graph) {
start=time.clock()
return(diameter(graph)) }
D=networkx.distance.diameter(G)
system.time(D<-diameter_test(G))
return time.clock()-start

Runtime: 15.66 sec Runtime: 0.42 sec X

Test 4: Find maximal cliques

NX Code 4
igraph Code 4

def max_clique_test(G):
max_clique_test<-function(graph) {
start=time.clock()
return(maximal.cliques(graph)) }
C=networkx.clique.find_cliques(G)
system.time(M<-max_clique_test(G))
return time.clock()-start

Runtime: 1.27 sec X Runtime: 8 min 24.95 sec

Finding maximal cliques can require several nested loops, which may contribute to R’s
poor performance

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Comparing two network metrics to find key actors

Often social network analysis is used to identify key actors within a social
group. To identify these actors, various centrality metrics can be computed
based on a network’s structure
I Degree (number of connections)
I Betweenness (number of shortest paths an actor is on)
I Closeness (relative distance to all other actors)
I Eigenvector centrality (leading eigenvector of sociomatrix)
One method for using these metrics to identify key actors is to plot actors’
scores for Eigenvector centrality versus Betweenness. Theoretically, these
metrics should be approximately linear; therefore, any non-linear outliers will be
of note.
I An actor with very high betweenness but low EC may be a critical
gatekeeper to a central actor
I Likewise, an actor with low betweenness but high EC may have unique
access to central actors

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Finding Key Actors with R

For this example, we will use the main component of the social network
collected on drug users in Hartford, CT. The network has 194 nodes and 273
edges.

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Finding Key Actors with R

For this example, we will use the main component of the social network
collected on drug users in Hartford, CT. The network has 194 nodes and 273
edges.

Load the data into igraph


library(igraph)
G<-read.graph("drug_main.txt",format="edgelist")
G<-as.undirected(G)
# By default, igraph inputs edgelist data as a directed graph.
# In this step, we undo this and assume that all relationships are reciprocal.

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Finding Key Actors with R

For this example, we will use the main component of the social network
collected on drug users in Hartford, CT. The network has 194 nodes and 273
edges.

Load the data into igraph


library(igraph)
G<-read.graph("drug_main.txt",format="edgelist")
G<-as.undirected(G)
# By default, igraph inputs edgelist data as a directed graph.
# In this step, we undo this and assume that all relationships are reciprocal.

Store metrics in new data frame


cent<-data.frame(bet=betweenness(G),eig=evcent(G)$vector)
# evcent returns lots of data associated with the EC, but we only need the
# leading eigenvector
res<-lm(eig~bet,data=cent)$residuals
cent<-transform(cent,res=res)
# We will use the residuals in the next step

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Finding Key Actors with R

Plot the data


library(ggplot2)
# We use ggplot2 to make things a
# bit prettier
p<-ggplot(cent,aes(x=bet,y=eig,
label=rownames(cent),colour=res,
size=abs(res)))+xlab("Betweenness
Centrality")+ylab("Eigenvector
Centrality")
# We use the residuals to color and
# shape the points of our plot,
# making it easier to spot outliers.
p+geom_text()+opts(title="Key Actor
Analysis for Hartford Drug Users")
# We use the geom_text function to plot
# the actors’ ID’s rather than points
# so we know who is who

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Finding Key Actors with R

Key Actor Analysis for Hartford Drug Users


1.0 44

Plot the data res

library(ggplot2) 0.8 −0.2


# We use ggplot2 to make things a 0
# bit prettier 0.2
p<-ggplot(cent,aes(x=bet,y=eig, 28
53 0.4
label=rownames(cent),colour=res, 0.6 0.6
size=abs(res)))+xlab("Betweenness

Centrality
Eigenvector
Centrality")+ylab("Eigenvector 50
abs(res)
Centrality")
0.1
# We use the residuals to color and
0.4 0.2
# shape the points of our plot, 141
47

# making it easier to spot outliers. 20 56


0.3
155
58 43
p+geom_text()+opts(title="Key Actor 100
22 126 0.4
57
147
Analysis for Hartford Drug Users") 23
140 18 0.5
104 19
# We use the geom_text function to plot 0.2 75 65
4184
139 9
0.6
13685
# the actors’ ID’s rather than points 48
17 87
55 54

63 89105 77
94
184
67
0.7
# so we know who is who 101
64 165
93

33
49 162
86 91
36177
174
17016821
52 111 27 16
42 68
6 12 69
148
149 78
169 172
181
183
110
25
150
182
185 176
130
45 79
26 12161 138 66 62 11
0.0
124
164
163
179
189
152
133
161
166
135
187
146
96
122
112
167
31
128
145
153
129
191
15
171
13
39
92
125
158
180
108
194
113
109
119
156
175
186
59
81
98
107
120
134
188
51
74
80
83
1 952190
210
37
3103
76 90
143
114
193
127
40
71
82
7132
173
178 3142
137
159
38 99157
192
123
106 151
8846
144
72 73154
5 97 60 244
8 118
116 160
131 117 70
34 115 14
30 29 35 102

0 1000 2000 3000 4000 5000 6000


Betweenness Centrality

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Highlighting Key Actors

Using the drug network data, we will now identify the location of the key actors
from the previous analysis
I We will use the same residual data from before to size the nodes and
locate the key actors
First, however, we’ll look at the network as a whole using igraph’s Tcl/Tk
interface

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Highlighting Key Actors

Using the drug network data, we will now identify the location of the key actors
from the previous analysis
I We will use the same residual data from before to size the nodes and
locate the key actors
First, however, we’ll look at the network as a whole using igraph’s Tcl/Tk
interface

Visualizing a network in igraph

library(igraph)
G<-as.undirected(read.graph(
"drug_main.txt",type="edgelist"))
tklplot(G,layout=layout.fruchterman.reingold)
# This will open a new X11 window plot of G

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Key Actor Plot




● ●




● ●

● ●


● ● ● ● ●

141


● ●
● 155 Network plot

●●

58 44
● ●47



● ●
● 28 # Create positions for all of


● ●


● ●


# the nodes w/ force directed
l<-layout.fruchterman.reingold(G,
●53
● ●


● ● ● ● ●

niter=500)
20
●50


● ● # Set the nodes’ size relative to

● ●







# their residual value





● ●

V(G)$size<-abs(res)*10
● ●

● ● ●
● ●

# Only display the labels of key



# players


● ●

nodes<-as.vector(V(G)+1)




# Key players defined as have a






# residual value >.25
nodes[which(abs(res)<.25)]<-NA



79

# Save plot as PDF
● ●


pdf(‘actor_plot.pdf’,pointsize=7)

● ●
● plot(G,layout=l,vertex.label=nodes,
● ●

102 ●

vertex.label.dist=0.25,

● ● ●
● ●






vertex.label.color=‘red’,edge.width=1)



● ●
● ●


dev.off()
● ●

● ● ●
● ●


● ●

● ●



● ● ●

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Other Useful SNA Plots


Highlight the graph’s longest geodesic
Find diameter

d<-get.diameter(G) # Find nodes on diameter path


# Reset G’s node/width size for new graph
V(G)$size<-4
E(G)$width<-1
E(G)$color<-‘dark grey’
E(G, path=d)$width<-3 # Set diameter path width to 3
E(G, path=d)$color<-‘red’ # and change color to red
# Save plot as PDF
pdf(‘diameter_plot.pdf’)
plot(G,layout=l,vertex.label=NA)
dev.off()

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Other Useful SNA Plots


Highlight the graph’s longest geodesic
Find diameter

d<-get.diameter(G) # Find nodes on diameter path


# Reset G’s node/width size for new graph
V(G)$size<-4
E(G)$width<-1
E(G)$color<-‘dark grey’
E(G, path=d)$width<-3 # Set diameter path width to 3
E(G, path=d)$color<-‘red’ # and change color to red
# Save plot as PDF
pdf(‘diameter_plot.pdf’)
plot(G,layout=l,vertex.label=NA)
dev.off()


● ● ● ●

● ● ● ●
● ●
● ● ● ● ●
● ● ●
● ● ● ●●● ●
● ● ● ● ●
● ● ● ● ●

● ● ● ● ●
● ● ● ●● ●●
● ● ● ● ● ● ●
● ● ● ●
● ● ● ●
● ● ● ● ● ● ●
● ● ●
● ● ● ● ●● ●●
● ● ● ● ● ●
● ● ● ●● ●
● ● ●
● ● ● ● ● ●

● ● ●
● ● ● ● ●
● ●● ● ● ●
● ● ● ● ●

● ●
● ● ● ● ●
● ● ●
● ●
● ●● ● ● ●
● ● ●●
● ● ● ● ● ●
● ● ●● ● ● ● ●

●●
● ● ● ●● ● ●
● ● ●
● ●
● ● ● ● ● ●
● ● ●
● ●
● ● ●
● ●
● ●

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Other Useful SNA Plots


Highlight the graph’s longest geodesic Extract the 2-core
Find diameter K-core Analysis
d<-get.diameter(G) # Find nodes on diameter path # Find each actor’s coreness
# Reset G’s node/width size for new graph cores<-graph.coreness(G)
V(G)$size<-4 # Extract 2-core, to eliminate pendants and pendant chains
E(G)$width<-1 G2<-subgraph(G,as.vector(which(cores>1))-1)
E(G)$color<-‘dark grey’ V(G2)$size<-4
E(G, path=d)$width<-3 # Set diameter path width to 3 l2<-layout.fruchterman.reingold(G2,niter=500)
E(G, path=d)$color<-‘red’ # and change color to red # Save plot as a PDF
# Save plot as PDF pdf(‘2core.pdf’,pointsize=7)
pdf(‘diameter_plot.pdf’) plot(G2,layout=l2)
plot(G,layout=l,vertex.label=NA) dev.off()
dev.off()


● ● ● ●

● ● ● ●
● ●
● ● ● ● ●
● ● ●
● ● ● ●●● ●
● ● ● ● ●
● ● ● ● ●

● ● ● ● ●
● ● ● ●● ●●
● ● ● ● ● ● ●
● ● ● ●
● ● ● ●
● ● ● ● ● ● ●
● ● ●
● ● ● ● ●● ●●
● ● ● ● ● ●
● ● ● ●● ●
● ● ●
● ● ● ● ● ●

● ● ●
● ● ● ● ●
● ●● ● ● ●
● ● ● ● ●

● ●
● ● ● ● ●
● ● ●
● ●
● ●● ● ● ●
● ● ●●
● ● ● ● ● ●
● ● ●● ● ● ● ●

●●
● ● ● ●● ● ●
● ● ●
● ●
● ● ● ● ● ●
● ● ●
● ●
● ● ●
● ●
● ●

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Basic SNA
Examples of SNA in R
Visualization
Additional Resources

Other Useful SNA Plots


Highlight the graph’s longest geodesic Extract the 2-core
Find diameter K-core Analysis
d<-get.diameter(G) # Find nodes on diameter path # Find each actor’s coreness
# Reset G’s node/width size for new graph cores<-graph.coreness(G)
V(G)$size<-4 # Extract 2-core, to eliminate pendants and pendant chains
E(G)$width<-1 G2<-subgraph(G,as.vector(which(cores>1))-1)
E(G)$color<-‘dark grey’ V(G2)$size<-4
E(G, path=d)$width<-3 # Set diameter path width to 3 l2<-layout.fruchterman.reingold(G2,niter=500)
E(G, path=d)$color<-‘red’ # and change color to red # Save plot as a PDF
# Save plot as PDF pdf(‘2core.pdf’,pointsize=7)
pdf(‘diameter_plot.pdf’) plot(G2,layout=l2)
plot(G,layout=l,vertex.label=NA) dev.off()
dev.off()


● ● ● ●
● ● ● ●
● ● ●
● ●
● ● ●
● ● ● ● ● ● ●
● ● ● ●

● ● ● ● ● ● ● ●
● ●
● ● ● ●
● ● ● ●●● ● ● ●
● ● ● ●● ● ● ● ●
● ● ●
● ● ● ● ● ● ● ●
● ● ● ● ● ● ●
● ● ● ● ● ● ● ●
● ● ● ●● ●● ● ● ●
● ● ● ● ● ● ● ●● ●
● ● ● ● ● ●
● ● ●
● ●
● ● ● ● ● ● ● ● ● ● ●
● ● ● ●
● ● ● ● ●● ●● ● ● ●
● ● ● ● ● ● ●
● ● ● ●● ●
● ● ●
● ● ● ● ● ● ● ●
● ●
● ● ● ●
● ● ● ● ●
● ●● ● ● ● ● ●
● ● ●
● ● ● ● ●
● ● ● ●
● ● ●
● ● ● ●
● ● ● ● ● ● ● ● ●
● ● ●
● ● ●
● ● ● ● ●
● ● ● ● ●
● ●● ●
● ● ● ● ●● ●
● ● ●
● ● ● ● ● ● ● ● ● ●
● ● ●● ● ● ● ●
● ● ● ● ●
●● ● ●
● ● ● ●● ● ● ●
● ● ● ● ● ●
● ●
● ● ● ● ● ●
● ● ●
● ● ● ●
● ● ● ●
● ●
● ●

Drew Conway Social Network Analysis in R


Why use R to do SNA?
Online Resources
Examples of SNA in R
Experts
Additional Resources

Online Resources
igraph
I Network Analysis with igraph
I Excellent resource for learning how to use igraph in R, but also reviews
many of the basic concepts of SNA
statnet
I Statnet Users Guide
I This package combines functionality from several popular R packages for
SNA, and the online users guide contains reference material for:
I network: A package for managing relational data in R
I ergm: A package to fit, simulate and diagnose exponential family models for networks
I latentnet: a package for fitting latent cluster models for networks
I sna: A package for social network analysis
I dynamicnetwork and rSoNIA: Prototype packages for managing and animating longitudinal network
data
I networksis: A Package to Simulate Bipartite Graphs with Fixed Marginals Through Sequential
Importance Sampling

Material from this presentation


I These slides are available for download at the NY HackR website under
files
I The R and Python code and data used for the benchmarking and analysis
examples are also available for download
Drew Conway Social Network Analysis in R
Why use R to do SNA?
Online Resources
Examples of SNA in R
Experts
Additional Resources

Helpful Experts

Several experts in both SNA in R, and SNA more general are active online and
can be very helpful for those trying these methods for the first time
I SNA in R Experts
I Nicole Radziwill - networks researcher
Web: http://qualityandinnovation.com/
Twitter: @nicoleradziwill
I Michael Bommarito - PhD student in political science at U Michigan
Web: http://computationallegalstudies.com/
Twitter: @mjbommar

I General SNA Help


I Valdis Krebs - Business networks researcher and developer of InFlow
Web: http://www.orgnet.com/
Twitter: @valdiskrebs
I Steve Borgatti - Professor at U Kentucky Business school and UCINET developer
Web: http://www.steveborgatti.com/
Twitter: @ittagroB

Drew Conway Social Network Analysis in R


Drew’s contact info

I Email: [email protected]
I Web: http://www.drewconway.com/zia
I Twitter: @drewconway

You might also like