social network analysis with r
DESCRIPTION
Social Network Analysis With R At National Chung Hsing UniversityTRANSCRIPT
![Page 1: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/1.jpg)
SOCIAL NETWORK ANALYSIS WITH RDavid Chiu @ 中興大學
![Page 2: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/2.jpg)
ABOUT ME
Co-founder of NumerInfoTrend Micro Engineerywchiu-tw.appspot.com
![Page 3: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/4.jpg)
SOCIAL NETWORK
http://libeltyseo.com/wp-content/uploads/2013/03/social-networking.png
![Page 5: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/5.jpg)
HUMAN NATURE
http://cdn.macado.com/assets/2010/03/peeping-tom.gif
![Page 6: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/7.jpg)
EXPLORE FACEBOOK
![Page 8: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/8.jpg)
LESSON 1: CONNECT TO SOCIAL NETWORKOAuth FlowOauth v.s. OAuth2OpenID
![Page 9: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/11.jpg)
OPENIDOpen standard that allows users to be authenticated by certain
co-operating sites
![Page 12: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/12.jpg)
CONNECT TO FACEBOOK
https://developers.facebook.com/
![Page 13: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/13.jpg)
GET ACCESS TOKEN
https://developers.facebook.com/tools/explorer/
![Page 14: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/14.jpg)
USER PERMISSION
![Page 15: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/16.jpg)
COPY TOKEN
![Page 17: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/17.jpg)
LESSON 2: SOCIAL NETWORK ANALYSIS WITHR
Let's Hack
![Page 18: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/21.jpg)
TEST ON API EXPLORER
![Page 22: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/23.jpg)
GET FRIEND LIST> friends <- fb_connect(path="me/friends", access_token=access_token)> friends
![Page 24: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/29.jpg)
GRAPH XML FORAMT (GRAPHML)
![Page 30: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/30.jpg)
LESSON 3: SOCIAL NETWORK VISUALIZATIONUsing Gephi
![Page 31: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/31.jpg)
GEPHIGephi, an open source graph visualization and manipulation
software
![Page 32: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/33.jpg)
LEARN MORE ABOUT GEPHIhttps://www.udemy.com/gephi/
![Page 34: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/35.jpg)
CONCLUSION REMARKInteresting Fact Abour FriendsValue Behind The NetworkStatstic Is Worth More Than Thousand of Words
![Page 36: Social Network Analysis With R](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081801/554ef9b4b4c90580698b493e/html5/thumbnails/37.jpg)
THANK YOU