diff --git a/Assignment 3.Rmd b/Assignment 3.Rmd index 649407e..2680cdf 100644 --- a/Assignment 3.Rmd +++ b/Assignment 3.Rmd @@ -1,5 +1,12 @@ +--- +output: html_document +--- # Assignment 3 - Social Network Analysis +### HUDK4050 +### jingshu Zhang +### 10/20/2020 + ## Part I Start by installing the "igraph" package. Once you have installed igraph, load the package. @@ -34,7 +41,7 @@ Since our data represnts every time a student makes a comment there are multiple EDGE <- count(D2, comment.to, comment.from) -names(EDGE) <- c("from", "to", "count") +names(EDGE) <- c("to", "from", "count") ``` @@ -90,7 +97,7 @@ 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) +plot(g,layout=layout_as_tree, 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. @@ -105,19 +112,99 @@ In Part II your task is to [look up](http://igraph.org/r/) in the igraph documen * 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 +```{r} +library(igraph) +plot(g,layout=layout_nicely, vertex.color=VERTEX$major, vertex.lable.cex=1, VERTEX.size=EDGE$count*10,margin=-0.1,edge.arrow.size=0.5, edge.arrow.width=0.5, edge.color="grey",vertex.label.color="black") + +```` ## 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. +```{r} +library(dplyr) +library(tidyr) +library(stringr) +library(igraph) + +C1<-read.csv("hudk4050-classes.csv", stringsAsFactors = FALSE, header = TRUE) +C2<-C1 +``` + 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. +```{r} +``` +#Data Tidying +```{r} +colnames(C2) <-C2[1,] +C2<-slice(C2,3:49) +C2<-select (C2,1:8) +C2<- unite(C2,"name",`First Name`,`Last Name`,sep=" ") +C2$name <- str_replace(C2$name, "`","") +C2 <- C2 %>% mutate_at(2:7,list(toupper)) +C2 <- C2 %>% mutate_at(2:7,str_replace_all," ","") +``` +# Data Restructuring +```{r} +C3<- C2 %>% gather(label, class,2:7,na.rm=TRUE,convert=FALSE)%>% select (name,class) +C3$count<-1 +C3<-filter(C3,class!="") +C3<-unique(C3) +C3<-spread(C3,class,count) +rownames(C3)<-C3$name +C3<-select(C3,-name) +C3<-select(C3,-HUDK4050) +C3[is.na(C3)]<-0 +``` +# Matrix Operations +```{r} +C4<-as.matrix(C3) +C4<-C4 %*% t(C4) +``` +# Graphing +```{r} +g1<-graph.adjacency(C4,mode="undirected",diag=FALSE) +plot(g1, layout=layout.fruchterman.reingold, + vertex.size=4, + #degree(g)*0.9, + vertex.label.cex=0.8,vertex.label.color="black",vertex.color="red" ) +``` Once you have done this, also [look up](http://igraph.org/r/) 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** +```{r} +#degree centrality of the nodes +sort(degree(g1), decreasing = TRUE) + +# Betweenness centrality +sort(betweenness(g1), decreasing = TRUE) + +# In the above two parts, I have the same problem as my classmates in the WeChat group. I follow the steps in our class to fix data, but the numbers I get are exactly the same. I cannot observe the degree centrality and Betweenness. At first I used my own method. After I found wrong, I changed my coding to the format shown in coding workout, but the result is still wrong and unobserved. + +# Fixed it!! +#Based on the data, we found that Yifei Zhang is the most central person in the network.She links two groups. She is the best person to help students link to each other and create community + +``` * 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. +```{r} + +# I know I may need to ceate a new data set which include name and interest. I try my best to write the code but it always something wrong. (LOL...) +``` + + ### 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. + +```{r} + +``` + + + + + diff --git a/Assignment-3.html b/Assignment-3.html new file mode 100644 index 0000000..618e0f1 --- /dev/null +++ b/Assignment-3.html @@ -0,0 +1,670 @@ + + + + +
+ + + + + + + + +C3<- C2 %>% gather(label, class,2:7,na.rm=TRUE,convert=FALSE)%>% select (name,class)
+C3$count<-1
+C3<-filter(C3,class!="")
+C3<-unique(C3)
+C3<-spread(C3,class,count)
+rownames(C3)<-C3$name
+C3<-select(C3,-name)
+C3<-select(C3,-HUDK4050)
+C3[is.na(C3)]<-0
+C4<-as.matrix(C3)
+C4<-C4 %*% t(C4)
+g1<-graph.adjacency(C4,mode="undirected",diag=FALSE)
+
+plot(g1, layout=layout.fruchterman.reingold,
+ vertex.size=4,
+ #degree(g)*0.9,
+ vertex.label.cex=0.8,vertex.label.color="black",vertex.color="red" )
+ Once you have done this, also look up how to generate the following network metrics:
#degree centrality of the nodes
+sort(degree(g1), 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
+# Betweenness centrality
+sort(betweenness(g1), 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
+# In the above two parts, I have the same problem as my classmates in the WeChat group. I follow the steps in our class to fix data, but the numbers I get are exactly the same. I cannot observe the degree centrality and Betweenness. At first I used my own method. After I found wrong, I changed my coding to the format shown in coding workout, but the result is still wrong and unobserved.
+
+# Fixed it!!
+#Based on the data, we found that Yifei Zhang is the most central person in the network.She links two groups. She is the best person to help students link to each other and create community
+# I know I may need to ceate a new data set which include name and interest. I try my best to write the code but it always something wrong. (LOL...)
+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.
+