+
Assignment 3 - Social Network Analysis
+
+
Part I
+
Start by installing the “igraph” package. Once you have installed igraph, load the package.
+
Now upload the data file “comment-data.csv” as a data frame called “D1”. Each row represents a comment from one student to another so the first line shows that student “28” commented on the comment of student “21”. It also shows the gender of both students and the students’ main elective field of study (“major”").
+
D1 <- read.csv("comment-data.csv", header = TRUE)
+library(igraph)
+
##
+## Attaching package: 'igraph'
+
## The following objects are masked from 'package:stats':
+##
+## decompose, spectrum
+
## The following object is masked from 'package:base':
+##
+## union
+
Before you proceed, you will need to change the data type of the student id variable. Since it is a number R will automatically think it is an integer and code it as such (look at the list of variables by clicking on the data frame arrow in the Data pane. Here you will see the letters “int”" next to the stid variable, that stands for integer). However, in this case we are treating the variable as a category, there is no numeric meaning in the variable. So we need to change the format to be a category, what R calls a “factor”. We can do this with the following code:
+
D1$comment.to <- as.factor(D1$comment.to)
+D1$comment.from <- as.factor(D1$comment.from)
+
igraph requires data to be in a particular structure. There are several structures that it can use but we will be using a combination of an “edge list” and a “vertex list” in this assignment. As you might imagine the edge list contains a list of all the relationships between students and any characteristics of those edges that we might be interested in. There are two essential variables in the edge list a “from” variable and a “to” variable that descibe the relationships between vertices. While the vertex list contains all the characteristics of those vertices, in our case gender and major.
+
So let’s convert our data into an edge list!
+
First we will isolate the variables that are of interest: comment.from and comment.to
+
library(dplyr)
+
##
+## Attaching package: 'dplyr'
+
## The following objects are masked from 'package:igraph':
+##
+## as_data_frame, groups, union
+
## The following objects are masked from 'package:stats':
+##
+## filter, lag
+
## The following objects are masked from 'package:base':
+##
+## intersect, setdiff, setequal, union
+
D2 <- select(D1, comment.to, comment.from) #select() chooses the columns
+
Since our data represents every time a student makes a comment there are multiple rows when the same student comments more than once on another student’s video. We want to collapse these into a single row, with a variable that shows how many times a student-student pair appears.
+
EDGE <- count(D2, comment.to, comment.from)
+
+names(EDGE) <- c("to", "from", "count")
+
EDGE is your edge list. Now we need to make the vertex list, a list of all the students and their characteristics in our network. Because there are some students who only recieve comments and do not give any we will need to combine the comment.from and comment.to variables to produce a complete list.
+
#First we will separate the commenters from our commentees
+V.FROM <- select(D1, comment.from, from.gender, from.major)
+
+#Now we will separate the commentees from our commenters
+V.TO <- select(D1, comment.to, to.gender, to.major)
+
+#Make sure that the from and to data frames have the same variables names
+names(V.FROM) <- c("id", "gender.from", "major.from")
+names(V.TO) <- c("id", "gender.to", "major.to")
+
+#Make sure that the id variable in both dataframes has the same number of levels
+lvls <- sort(union(levels(V.FROM$id), levels(V.TO$id)))
+
+VERTEX <- full_join(mutate(V.FROM, id=factor(id, levels=lvls)),
+ mutate(V.TO, id=factor(id, levels=lvls)), by = "id")
+
+#Fill in missing gender and major values - ifelse() will convert factors to numerical values so convert to character
+VERTEX$gender.from <- ifelse(is.na(VERTEX$gender.from) == TRUE, as.factor(as.character(VERTEX$gender.to)), as.factor(as.character(VERTEX$gender.from)))
+
+VERTEX$major.from <- ifelse(is.na(VERTEX$major.from) == TRUE, as.factor(as.character(VERTEX$major.to)), as.factor(as.character(VERTEX$major.from)))
+
+#Remove redundant gender and major variables
+VERTEX <- select(VERTEX, id, gender.from, major.from)
+
+#rename variables
+names(VERTEX) <- c("id", "gender", "major")
+
+#Remove all the repeats so that we just have a list of each student and their characteristics
+VERTEX <- unique(VERTEX)
+
Now we have both a Vertex and Edge list it is time to plot our graph!
+
#Load the igraph package
+
+library(igraph)
+
+#First we will make an object that contains the graph information using our two dataframes EDGE and VERTEX. Notice that we have made "directed = TRUE" - our graph is directed since comments are being given from one student to another.
+
+g <- graph.data.frame(EDGE, directed=TRUE, vertices=VERTEX)
+
+#Now we can plot our graph using the force directed graphing technique - our old friend Fruchertman-Reingold!
+
+plot(g,layout=layout.fruchterman.reingold)
+

+
#There are many ways to change the attributes of the graph to represent different characteristics of the newtork. For example, we can color the nodes according to gender.
+
+plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender)
+

+
#We can change the thickness of the edge according to the number of times a particular student has sent another student a comment.
+
+plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender, edge.width=EDGE$count)
+

