Skip to content

Instantly share code, notes, and snippets.

@jean-robert
Created January 22, 2012 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jean-robert/1657558 to your computer and use it in GitHub Desktop.
Save jean-robert/1657558 to your computer and use it in GitHub Desktop.
Cluster your Facebook friends
require(igraph)
require(RCurl)
require(rjson)
# Insert your own token
access_token <- '****'
# Romain François's function to connect to Facebook Graph API
facebook <- function( path = "me", access_token = token, options) {
if( !missing(options) ){
options <- sprintf( "?%s", paste( names(options), "=", unlist(options), collapse = "&", sep = "" ) )
} else {
options <- ""
}
data <- getURL( sprintf( "https://graph.facebook.com/%s%s&access_token=%s", path, options, access_token ) )
fromJSON( data )
}
# Petr Simecek's code to retrieve friends' relationships
# scrape the list of friends
friends <- facebook( path="me/friends" , access_token=access_token)
# extract Facebook IDs
friends.id <- sapply(friends$data, function(x) x$id)
# extract names
friends.name <- sapply(friends$data, function(x) iconv(x$name,"UTF-8","ASCII//TRANSLIT"))
# short names to initials
initials <- function(x) paste(substr(x,1,1), collapse="")
friends.initial <- sapply(strsplit(friends.name," "), initials)
# friendship relation matrix
N <- length(friends.id)
friendship.matrix <- matrix(0,N,N,dimnames=list(friends.id, friends.id))
for (i in 1:N) {
tmp <- facebook( path=paste("me/mutualfriends", friends.id[i], sep="/") , access_token=access_token)
mutualfriends <- sapply(tmp$data, function(x) x$id)
friendship.matrix[i,friends.id %in% mutualfriends] <- 1
}
ga.data <- do.call(rbind, lapply(1:N, function(i) {
do.call(rbind, lapply(1:N, function(j) {
if(friendship.matrix[i,j]==1) return(data.frame(from=friends.name[i], to=friends.name[j]))
}))
}))
# Gary's code for social network analysis and clustering
g <- graph.data.frame(ga.data, directed=FALSE)
gnc <- edge.betweenness.community(g, directed=FALSE)
m <- vector()
for (s in 0:nrow(gnc$merges) ) {
memb <- community.to.membership(g,gnc$merge,steps=s)$membership
m <- c(m,modularity (g, memb, weights=NULL))
}
ideal <- steps <- which(m==max(m)) - 1
gn.groups <- community.to.membership(g,gnc$merge, steps=ideal <- steps)$membership
V(g)$color <- gn.groups
#V(g)$label <- V(g)$name # this is to show the friends' name on the plot
V(g)$label <- NA
V(g)$size <- 3
png('friendscluster.png')
plot(g)
dev.off()
Copy link

ghost commented May 16, 2014

Wonderful project, I have forked and will be working to see if I could add something here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment