social network analysis with r

Post on 10-May-2015

1.731 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Social Network Analysis With R At National Chung Hsing University

TRANSCRIPT

SOCIAL NETWORK ANALYSIS WITH RDavid Chiu @ 中興大學

ABOUT ME

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

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

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 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?

EXPLORE FACEBOOK

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

OAUTH FLOWOpen standard for authorization. OAuth provides a method for

clients to access server resources on behalf of a resource owner

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

OPENIDOpen 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 LISTUser Data Permissions:

user_hometownuser_locationuser_interestsuser_likesuser_relationships

Friends Data Permissions: friends_hometownfriends_locationfriends_interestsfriends_likesfriends_relationships

Extended Permissions: read_friendlists

COPY TOKEN

LESSON 2: SOCIAL NETWORK ANALYSIS WITHR

Let's Hack

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

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

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

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 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)

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

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

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

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

GRAPH XML FORAMT (GRAPHML)

LESSON 3: SOCIAL NETWORK VISUALIZATIONUsing Gephi

GEPHIGephi, an open source graph visualization and manipulation

software

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

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

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))

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

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)

THANK YOU

top related