Created
October 31, 2016 16:43
-
-
Save BioSciEconomist/db570d842b11995ddba046b6098b25ee to your computer and use it in GitHub Desktop.
An Introduction to Social Network Analysis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# *------------------------------------------------------------------ | |
# | PROGRAM NAME: SNA ex | |
# | DATE: 4/9/12 | |
# | CREATED BY: MATT BOGARD | |
# | PROJECT FILE: P:\R Code References\SNA | |
# *---------------------------------------------------------------- | |
# | PURPOSE: DEMONSTRATION OF BASIC CONCEPTS OF NETWORK ANALYSIS | |
# | Code to support: http://econometricsense.blogspot.com/2012/04/introduction-to-social-network-analysis.html | |
# | REFERENCES: Conway, Drew. Social Network Analysis in R. | |
# | New York City R User Group Meetup Presentation August 6, 2009 | |
# | http://www.drewconway.com/zia/wp-content/uploads/2009/08/sna_in_R.pdf | |
# *------------------------------------------------------------------ | |
#------------------------------------------------ | |
# matrix algebra | |
#------------------------------------------------ | |
# specify the adjacency matrix | |
A <- matrix(c(0,1,1,1,1,0,1,0,1,1,0,0,1,0,0,0 ),4,4, byrow= TRUE) | |
EV <- eigen(A) # compute eigenvalues and eigenvectors | |
max(EV$values) # find the maximum eigenvalue | |
# get the eigenvector associated with the largest eigenvalue | |
centrality <- data.frame(EV$vectors[,1]) | |
names(centrality) <- "Centrality" | |
print(centrality) | |
#------------------------------------------------ | |
# igraph tools | |
#------------------------------------------------ | |
library(igraph) | |
G<-graph.adjacency(A, mode=c("undirected")) # convert adjacency matrix to an igraph object | |
cent<-data.frame(bet=betweenness(G),eig=evcent(G)$vector) # calculate betweeness & eigenvector centrality | |
res<-as.vector(lm(eig~bet,data=cent)$residuals) # calculate residuals | |
cent<-transform(cent,res=res) # add to centrality data set | |
write.csv(cent,"r_keyactorcentrality.csv") # save in project folder | |
plot(G, layout = layout.fruchterman.reingold) # network visualization | |
# create vertex names and scale by centrality | |
plot(G, layout = layout.fruchterman.reingold, vertex.size = 20*evcent(G)$vector, vertex.label = as.factor(rownames(cent)), main = 'Network Visualization in R') | |
# key actor analysis - plot eigenvector centrality vs. betweeness | |
# and scale by residuals from regression: eig~bet | |
library(ggplot2) | |
p<-ggplot(cent,aes(x=bet,y=eig,label=rownames(cent),colour=res, | |
size=abs(res)))+xlab("Betweenness Centrality")+ylab("Eigenvector Centrality") | |
pdf('key_actor_analysis.pdf') | |
p+geom_text()+opts(title="Key Actor Analysis") | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment