Social Network Analysis With R – Lesson 1: Connect to Social Network – Connect to Facebook



Social Network Analysis With R – Lesson 1: Connect to Social Network – Connect to Facebook

0 0


SocialNetworkWithR

Social Network Analysis With R

On Github ywchiu / SocialNetworkWithR

Social Network Analysis With R

David Chiu @ 中興大學

About Me

Agenda

  • What is Social Network?
  • Why to Analyze Social Network?
  • How to Analyze
    • Social Network Connection (Oauth, Oauth2, OpenID)
    • Social Network Analysis With R
    • Social Network Visualization (Gephi)
    • Facebook Post Liked Analysis
  • Conclusion

Social Network

http://libeltyseo.com/wp-content/uploads/2013/03/social-networking.png

Human Nature

http://cdn.macado.com/assets/2010/03/peeping-tom.gif

What do we want to know?

  • Who knows whom, and which people are common to their social networks?
  • How frequently are particular people communicating with one another?
  • Which social network connections generate the most value for a particular niche?
  • How does geography affect your social connections in an online world?
  • Who are the most influential/popular people in a social network?
  • What are people chatting about (and is it valuable)?
  • What are people interested in based upon the human language that they use in a digital world?

Explore Facebook

Lesson 1: Connect to Social Network

  • OAuth Flow
  • Oauth v.s. OAuth2
  • OpenID

OAuth Flow

Open standard for authorization. OAuth provides a method for clients to access server resources on behalf of a resource owner

Difference Between OAuth and OAuth2

  • More OAuth Flows to allow better support for non-browser based applications
  • OAuth 2.0 no longer requires client applications to have cryptography
  • OAuth 2.0 signatures are much less complicated
  • OAuth 2.0 Access tokens are "short-lived"
  • OAuth 2.0 is meant to have a clean separation of roles between the server responsible for handling OAuth requests and the server handling user authorization

OpenID

Open standard that allows users to be authenticated by certain co-operating sites

Connect to Facebook

https://developers.facebook.com/

Get Access Token

https://developers.facebook.com/tools/explorer/

User Permission

Permission List

  • User Data Permissions: 
    • user_hometown
    • user_location
    • user_interests
    • user_likes
    • user_relationships
  • Friends Data Permissions: 
    • friends_hometown
    • friends_location
    • friends_interests
    • friends_likes
    • friends_relationships
  • Extended Permissions: 
    • read_friendlists

Copy Token

Lesson 2: Social Network Analysis With R

Let's Hack

Required Packages

  • RCurl
    • General network (HTTP/FTP/...) client interface for R
  • rjson
    • JSON for R
  • XML
    • Tools for parsing and generating XML within R and S-Plus
  • igraph
    • Network analysis and visualization
  • bitops
    • Bitwise Operations

Required Package

install.packages(c('rjson','RCurl','XML','igraph','bitops'),dependencies=TRUE)
library(rjson)
library(RCurl)
library(XML)
library(igraph)
library(bitops)

Facebook Connect

fb_connect <-  function( path = "me", access_token = token, options){
  if( !missing(options) ){
    options <- sprintf( "?%s", paste( names(options), "=", unlist(options), collapse = "&", sep = "" ) )
    options <- gsub(",", "%2C", options)
    url <- sprintf( "https://graph.facebook.com/%s%s&access_token=%s", path, options, access_token )
  } else {
    url <- sprintf( "https://graph.facebook.com/%s?access_token=%s", path, access_token )
  }
  data <- getURL( url, ssl.verifypeer = FALSE  )
  fromJSON( data )
}

Test On API Explorer

Executed Result

> fb_connect("me",access_token)
$id
[1] "778224889"

$name
[1] "David Chiu"

$first_name
[1] "David"

$last_name
[1] "Chiu"

$link
[1] "https://www.facebook.com/ChiuYW"

$username
[1] "ChiuYW"

Get Friend List

> friends <- fb_connect(path="me/friends", access_token=access_token)
> friends

Get Friend Group List

friendgroups <- fb_connect( path="me/friendlists", access_token=access_token)
friendgroups.id   <- sapply(friendgroups$data, function(x) x$id)
friendgroups.name <- sapply(friendgroups$data, function(x) x$name)

Facebook Element Extractor

# Get Friends Information From FB
me <- fb_connect(path="me", access_token=access_token, options=list("fields"="id,name,location,hometown,gender,friends.fields(location,hometown,name,gender)"))
myname <- me$name
friends <- me$friends

# Extract Facebook IDs
friends.id <- sapply(friends$data, function(x) x$id)

# Extract FB Meta Information 
friends.name <- unlist(sapply(friends$data, function(x)  iconv(x$name,to='UTF8',sub="")))
friends.gender <- sapply(friends$data, function(x) unlist(if(is.null(x$gender)) "none" else x$gender))
friends.location.id <- unlist(sapply(friends$data, function(x) x$location$id))
friends.location.name <- unlist(sapply(friends$data, function(x) {if (is.null(x$location$id)) "none" else iconv(x$location$name,to='UTF8',sub="")}))
friends.hometown.name <- unlist(sapply(friends$data, function(x) {if (is.null(x$hometown$id)) "none" else iconv(x$hometown$name,to='UTF8',sub="")}))

Get Friendship Matrix

friendships <- function() {
  print("Generating friendship matrix")
  N <- length(friends.id)
  friendship.matrix <- matrix(0,N,N)
  for (i in 1:N) {
    print(paste(i,friends.name[i]))
    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
  }

  # Create connections with my friends
  friends_con <- c(1:N)
  friends_con[] <-1

  # Add this vector as a column to the friendship matrix
  friendship.matrix <- cbind(friendship.matrix,friends_con)

  # Append my friendship with myself 
  friendship.matrix <- rbind(friendship.matrix,append(friends_con,0))

  rownames(friendship.matrix) <- append(friends.name,myname)
  colnames(friendship.matrix) <- append(friends.name,myname)

  return (friendship.matrix)
}

Friend Graph

friendgraph <- function(friendship.matrix) {
  friendship.graph <- graph.adjacency(friendship.matrix, mode=c("undirected"), weighted=NULL)

  V(friendship.graph)$gender <- append(friends.gender, myself$gender)
  V(friendship.graph)$location <- append(friends.location.name, myself$location$name)
  V(friendship.graph)$hometown <- append(friends.hometown.name, myself$hometown$name)
  V(friendship.graph)$Label <- V(friendship.graph)$name
  return(friendship.graph)
}

Draw Friendship Graph

f_matrix <- friendships()
f_graph <- friendgraph(f_matrix)
write.graph(f_graph, "friends.graphml",format="graphml")

Graph XML Foramt (Graphml)

Lesson 3: Social Network Visualization

Using Gephi

Gephi

Gephi, an open source graph visualization and manipulation software

Modularity

  • Measure of the structure of networks or graphs. It was designed to measure the strength of division of a network into modules
  • Networks with high modularity have dense connections between the nodes within modules but sparse connections between nodes in different modules

Learn More About Gephi

https://www.udemy.com/gephi/

Get Likes Count of Posts

require(plyr)
posts <- fb_connect(path="me/posts", access_token=access_token, options=list("fields"="likes.fields(name)"))
post.likes <- sapply(posts$data, function(x) x$likes)
post.likes.data <- sapply(post.likes, function(x) x$data)
li = list()
for (i in 1:length(post.likes.data)){
  for (j in 1:(length(post.likes.data[[i]])-1)){
    li <- append(li, post.likes.data[[i]][[j]]$name)
  }
}
df = do.call(rbind.data.frame, li)
colnames(df) <- c("name")
ddply(df, .(name), summarize, NumSubs = length(name))

Conclusion Remark

  • Interesting Fact Abour Friends
  • Value Behind The Network
  • Statstic Is Worth More Than Thousand of Words

Your Turn to Do Some Analysis

  • Find Popular Checkins Among Your Friends
  • Common Interest Between Your Friends
  • Female/Male Distribution of Your Know Friends
  • Most Pupular Photos Being Liked
  • Asociation Rule of Friends (Gossip)

THANK YOU