+
+
+
Part II
+
In Part II your task is to look up in the igraph documentation and modify the graph above so that:
+
+- Ensure that sizing allows for an unobstructed view of the network features (For example, the arrow size is smaller)
+- The vertices are colored according to major
+- The vertices are sized according to the number of comments they have recieved
+
+
comnum <- count(D2, comment.to)
+names(comnum)<- c('id', 'comreceived')
+
+comnum <- right_join(comnum,VERTEX, by=c('id'))
+comnum$comreceived[is.na(comnum$comreceived)]<- 0
+plot(g,layout=layout.fruchterman.reingold, vertex.color = VERTEX$major, edge.with=EDGE$count, vertex.size=20+comnum$comreceived, edge.arrow.size = 0.5)
+

+
+
+
Part III
+
Now practice with data from our class. This data is real class data directly exported from Qualtrics and you will need to wrangle it into shape before you can work with it. Import it into R as a data frame and look at it carefully to identify problems.
+
Please create a person-network with the data set hudk4050-classes.csv. To create this network you will need to create a person-class matrix using the tidyr functions and then create a person-person matrix using t(). You will then need to plot a matrix rather than a to/from data frame using igraph.
+
Once you have done this, also look up how to generate the following network metrics:
+
+Betweeness centrality and dregree centrality. Who is the most central person in the network according to these two metrics? Write a sentence or two that describes your interpretation of these metrics
+Color the nodes according to interest. Are there any clusters of interest that correspond to clusters in the network? Write a sentence or two describing your interpetation.
+
+
library(igraph)
+library(tidyr)
+
##
+## Attaching package: 'tidyr'
+
## The following object is masked from 'package:igraph':
+##
+## crossing
+
library(dbplyr)
+
##
+## Attaching package: 'dbplyr'
+
## The following objects are masked from 'package:dplyr':
+##
+## ident, sql
+
library(stringr)
+cd <- read.csv("hudk4050-classes.csv", stringsAsFactors=FALSE,header=TRUE)
+colnames(cd)<- cd[1,]
+cd <- unite(cd,'name',`First Name`,`Last Name`,sep = " ")
+cd1 <- slice(cd,3:49)
+cd1 <- select(cd1,1:8)
+cd1$name <- str_replace(cd1$name, '`','')
+cd1$name <- str_to_title(cd1$name)
+cd1 <- cd1 %>% mutate_at(2:7, list(toupper))
+cd1 <- cd1 %>% mutate_at(2:7, str_replace_all, " ", "")
+
+cd2 <- cd1 %>% gather(label, class, 2:7, na.rm = TRUE, convert = FALSE) %>% select(class, name)
+cd2$count <- 1
+cd2 <- filter(cd2, class !=" ")
+cd2 <- unique(cd2)
+cd2 <- spread (cd2, class, count)
+
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
+## Using compatibility `.name_repair`.
+## This warning is displayed once every 8 hours.
+## Call `lifecycle::last_warnings()` to see where this warning was generated.
+
rownames(cd2) <- cd2$name
+cd2 <- select(cd2, -name, -HUDK4050)
+cd2[is.na(cd2)]<- 0
+cd2 <- select (cd2, 2:52)
+
+cd3 <- as.matrix(cd2)
+cd3 <- cd3 %*% t(cd3)
+#person-class
+g1 <- graph_from_incidence_matrix(cd2, directed = FALSE)
+#person-person
+g2 <- graph.adjacency(cd3, mode = "undirected", diag = FALSE)
+plot(g1, layout=layout.fruchterman.reingold,
+ vertex.size = 4,
+ #degree(g1)*0.7,
+ vertex.label.cex = 0.8,
+ vertex.label.color = "black",
+ vertex.color = "gainsboro")
+

