Transcript Chapter 11

Social Network Analysis

우영상

목 차 1. Network of Terms 2. Network of Tweets 3. Two-Mode Network

Social Network Analysis • • • • igraph 패키지를 이용하여 일반적인 Social Network 상 에서 발생하는 용어를 분석한다.

같은 Tweet 상에서 발생하는 용어들이 형성하는 Network을 파악한다.

Network 상에서 용어들이 어떻게 공유되는지 파악하기 위한 그래프를 그린다.

용어와 Tweet으로 구성된 Two-Mode Network Graph 를 그린다.

Network of Terms • • • 동시에 등록되는 Tweet을 기반으로하여 Tweet에 생성 되는 용어들의 연관관계를 Network으로 표현.

용어들의 관계를 adjacency matrix표현.

자주 생성되는 용어들의 관계를 그래프로 표현.

Network of Terms • • • • adjacency matrix을 구하기 Boolean matrix로 변환.

– termDocMatrix[termDocMatrix>=1] <- 1 adjacency matrix을 구한다.

– termMatrix <- termDocMatrix %*% t(termDocMatrix) Adjacency Matrix를 통해서 igraph를 만든다.

– g <- graph.adjacency(termMatrix, weighted=T, mode = "undirected") Loop을 제거한다.

– g <- simplify(g)

Network of Terms • • • layout – – 그래프를 그리기 위한 Coordinate을 제공 termDocMatrix[termDocMatrix>=1] <- 1 Fruchterman and Reingold Algorithm을 통해서 Graph 생성 • layout1 <- layout.fruchterman.reingold(g) kamada.kawai 알고리즘으로 Graph생성.

– – – plot(g, layout=layout.kamada.kawai) Loop을 제거한다.

g <- simplify(g)

Network of Terms • • 각각의 Vertex의 Degree에 따라서 Font 사이즈를 조정할 수 있다.

V()와 E()을 통해서 Vertex와 Edge의 속성을 변경할 수 있다.

– V(g)$label.cex <- 2.2 * V(g)$degree / max(V(g)$degree) + .2

V(g)$label.color <- rgb(0,0,.2,.8) V(g)$frame.color <- NA egam <- (log(E(g)$weight)+.4) / max(log(E(g)$weight)+.4) E(g)$color <- rgb(.5,.5,0,egam) E(g)$width <- egam plot(g, layout=layout1)

Network of Terms

Network of Tweet • 기존 Network of Terms에서 공통적으로 있는 단어를 배 제하고 Network을 형성함으로서 간단하게 공통적인 단 어와 함께 연관되는 단어들의 찾을 수 있다.

Network of Tweet • • • • termDocMatrix의 Terms Column에서 r, data, mining 단어의 index를 추출한다.

– idx <- which(dimnames(termDocMatrix)$Terms %in% c("r","data","mining")) r, data, mining 단어를 삭제한다.

– M <-termDocMatrix[-idx,] Adjacency Matrix를 생성한다.

– tweetMatrix <- t(M) %*% M tweetMatrix에서 igraph를 생성한다.

– g <- graph.adjacency(tweetMatrix, weighted=T, mode = "undirected")

Network of Tweet • Vertex의 Degree를 Bar Graph로 표현한다 – barplot(table(V(g)$degree))

Network of Tweet • • • • Vertex의 Degree에 따라서 그 색을 달리 하여 그래프를 그린다.

Tweet ID가 고립된 Vertex의 집합과 빈도수가 많은 20개의 그래프 를 그린다.

나머지 Vertex는 Tweeter ID만 출력하게 된다.

각 Edge의 색은 그 Edge의 weight에 따라 Alpha값이 바뀐다.

idx <- V(g)$degree == 0 V(g)$label.color[idx] <- rgb(0,0,.3,.7) V(g)$label[idx] <-paste(V(g)$name[idx], substr(df$text[idx],1,20), sep=": ") egam <- (log(E(g)$weight)+ .2)/ max(log(E(g)$weight)+.2) E(g)$color <- rgb(.5, .5, 0, egam) E(g)$width <- egam set.seed(3152) layout2 <- layout.fruchterman.reingold(g) plot(g, layout = layout2)

Network of Tweet

Network of Tweet • delete.vertices를 통해서 초승달 모양으로 고립된 Vertex 를 삭제한다.

g2 <- delete.vertices(g, V(g)[degree(g)==0]) plot(g2, layout=layout.fruchterman.reingold) • Degree가 낮은 Edge를 삭제함으로서 graph를 좀더 간 단하게 만든다.

g3 <- delete.edges(g, E(g)[E(g)$weight <= 1]) g3 <- delete.vertices(g3, V(g3)[degree(g3) == 0]) plot(g3, layout=layout.fruchterman.reingold)

Network of Tweet

Network of Tweet

Two-Mode Network • • Tweet과 Terms이 두 가지 타입으로 이뤄 진 Two-mode Network을 만든다.

Two-Mode Network 서로 다른 두 개의 Node Set을 일정한 기준의 묶는다.

Two-Mode Network • • pipartite igraph 을 생성 mode는 Edge의 Direction을 나타내며, all은 mutual edge가 만들어진다.

g<- graph.incidence(termDocMatrix, mode = c("all")) nTerms <- nrow(M) • r에 이웃한 모든 Vertex를 반환받는다 V(g)[nei("r")] • “r” 과 “data”와 “mining” 세 단어가 포함된 자료는 다음과 같이 확인할 수 있다.

(rdmVertices <- V(g)[nei("r") & nei("data") & nei("mining")]) df$text[as.numeric(rdmVertices$label)]

Two-Mode Network

Two-Mode Network

Two-Mode Network

Two-Mode Network

Q&A