การสร้างกราฟจากข้อมูลดิบ

กรณีศึกษา: การวิเคราะห์เครือข่ายใน R

Edmund Hart

Instructor

สำรวจข้อมูล

  • ข้อมูลคือทวีตทั้งหมดที่กล่าวถึง #rstats ในช่วงหลายวัน
  • แอตทริบิวต์หลักสำหรับการสร้างกราฟ ได้แก่:
    • screen name
    • ข้อความดิบของทวีต
กรณีศึกษา: การวิเคราะห์เครือข่ายใน R

โครงสร้างของทวีต

  1. ReecheshJC: "Hey #rstats, how do I do fct_lump but where I lump based on count values in a column?"
  2. kom_256: "RT @elenagbg: Retweeted R-Ladies Madrid (@RLadiesMAD):\n\nEn el #OCSummit17... Fast Talks sobre #rstats organizado por... https://t.co/CKY5aG…"
กรณีศึกษา: การวิเคราะห์เครือข่ายใน R
library(igraph)
library(stringr)
raw_tweets <- read.csv("datasets/rstatstweets.csv", 
  stringsAsFactors = FALSE)

ตัวอย่างข้อมูล แถวเดียว

user_name:    Karen Millidine
screen_name:    KJMillidine
tweet_tex:t    RT @Rbloggers: RStudio v1.1 Released 
https://t.co/kCMHc689nY #rstats #DataScience
favorites:    0
retweets:    96
location:    None
expanded_url:    https://wp.me/pMm6L-ExV
in_reply_to_tweet_id:    NA
in_reply_to_user_id:    NA
dt:    10/10/17
กรณีศึกษา: การวิเคราะห์เครือข่ายใน R

สร้างกราฟ

## Get all the screen names
all_sn <- unique(raw_tweets$screen_name)

## Create graph
retweet_graph <- graph.empty()

## Add screen names as vertices
retweet_graph <- retweet_graph + vertices(all_sn)
กรณีศึกษา: การวิเคราะห์เครือข่ายใน R

สร้างกราฟ

## Extract name and add edges
for(i in 1:dim(raw_tweets)[1]){

# Extract retweet name rt_name <- find_rt(raw_tweets$tweet_text[i]) # If there is a name add an edge if(!is.null(rt_name)){
# Check to make sure the vertex exists, if not, add it if(!rt_name %in% all_sn){ retweet_graph <- retweet_graph + vertices(rt_name) }
# add the edge retweet_graph <- retweet_graph + edges(c(raw_tweets$screen_name[i], rt_name))
}
}
กรณีศึกษา: การวิเคราะห์เครือข่ายใน R

ทำความสะอาดกราฟ

## Size the number of degree 0 vertices
sum(degree(retweet_graph) == 0) 

## Trim and simplify
retweet_graph <- simplify(retweet_graph)
retweet_graph <- delete.vertices(retweet_graph, 
  degree(retweet_graph) == 0)
กรณีศึกษา: การวิเคราะห์เครือข่ายใน R

มาฝึกกันเถอะ!

กรณีศึกษา: การวิเคราะห์เครือข่ายใน R

Preparing Video For Download...