SOCIAL NETWORK ANALYSIS WITH R
David Chiu @ 中興大學
ABOUT ME
Co-founder of NumerInfo
Trend Micro Engineer
ywchiu-tw.appspot.com
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
i sa lp ca e( ( rs n,Ru l ,X L, ir p'' io s) dp ne c e= RE
n tl . ak gs c' jo ''C r' 'M ' ' g ah,b tp ' ,e ed ni sT U)
l ba yr sn
i rr ( jo )
l ba yR ul
i rr ( Cr )
l ba yX L
i rr ( M)
l ba yi rp )
i rr ( ga h
l ba yb tp )
i rr ( io s
FACEBOOK CONNECT

f _o nc < f nt o ( p t = " e, ac s_ oe = tk n ot os {
b cn e t - u c in a h
m" ce s tk n
o e, p in )
i ( !i sn (p i n) )
f m si go to s {
o to s< s rn f "% " ps e nm s o to s ,"" ul s( pi n) c la s ="
p i n - p i t( ?s, a t( ae ( pi n) =, ni to t os , ol pe
&
o to s< g u( ,, "2 " o to s
p i n - s b "" %C, p in )
u l< s rn f "t p: / r p .a eo kc m% % &c es tk n % " pt , ot os a c
r - p it ( ht s /g ah fc bo . o/ssac s _o e= s, a h pi n , c
}e s {
le
u l< s rn f "t p: / r p .a eo kc m% ? ce st kn %" p t, a cs _o e )
r - p it ( ht s /g ah fc bo . o/sa cs _ oe=s , ah c es t kn
}
dt < gt R ( u l s l vr fp e =F LE )
aa - e UL r , s. ei y er
AS
fo JO (d t )
rm S N aa
}
TEST ON API EXPLORER
EXECUTED RESULT
> f_ on c( m" a cs _o e)
bc n et "e ,c es t kn
$d
i
[ ]" 72 48 "
1 7 8 28 9
$ ae
nm
[ ]" ai C i"
1 Dvd hu
$ is _a e
f rt n m
[ ]" ai "
1 Dvd
$ at nm
l s_ a e
[ ]" hu
1 Ci"
$ ik
ln
[ ]" tp :/ w. a eo kc mC i Y"
1 h t s/ ww fc bo . o/ hu W
$ sr ae
u en m
[ ]" hu W
1 C i Y"
GET FRIEND LIST
> fi ns < f_ on c (a h" ef in s,a cs _o e=c es tk n
re d - bc n et pt =m / re d" c es t kn ac s_ oe )
> fi ns
re d
GET FRIEND GROUP LIST
f in go p < f _ on c( pt = m/ re di t" a cs _ oe =c es tk n
r ed r us - bc ne t a h" ef in l ss, ce st kn ac s _o e)
f in go p. d < sp l (r ed ru s d t,f nt o()xi)
r ed r us i
- a py fi n go p$ aa u c inx $d
f in go p. ae < sp l (r ed ru s d t,f nt o()xn m)
r ed r us nm - a py fi n go p$ aa u c inx $a e
FACEBOOK ELEMENT EXTRACTOR

# Gt Fi ns If r ai nF o F
e r ed n om to rm B
m < f_ on c( a h" e, ac s _o e= ce st k n o t osls (f ed ""d nm ,o
e - b cn et pt =m " c es tk na c s_ oe , pi n=it " il s=i, a el c
m nm < m$ ae
y ae - e nm
f in s< m $r ed
r ed - e fi n s
# Et at Fc bo I s
xr c a eo k D
f in si < sp l (r ed $a a fn to ( )x i)
r ed . d - a py fi n sd t , u ci nx $ d

# Et at F Mt I fr ai n
xr c B ea n om t o
f in sn m < u l s( ap yf i ns dt ,f nt o () i ov xn m , o' T 8, u= " )
r ed . ae - ni ts p l( re d$ aa uc in x
c n( $a e t= UF 's b " ) )
f in sg ne < s pl (r ed $ aa fn t o( )u ls ( fi . u l x gn e ) " o e e
r ed . ed r - a py f in sd t, u ci nx ni ti (s nl ( $e dr ) nn " l
f in sl ct o. d < u l s( ap yf in s dt,f nt o () xl ct o$ d )
r ed . oa in i - ni ts p l( re d$ aa uc in x $o a in i)
f in sl ct o. a e< u ls (a py fi n sd t , fn t o( ){ f( s n l ( $ oa in
r ed . oa in nm - n it s pl (r ed $a a u ci nx i i. ulxl c to$
f in sh mt w. a e< u ls (a py fi n sd t , fn t o( ){ f( s n l ( $ oe on
r ed . oe on nm - n it s pl (r ed $a a u ci nx i i. ulxh m tw$
GET FRIENDSHIP MATRIX

f in si s< f nt o( {
r ed h p - u c in )
pi t" ee ai g fi ns i mt i"
rn ( Gn rt n re d hp a rx)
N< l nt (r ed .d
- e gh fi n si )
fi ns i. ar x < m t i(,, )
re d hp mt i - ar x0N N
fr ( i 1N {
o i n :)
p it ps ei f in sn m[ ] )
r n (a t( ,r ed . ae i)
t p< f cb o( pt = at (m /u ul r ed " fi nsi [ ] sp"" , ac s _o e
m - a e o k a h p s e " e m t a f i n s , r e d . d i , e =/ )
c es tk
m ta fi ns < sp l (m $a a fn to () xi)
u u lr ed - a py tp d t, uc i nx $d
f in si .a r xi fi ns i %n m ta fi n s < 1
r e dh pm ti [, r ed .d i % u u lr ed ] }
#C et c ne to sw t m fi ns
r a e o nc i n ih y r ed
fi ns cn < c1N
re d _o - ( :)
fi ns cn ]< 1
re d _o [ #A dt i vc o a a c lm t te fi n si m ti
d hs et r s
o un o h r ed hp a rx
fi ns i. ar x < c i df in si .a r xf in sc n
re d hp mt i - bn (r e dh pm ti ,r e d_ o)
#A pn m fi n si w t m s l
p e d y r ed hp i h ye f
fi ns i. ar x < r i df in si .a r xa pn (r e d_ o, )
re d hp mt i - bn (r e dh pm ti ,p e df in sc n 0 )
rw ae (r ed h pm ti )< a pn (r e d. aemn m )
on m sf in si .a r x - pe df in sn m,y ae
cl ae (r ed h pm ti )< a pn (r e d. aemn m )
on m sf in si .a r x - pe df in sn m,y ae
rt r (r ed hpmt i )
eu n fi ns i.a rx
}
FRIEND GRAPH

f in ga h< f nt o( r ed hpmt i ) {
r ed r p - u c in fi ns i.a rx
f i n s i . r p < g a h a j c n y f i n s i . a r x m d = (u d r c e " , w i h e =
re d hp ga h - r p.d ae c ( re d hp mt i, o ec"n i et d) e g tdN
Vf in si .r p )g ne < a pn (r ed .e d r m s l$ ed r
(r e dh pg ah $e d r - p ed fi n sg ne , ye fg ne )
V f i n s i . r p ) l c t o < a p n ( r e d . o a i nn m , m s l $ o a i n n m )
(r e dh pg ah $o a in - pe df i ns lc to . ae y ef lc t o$ ae
V f i n s i . r p ) h m t w < a p n ( r e d . o e o nn m , m s l $ o e o n n m )
(r e dh pg ah $o e on - pe df i ns hm tw . ae y ef hm t w$ ae
Vf in si .r p )L bl < Vf in si . rp )n m
(r e dh pg ah $a e - ( re dh pg ah $ ae
rt r ( re dh p ga h
eu n fi ns i. rp )
}
DRAW FRIENDSHIP GRAPH
f mt i < f in s is )
_ ar x - re dh p(
f ga h< f in ga hf m ti)
_ rp - r ed r p( _a rx
w ie ga hf ga h "r e d. rp m" fr a =g ah l )
r t. r p ( _r p, f in sg a hl ,o mt "r p m"
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

r qi ep y)
e ur ( lr
p ss < f_ on c( ah " ep ss ,a cs _ oe =c es t kn ot o sl s( fe d "" i
o t - bc ne t pt =m /o t " ce st kn a cs _o e, p in =i t "i ls =l
p s. ie < sp l (o t$ aa f nt o( ) xl ks
o tl k s - a py ps s dt , uc in x $i e )
p s. ie .a a< s pl ( ot lk s fn to () x dt )
o tl k sd t - ap yp s. i e, uc i nx $ aa
l =l s (
i
it )
f r( i 1l nt ( ot lk sd t ){
o i n : eg hp s. i e. aa )
fr ( i 1(e g hp s. ie . aa [])1)
o j n :l nt (o t lk sd t [ i]- ){
l < a p n ( i p s . i e . a a [ ] [ j ]n m )
i - p ed l, o tl ks d t[ i] []$a e
}
}
d =d. al ri dd t. r m, l)
f
oc l( bn . aa fa e i
c la e( f < c " ae )
o nm s d) - (n m"
d py d, .n m) s ma i e N m us =l nt (a e )
d l( f ( ae , um rz , uS b
e g hn m)
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

Social Network Analysis With R

  • 1.
    SOCIAL NETWORK ANALYSISWITH R David Chiu @ 中興大學
  • 2.
    ABOUT ME Co-founder ofNumerInfo Trend Micro Engineer ywchiu-tw.appspot.com
  • 3.
    AGENDA What is SocialNetwork? 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
  • 4.
  • 5.
  • 6.
    WHAT DO WEWANT 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?
  • 7.
  • 8.
    LESSON 1: CONNECTTO SOCIAL NETWORK OAuth Flow Oauth v.s. OAuth2 OpenID
  • 9.
    OAUTH FLOW Open standardfor authorization. OAuth provides a method for clients to access server resources on behalf of a resource owner
  • 10.
    DIFFERENCE BETWEEN OAUTHAND 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
  • 11.
    OPENID Open standard thatallows users to be authenticated by certain co-operating sites
  • 12.
  • 13.
  • 14.
  • 15.
    PERMISSION LIST User DataPermissions: 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
  • 16.
  • 17.
    LESSON 2: SOCIALNETWORK ANALYSIS WITH R Let's Hack
  • 18.
    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
  • 19.
    REQUIRED PACKAGE i salp ca e( ( rs n,Ru l ,X L, ir p'' io s) dp ne c e= RE n tl . ak gs c' jo ''C r' 'M ' ' g ah,b tp ' ,e ed ni sT U) l ba yr sn i rr ( jo ) l ba yR ul i rr ( Cr ) l ba yX L i rr ( M) l ba yi rp ) i rr ( ga h l ba yb tp ) i rr ( io s
  • 20.
    FACEBOOK CONNECT f _onc < f nt o ( p t = " e, ac s_ oe = tk n ot os { b cn e t - u c in a h m" ce s tk n o e, p in ) i ( !i sn (p i n) ) f m si go to s { o to s< s rn f "% " ps e nm s o to s ,"" ul s( pi n) c la s =" p i n - p i t( ?s, a t( ae ( pi n) =, ni to t os , ol pe & o to s< g u( ,, "2 " o to s p i n - s b "" %C, p in ) u l< s rn f "t p: / r p .a eo kc m% % &c es tk n % " pt , ot os a c r - p it ( ht s /g ah fc bo . o/ssac s _o e= s, a h pi n , c }e s { le u l< s rn f "t p: / r p .a eo kc m% ? ce st kn %" p t, a cs _o e ) r - p it ( ht s /g ah fc bo . o/sa cs _ oe=s , ah c es t kn } dt < gt R ( u l s l vr fp e =F LE ) aa - e UL r , s. ei y er AS fo JO (d t ) rm S N aa }
  • 21.
    TEST ON APIEXPLORER
  • 22.
    EXECUTED RESULT > f_on c( m" a cs _o e) bc n et "e ,c es t kn $d i [ ]" 72 48 " 1 7 8 28 9 $ ae nm [ ]" ai C i" 1 Dvd hu $ is _a e f rt n m [ ]" ai " 1 Dvd $ at nm l s_ a e [ ]" hu 1 Ci" $ ik ln [ ]" tp :/ w. a eo kc mC i Y" 1 h t s/ ww fc bo . o/ hu W $ sr ae u en m [ ]" hu W 1 C i Y"
  • 23.
    GET FRIEND LIST >fi ns < f_ on c (a h" ef in s,a cs _o e=c es tk n re d - bc n et pt =m / re d" c es t kn ac s_ oe ) > fi ns re d
  • 24.
    GET FRIEND GROUPLIST f in go p < f _ on c( pt = m/ re di t" a cs _ oe =c es tk n r ed r us - bc ne t a h" ef in l ss, ce st kn ac s _o e) f in go p. d < sp l (r ed ru s d t,f nt o()xi) r ed r us i - a py fi n go p$ aa u c inx $d f in go p. ae < sp l (r ed ru s d t,f nt o()xn m) r ed r us nm - a py fi n go p$ aa u c inx $a e
  • 25.
    FACEBOOK ELEMENT EXTRACTOR #Gt Fi ns If r ai nF o F e r ed n om to rm B m < f_ on c( a h" e, ac s _o e= ce st k n o t osls (f ed ""d nm ,o e - b cn et pt =m " c es tk na c s_ oe , pi n=it " il s=i, a el c m nm < m$ ae y ae - e nm f in s< m $r ed r ed - e fi n s # Et at Fc bo I s xr c a eo k D f in si < sp l (r ed $a a fn to ( )x i) r ed . d - a py fi n sd t , u ci nx $ d # Et at F Mt I fr ai n xr c B ea n om t o f in sn m < u l s( ap yf i ns dt ,f nt o () i ov xn m , o' T 8, u= " ) r ed . ae - ni ts p l( re d$ aa uc in x c n( $a e t= UF 's b " ) ) f in sg ne < s pl (r ed $ aa fn t o( )u ls ( fi . u l x gn e ) " o e e r ed . ed r - a py f in sd t, u ci nx ni ti (s nl ( $e dr ) nn " l f in sl ct o. d < u l s( ap yf in s dt,f nt o () xl ct o$ d ) r ed . oa in i - ni ts p l( re d$ aa uc in x $o a in i) f in sl ct o. a e< u ls (a py fi n sd t , fn t o( ){ f( s n l ( $ oa in r ed . oa in nm - n it s pl (r ed $a a u ci nx i i. ulxl c to$ f in sh mt w. a e< u ls (a py fi n sd t , fn t o( ){ f( s n l ( $ oe on r ed . oe on nm - n it s pl (r ed $a a u ci nx i i. ulxh m tw$
  • 26.
    GET FRIENDSHIP MATRIX fin si s< f nt o( { r ed h p - u c in ) pi t" ee ai g fi ns i mt i" rn ( Gn rt n re d hp a rx) N< l nt (r ed .d - e gh fi n si ) fi ns i. ar x < m t i(,, ) re d hp mt i - ar x0N N fr ( i 1N { o i n :) p it ps ei f in sn m[ ] ) r n (a t( ,r ed . ae i) t p< f cb o( pt = at (m /u ul r ed " fi nsi [ ] sp"" , ac s _o e m - a e o k a h p s e " e m t a f i n s , r e d . d i , e =/ ) c es tk m ta fi ns < sp l (m $a a fn to () xi) u u lr ed - a py tp d t, uc i nx $d f in si .a r xi fi ns i %n m ta fi n s < 1 r e dh pm ti [, r ed .d i % u u lr ed ] } #C et c ne to sw t m fi ns r a e o nc i n ih y r ed fi ns cn < c1N re d _o - ( :) fi ns cn ]< 1 re d _o [ #A dt i vc o a a c lm t te fi n si m ti d hs et r s o un o h r ed hp a rx fi ns i. ar x < c i df in si .a r xf in sc n re d hp mt i - bn (r e dh pm ti ,r e d_ o) #A pn m fi n si w t m s l p e d y r ed hp i h ye f fi ns i. ar x < r i df in si .a r xa pn (r e d_ o, ) re d hp mt i - bn (r e dh pm ti ,p e df in sc n 0 ) rw ae (r ed h pm ti )< a pn (r e d. aemn m ) on m sf in si .a r x - pe df in sn m,y ae cl ae (r ed h pm ti )< a pn (r e d. aemn m ) on m sf in si .a r x - pe df in sn m,y ae rt r (r ed hpmt i ) eu n fi ns i.a rx }
  • 27.
    FRIEND GRAPH f inga h< f nt o( r ed hpmt i ) { r ed r p - u c in fi ns i.a rx f i n s i . r p < g a h a j c n y f i n s i . a r x m d = (u d r c e " , w i h e = re d hp ga h - r p.d ae c ( re d hp mt i, o ec"n i et d) e g tdN Vf in si .r p )g ne < a pn (r ed .e d r m s l$ ed r (r e dh pg ah $e d r - p ed fi n sg ne , ye fg ne ) V f i n s i . r p ) l c t o < a p n ( r e d . o a i nn m , m s l $ o a i n n m ) (r e dh pg ah $o a in - pe df i ns lc to . ae y ef lc t o$ ae V f i n s i . r p ) h m t w < a p n ( r e d . o e o nn m , m s l $ o e o n n m ) (r e dh pg ah $o e on - pe df i ns hm tw . ae y ef hm t w$ ae Vf in si .r p )L bl < Vf in si . rp )n m (r e dh pg ah $a e - ( re dh pg ah $ ae rt r ( re dh p ga h eu n fi ns i. rp ) }
  • 28.
    DRAW FRIENDSHIP GRAPH fmt i < f in s is ) _ ar x - re dh p( f ga h< f in ga hf m ti) _ rp - r ed r p( _a rx w ie ga hf ga h "r e d. rp m" fr a =g ah l ) r t. r p ( _r p, f in sg a hl ,o mt "r p m"
  • 29.
  • 30.
    LESSON 3: SOCIALNETWORK VISUALIZATION Using Gephi
  • 31.
    GEPHI Gephi, an opensource graph visualization and manipulation software
  • 32.
    MODULARITY Measure of thestructure 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
  • 33.
    LEARN MORE ABOUTGEPHI https://www.udemy.com/gephi/
  • 34.
    GET LIKES COUNTOF POSTS r qi ep y) e ur ( lr p ss < f_ on c( ah " ep ss ,a cs _ oe =c es t kn ot o sl s( fe d "" i o t - bc ne t pt =m /o t " ce st kn a cs _o e, p in =i t "i ls =l p s. ie < sp l (o t$ aa f nt o( ) xl ks o tl k s - a py ps s dt , uc in x $i e ) p s. ie .a a< s pl ( ot lk s fn to () x dt ) o tl k sd t - ap yp s. i e, uc i nx $ aa l =l s ( i it ) f r( i 1l nt ( ot lk sd t ){ o i n : eg hp s. i e. aa ) fr ( i 1(e g hp s. ie . aa [])1) o j n :l nt (o t lk sd t [ i]- ){ l < a p n ( i p s . i e . a a [ ] [ j ]n m ) i - p ed l, o tl ks d t[ i] []$a e } } d =d. al ri dd t. r m, l) f oc l( bn . aa fa e i c la e( f < c " ae ) o nm s d) - (n m" d py d, .n m) s ma i e N m us =l nt (a e ) d l( f ( ae , um rz , uS b e g hn m)
  • 35.
    CONCLUSION REMARK Interesting FactAbour Friends Value Behind The Network Statstic Is Worth More Than Thousand of Words
  • 36.
    YOUR TURN TODO 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)
  • 37.