Twitter network analysis

Analyzing Social Media Data in R

Sowmya Vivek

Data Science Coach

Lesson overview

  • Understand the concepts of networks
  • Application of network concepts to social media
  • Create a retweet network for a topic
Analyzing Social Media Data in R

Network and network analysis

Network

Analyzing Social Media Data in R

Network and network analysis

Network analysis

Analyzing Social Media Data in R

Components of a network

Node or Vertex

Analyzing Social Media Data in R

Components of a network

Node or Vertex with Edge

Analyzing Social Media Data in R

Directed vs undirected network

Directed network

Analyzing Social Media Data in R

Directed vs undirected network

Undirected network

Analyzing Social Media Data in R

Applications in social media

  • Twitter users create complex network structures
  • Analyze the structure and size of the networks
  • Identify key players and influencers in a network
  • Pivotal to transmit information to a wide audience
Analyzing Social Media Data in R

Retweet network

  • Network of users who retweet original tweets posted
  • A directed network where the source vertex is the user who retweets
  • Target vertex is the user who posted the original tweet
  • Position on a retweet network helps identify key players to spread brand messaging
Analyzing Social Media Data in R

Retweet network of #OOTD

  • Create a retweet network of users who retweet on #OOTD
  • This hashtag is popular amongst users in the age group 16-24
  • Can be used to grab the attention of potential customers
Analyzing Social Media Data in R

Create the tweet data frame

# Create tweet data frame for tweets on #OOTD
twts_OOTD <- search_tweets("#OOTD ", n = 18000, include_rts = TRUE)
Analyzing Social Media Data in R

Create data frame for the network

# Create data frame for the network
rt_df <- twts_OOTD[, c("screen_name" , "retweet_screen_name" )]
head(rt_df,10)
screen_name      retweet_screen_name
<chr>                   <chr>
ShesinfashionCc          NA            
glamwearplanet           NA            
lanacond0r         LiveKellyRyan            
animeninjaz              NA            
zeluslondon              NA            
IonaJaneLevy             NA
Analyzing Social Media Data in R

Include only retweets in the data frame

# Remove rows with missing values
rt_df_new <- rt_df[complete.cases(rt_df), ]
Analyzing Social Media Data in R

Convert data frame to a matrix

# Convert to matrix
matrx <- as.matrix(rt_df_new)
Analyzing Social Media Data in R

Create the retweet network

# Create the retweet network
library(igraph)
nw_rtweet <- graph_from_edgelist(el = matrx, directed = TRUE)
Analyzing Social Media Data in R

View the retweet network

# View the retweet network
print.igraph(nw_rtweet)
Analyzing Social Media Data in R

View the retweet network

IGRAPH 7f42937 DN-- 4100 4616 -- 
+ attr: name (v/c)
+ edges from 7f42937 (vertex names):
 [1] MaikielYungin  ->ZingletC        MaikielYungin  ->ZingletC       
 [3] victoria_shop_1->victoria_shop_1 victoria_shop_1->victoria_shop_1
 [5] victoria_shop_1->victoria_shop_1 victoria_shop_1->victoria_shop_1
 [7] victoria_shop_1->victoria_shop_1 victoria_shop_1->victoria_shop_1
 [9] victoria_shop_1->victoria_shop_1 w3daily        ->RealFirstBuzz  
[11] w3daily        ->RealFirstBuzz   w3daily        ->RealFirstBuzz  
[13] w3daily        ->RealFirstBuzz   w3daily        ->RealFirstBuzz  
[15] w3daily        ->RealFirstBuzz   w3daily        ->RealFirstBuzz  
Analyzing Social Media Data in R

Let's practice!

Analyzing Social Media Data in R

Preparing Video For Download...