+
plot(g2, layout=layout.fruchterman.reingold,
+ vertex.size = 4,
+ #degree(g1)*0.7,
+ vertex.label.cex = 0.8,
+ vertex.label.color = "black",
+ vertex.color = "gainsboro")
+

+
sort(degree(g1), decreasing = TRUE)
+
## HUDM5026 HUDM5126 HUDK4029
+## 13 11 10
+## HUDM4125 HUDK4031 Jie Yao
+## 10 7 5
+## HUDM4122 MSTU4000 Dan Lei
+## 5 5 4
+## Danny Shan Fei Wang Rong Sang
+## 4 4 4
+## Yuxuan Ge Zhouda Wu HUDK4011
+## 4 4 4
+## Amanda Oliveira Guoliang Xu Hangshi Jin
+## 3 3 3
+## Jiaao Qi Jiacong Zhu Jiahao Shen
+## 3 3 3
+## Nicole Schlosberg Stanley Si Heng Zhao Wenqi Gao
+## 3 3 3
+## Xiaojia Liu Xijia Wang Xiyun Zhang
+## 3 3 3
+## Xueshi Wang Yifei Zhang Yingxin Xie
+## 3 3 3
+## Yingxin Ye Yixiong Xu Yunzhao Wu
+## 3 3 3
+## Yurui Wang Yuting Zhou Zach Friedman
+## 3 3 3
+## Zhixin Zheng MSTU4133 MSTU5003
+## 3 3 3
+## Ali Al Jabri Fangqi Liu He Chen
+## 2 2 2
+## Hyungoo Lee Kaijie Fang Ruoyi Zhang
+## 2 2 2
+## Shuying Xiong Tianyu Chang Wenning Xiao
+## 2 2 2
+## Yucheng Pan HUDK5020 HUDK5023
+## 2 2 2
+## HUDK5035 HUDM5059 HUDM5123
+## 2 2 2
+## ITSF5006 MSTU4083 Abdul Malik Muftau
+## 2 2 1
+## Berj Akian Jiasheng Yu Mahshad Davoodifard
+## 1 1 1
+## A&HL4000 A&HL4997 A&HL5199
+## 1 1 1
+## A&HL5507 A&HL5675 A&HL6302
+## 1 1 1
+## A&HW4041 CCPX4044 EDPA4033
+## 1 1 1
+## EDPE4056 EDPS4029 HUD4120
+## 1 1 1
+## HUDK4023 HUDK4080 HUDK4120
+## 1 1 1
+## HUDK4122 HUDK5029 HUDK5037
+## 1 1 1
+## HUDM4025 HUDM4120 HUDM41220
+## 1 1 1
+## HUDM4124 HUDM5023 HUDM5122
+## 1 1 1
+## HUDM5150 HUDM6051 ITSF6590
+## 1 1 1
+## MSTM5033 MSTU4005 MSTU4016
+## 1 1 1
+## MSTU5002 MSTU5027 ORLJ6040
+## 1 1 1
+## QQMSGR5073 Chris Kim Qianhui Yuan
+## 1 0 0
+## Sara Vasquez Vidya Madhavan
+## 0 0
+
sort(betweenness(g1),decreasing = TRUE)
+
## HUDK4029 HUDM5026 Yifei Zhang
+## 1536.858964 1250.875761 1063.386015
+## Dan Lei HUDK4031 Stanley Si Heng Zhao
+## 572.148199 501.986134 435.573975
+## Zach Friedman Zhixin Zheng HUDM4125
+## 412.817049 411.330753 401.302852
+## MSTU4000 Nicole Schlosberg HUDM5059
+## 361.229396 346.425236 345.000000
+## Yingxin Ye HUDM5126 HUDK5020
+## 283.000000 280.729484 280.000000
+## HUDK5023 HUDM4122 Danny Shan
+## 271.428160 238.771090 216.000000
+## Jie Yao Yuxuan Ge Fei Wang
+## 184.192063 165.941937 154.395513
+## Amanda Oliveira Xijia Wang HUDM5123
+## 145.000000 145.000000 144.000000
+## Xueshi Wang Yuting Zhou Yixiong Xu
+## 141.103900 112.927032 101.839291
+## Zhouda Wu HUDK4011 Xiaojia Liu
+## 101.369811 96.374176 93.941937
+## Rong Sang MSTU5003 Yunzhao Wu
+## 93.229915 92.030708 80.203064
+## Hyungoo Lee Kaijie Fang Ruoyi Zhang
+## 73.000000 73.000000 73.000000
+## Wenning Xiao Guoliang Xu Hangshi Jin
+## 73.000000 54.420017 54.420017
+## Jiaao Qi Jiacong Zhu Jiahao Shen
+## 54.420017 54.420017 54.420017
+## Wenqi Gao Xiyun Zhang Yingxin Xie
+## 54.420017 54.420017 54.420017
+## MSTU4083 HUDK5035 MSTU4133
+## 50.473810 27.548990 23.390476
+## Yucheng Pan Tianyu Chang ITSF5006
+## 18.611111 7.203064 4.000000
+## Fangqi Liu Shuying Xiong Yurui Wang
+## 3.000000 3.000000 3.000000
+## Ali Al Jabri He Chen Abdul Malik Muftau
+## 1.000000 1.000000 0.000000
+## Berj Akian Chris Kim Jiasheng Yu
+## 0.000000 0.000000 0.000000
+## Mahshad Davoodifard Qianhui Yuan Sara Vasquez
+## 0.000000 0.000000 0.000000
+## Vidya Madhavan A&HL4000 A&HL4997
+## 0.000000 0.000000 0.000000
+## A&HL5199 A&HL5507 A&HL5675
+## 0.000000 0.000000 0.000000
+## A&HL6302 A&HW4041 CCPX4044
+## 0.000000 0.000000 0.000000
+## EDPA4033 EDPE4056 EDPS4029
+## 0.000000 0.000000 0.000000
+## HUD4120 HUDK4023 HUDK4080
+## 0.000000 0.000000 0.000000
+## HUDK4120 HUDK4122 HUDK5029
+## 0.000000 0.000000 0.000000
+## HUDK5037 HUDM4025 HUDM4120
+## 0.000000 0.000000 0.000000
+## HUDM41220 HUDM4124 HUDM5023
+## 0.000000 0.000000 0.000000
+## HUDM5122 HUDM5150 HUDM6051
+## 0.000000 0.000000 0.000000
+## ITSF6590 MSTM5033 MSTU4005
+## 0.000000 0.000000 0.000000
+## MSTU4016 MSTU5002 MSTU5027
+## 0.000000 0.000000 0.000000
+## ORLJ6040 QQMSGR5073
+## 0.000000 0.000000
+
sort(degree(g2), decreasing = TRUE)
+
## Guoliang Xu Hangshi Jin Jiaao Qi
+## 31 31 31
+## Jiacong Zhu Jiahao Shen Wenqi Gao
+## 31 31 31
+## Xiyun Zhang Yingxin Xie Yifei Zhang
+## 31 31 24
+## Xiaojia Liu Yuxuan Ge Zhixin Zheng
+## 22 22 20
+## Stanley Si Heng Zhao Dan Lei Yuting Zhou
+## 19 16 16
+## Xueshi Wang Zhouda Wu Ruoyi Zhang
+## 14 14 12
+## Tianyu Chang Xijia Wang Yunzhao Wu
+## 12 12 12
+## Jie Yao Zach Friedman Nicole Schlosberg
+## 11 11 10
+## Yixiong Xu Berj Akian Kaijie Fang
+## 10 9 9
+## Rong Sang Yucheng Pan Amanda Oliveira
+## 8 7 6
+## Fei Wang Jiasheng Yu Wenning Xiao
+## 6 6 4
+## Yingxin Ye Danny Shan Fangqi Liu
+## 2 1 1
+## Hyungoo Lee Shuying Xiong Abdul Malik Muftau
+## 1 1 0
+## Ali Al Jabri Chris Kim He Chen
+## 0 0 0
+## Mahshad Davoodifard Qianhui Yuan Sara Vasquez
+## 0 0 0
+## Vidya Madhavan Yurui Wang
+## 0 0
+
sort(betweenness(g2),decreasing = TRUE)
+
## Yifei Zhang Stanley Si Heng Zhao Dan Lei
+## 260.6143603 97.2791152 83.4785714
+## Zhixin Zheng Zach Friedman Nicole Schlosberg
+## 66.2352941 43.3856397 36.6078619
+## Yingxin Ye Xueshi Wang Yuting Zhou
+## 34.0000000 24.1453512 19.7898193
+## Zhouda Wu Guoliang Xu Hangshi Jin
+## 8.9230159 7.5944061 7.5944061
+## Jiaao Qi Jiacong Zhu Jiahao Shen
+## 7.5944061 7.5944061 7.5944061
+## Wenqi Gao Xiyun Zhang Yingxin Xie
+## 7.5944061 7.5944061 7.5944061
+## Yixiong Xu Jie Yao Xiaojia Liu
+## 5.0523810 4.4984127 3.2007978
+## Yuxuan Ge Yucheng Pan Abdul Malik Muftau
+## 3.2007978 0.8333333 0.0000000
+## Ali Al Jabri Amanda Oliveira Berj Akian
+## 0.0000000 0.0000000 0.0000000
+## Chris Kim Danny Shan Fangqi Liu
+## 0.0000000 0.0000000 0.0000000
+## Fei Wang He Chen Hyungoo Lee
+## 0.0000000 0.0000000 0.0000000
+## Jiasheng Yu Kaijie Fang Mahshad Davoodifard
+## 0.0000000 0.0000000 0.0000000
+## Qianhui Yuan Rong Sang Ruoyi Zhang
+## 0.0000000 0.0000000 0.0000000
+## Sara Vasquez Shuying Xiong Tianyu Chang
+## 0.0000000 0.0000000 0.0000000
+## Vidya Madhavan Wenning Xiao Xijia Wang
+## 0.0000000 0.0000000 0.0000000
+## Yunzhao Wu Yurui Wang
+## 0.0000000 0.0000000
+
#Guoliang Xu has the highest degree centrality, Yifei Zhang has the most betweenness centrality
+
+cd4 <- select(cd1,name, `Which of these topics is most interesting to you?`)
+names(cd4) <- c("name", "interest")
+cd4$interest <- as.factor(cd4$interest)
+plot(g1,layout=layout.fruchterman.reingold, vertex.color=cd4$interest, vertex.label.cex = 0.3, vertex.size = 30)
+

+
plot(g2,layout=layout.fruchterman.reingold, vertex.color=cd4$interest, vertex.label.cex = 0.3, vertex.size = 30, edge.width = 0.5)
+

+
#both two cluster of interests correspond to the cluster of network
+
+
To Submit Your Assignment
+
Please submit your assignment by first “knitting” your RMarkdown document into an html file and then comit, push and pull request both the RMarkdown file and the html file.
+
+
+