Created
November 21, 2013 01:28
-
-
Save compsocial/7574514 to your computer and use it in GitHub Desktop.
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
# pull twitter package from repo | |
install.packages('devtools') | |
library('devtools') | |
install_github("twitteR", username="geoffjentry") | |
library('twitteR') | |
setup_twitter_oauth(YOUR CREDENTIALS HERE, credentials_file=NULL) | |
# set up igraph | |
install.packages('igraph') | |
library('igraph') | |
# set up some data; i hate the <- operator | |
me = getUser('eegilbert') | |
# set to 200 to avoid traversing lots of pages and eating up api | |
N = 200 | |
# also look at ids, if you don't care about screen names | |
ifollow = me$getFriends(n=N) | |
ifollow = twListToDF(ifollow) | |
ifollow = ifollow$screenName | |
# convert to adj matrix | |
adj = matrix(nrow=N, ncol=N) | |
rownames(adj) = ifollow | |
colnames(adj) = ifollow | |
adj[] = 0 | |
for (theirname in ifollow) { | |
# back off if we're out of api; this loop can take hours | |
apileft = getCurRateLimitInfo(c("friends"))$remaining[1] | |
if (as.numeric(apileft) <= 0) { Sys.sleep(15*60+30) } | |
them = getUser(theirname) | |
theyfollow = them$getFriends(n=N) | |
theyfollow = twListToDF(theyfollow) | |
theyfollow = theyfollow$screenName | |
alsofollow = intersect(ifollow, theyfollow) | |
for (mutualname in alsofollow) { | |
adj[theirname, mutualname] = 1 | |
} | |
print(paste("done with", theirname)) | |
} | |
# set up graph | |
g = graph.adjacency(adj, mode = "directed", add.rownames=T) | |
# may want to play with filtering nodes to better visualize, as below | |
g = delete.vertices(g, which(degree(g)<10)) | |
V(g)$label <- V(g)$name | |
V(g)$degree <- degree(g) | |
# make it look a little better | |
V(g)$label.cex = 2.2 * V(g)$degree / max(V(g)$degree)+ .2 | |
V(g)$label.color = rgb(0, 0, .2, .8) | |
# basic viz | |
plot(g, layout=layout.auto(g)) | |
diameter(g) | |
# random walk community detection | |
communities = walktrap.community(g) | |
# how many communities | |
length(communities) | |
# draw them on the graph | |
plot(communities, g) | |
# rank people by different centralities | |
cm = cbind(V(g), betweenness(g)) | |
cm[sort.list(cm[,2], decreasing=T),] | |
cm = cbind(V(g), page.rank(g)$vector) | |
cm[sort.list(cm[,2], decreasing=T),] | |
# look to burt | |
sort(constraint(g)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment