social network analysis with r

37
SOCIAL NETWORK ANALYSIS WITH R David Chiu @ 中興大學

Upload: david-chiu

Post on 10-May-2015

1.731 views

Category:

Technology


1 download

DESCRIPTION

Social Network Analysis With R At National Chung Hsing University

TRANSCRIPT

Page 1: Social Network Analysis With R

SOCIAL NETWORK ANALYSIS WITH RDavid Chiu @ 中興大學

Page 2: Social Network Analysis With R

ABOUT ME

Co-founder of NumerInfoTrend Micro Engineerywchiu-tw.appspot.com

Page 3: Social Network Analysis With R

AGENDAWhat is Social Network?Why to Analyze Social Network?How to Analyze

Social Network Connection (Oauth, Oauth2, OpenID)Social Network Analysis With RSocial Network Visualization (Gephi)Facebook Post Liked Analysis

Conclusion

Page 4: Social Network Analysis With R

SOCIAL NETWORK

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

Page 5: Social Network Analysis With R

HUMAN NATURE

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

Page 6: Social Network Analysis With R

WHAT DO WE WANT TO KNOW?Who knows whom, and which people are common to theirsocial networks?How frequently are particular people communicating with oneanother?Which social network connections generate the most value fora particular niche?How does geography affect your social connections in anonline world?Who are the most influential/popular people in a socialnetwork?What are people chatting about (and is it valuable)?What are people interested in based upon the human languagethat they use in a digital world?

Page 7: Social Network Analysis With R

EXPLORE FACEBOOK

Page 8: Social Network Analysis With R

LESSON 1: CONNECT TO SOCIAL NETWORKOAuth FlowOauth v.s. OAuth2OpenID

Page 9: Social Network Analysis With R

OAUTH FLOWOpen standard for authorization. OAuth provides a method for

clients to access server resources on behalf of a resource owner

Page 10: Social Network Analysis With R

DIFFERENCE BETWEEN OAUTH AND OAUTH2More OAuth Flows to allow better support for non-browserbased applicationsOAuth 2.0 no longer requires client applications to havecryptographyOAuth 2.0 signatures are much less complicatedOAuth 2.0 Access tokens are "short-lived"OAuth 2.0 is meant to have a clean separation of rolesbetween the server responsible for handling OAuth requestsand the server handling user authorization

Page 11: Social Network Analysis With R

OPENIDOpen standard that allows users to be authenticated by certain

co-operating sites

Page 12: Social Network Analysis With R

CONNECT TO FACEBOOK

https://developers.facebook.com/

Page 13: Social Network Analysis With R

GET ACCESS TOKEN

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

Page 14: Social Network Analysis With R

USER PERMISSION

Page 15: Social Network Analysis With R

PERMISSION LISTUser Data Permissions:

user_hometownuser_locationuser_interestsuser_likesuser_relationships

Friends Data Permissions: friends_hometownfriends_locationfriends_interestsfriends_likesfriends_relationships

Extended Permissions: read_friendlists

Page 16: Social Network Analysis With R

COPY TOKEN

Page 17: Social Network Analysis With R

LESSON 2: SOCIAL NETWORK ANALYSIS WITHR

Let's Hack

Page 18: Social Network Analysis With R

REQUIRED PACKAGESRCurl

General network (HTTP/FTP/...) client interface for Rrjson

JSON for RXML

Tools for parsing and generating XML within R and S-Plusigraph

Network analysis and visualizationbitops

Bitwise Operations

Page 19: Social Network Analysis With R

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

Page 20: Social Network Analysis With R

FACEBOOK CONNECTfb_connect <- function( path = "me", access_token = token, options){ if( !missing(options) ){ options <- sprintf( "?%s", paste( names(options), "=", unlist(options), collapse = "&", 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 )}

Page 21: Social Network Analysis With R

TEST ON API EXPLORER

Page 22: Social Network Analysis With R

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"

Page 23: Social Network Analysis With R

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

Page 24: Social Network Analysis With R

GET FRIEND GROUP LISTfriendgroups <- 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)

Page 25: Social Network Analysis With R

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

# Extract Facebook IDsfriends.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" elsefriends.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$idfriends.hometown.name <- unlist(sapply(friends$data, function(x) {if (is.null(x$hometown$id

Page 26: Social Network Analysis With R

GET FRIENDSHIP MATRIXfriendships <- 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)}

Page 27: Social Network Analysis With R

FRIEND GRAPHfriendgraph <- 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)}

Page 28: Social Network Analysis With R

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

Page 29: Social Network Analysis With R

GRAPH XML FORAMT (GRAPHML)

Page 30: Social Network Analysis With R

LESSON 3: SOCIAL NETWORK VISUALIZATIONUsing Gephi

Page 31: Social Network Analysis With R

GEPHIGephi, an open source graph visualization and manipulation

software

Page 32: Social Network Analysis With R

MODULARITYMeasure of the structure of networks or graphs. It wasdesigned to measure the strength of division of a network intomodulesNetworks with high modularity have dense connectionsbetween the nodes within modules but sparse connectionsbetween nodes in different modules

Page 33: Social Network Analysis With R

LEARN MORE ABOUT GEPHIhttps://www.udemy.com/gephi/

Page 34: Social Network Analysis With R

GET LIKES COUNT OF POSTSrequire(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))

Page 35: Social Network Analysis With R

CONCLUSION REMARKInteresting Fact Abour FriendsValue Behind The NetworkStatstic Is Worth More Than Thousand of Words

Page 36: Social Network Analysis With R

YOUR TURN TO DO SOME ANALYSISFind Popular Checkins Among Your FriendsCommon Interest Between Your FriendsFemale/Male Distribution of Your Know FriendsMost Pupular Photos Being LikedAsociation Rule of Friends (Gossip)

Page 37: Social Network Analysis With R

THANK YOU