diff --git a/Assignment 3.Rmd b/Assignment 3.Rmd index 649407e..34daa9b 100644 --- a/Assignment 3.Rmd +++ b/Assignment 3.Rmd @@ -6,6 +6,7 @@ Start by installing the "igraph" package. Once you have installed igraph, load t 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""). ```{r} + D1 <- read.csv("comment-data.csv", header = TRUE) ``` @@ -26,6 +27,7 @@ First we will isolate the variables that are of interest: comment.from and comme library(dplyr) D2 <- select(D1, comment.to, comment.from) #select() chooses the columns + ``` Since our data represnts 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. @@ -34,7 +36,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") ``` @@ -103,8 +105,59 @@ plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender, edge.widt In Part II your task is to [look up](http://igraph.org/r/) 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) +```{r} + +#this section is a small dumpster fire... i kept trying to experiment with variations of the graphs to see the effects... a sort of trial and error approach to learning... after a few hours, i learned some... and was grateful for that + + +tkplot(g,vertex.color=VERTEX$gender,edge.width=EDGE$count) + +plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender, edge.width=EDGE$count,vertex.size=EDGE$count*7, edge.arrow.size=.5) + +#https://igraph.org/r/doc/layout.deprecated.html +plot(g,layout=layout.reingold.tilford, vertex.color=VERTEX$gender, edge.arrow.size=.5) + +#https://igraph.org/r/doc/plot.common.html +#circle testing +plot(g,layout=layout.circle, vertex.color=VERTEX$gender, vertex.size=EDGE$count*7, edge.arrow.size=.5) +plot(g,layout=layout.circle, vertex.color=VERTEX$gender, vertex.size=EDGE$count*7, edge.arrow.size=.5, edge.color="darkorange4") +plot(g,layout=layout.circle, vertex.color=VERTEX$gender, vertex.size=EDGE$count*7, edge.arrow.size=.5, edge.color="darkslategrey") +plot(g,layout=layout.circle, vertex.color=VERTEX$gender, vertex.size=EDGE$count*7, edge.arrow.size=.5, edge.color="bisque4") + +#layout.sphere +plot(g,layout=layout.sphere, vertex.color=VERTEX$gender, vertex.size=EDGE$count*7, edge.arrow.size=.5) + +#layout.random +plot(g,layout=layout.random, vertex.color=VERTEX$gender, vertex.size=EDGE$count*7, edge.arrow.size=.5) + +#layout.fruchterman.reingold +plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender, vertex.size=EDGE$count*7, edge.arrow.size=.5) + +#layout.kawai +plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender, vertex.size=EDGE$count*7, edge.arrow.size=.5) + + + +#layout.lgl +plot(g,layout=layout.lgl, vertex.color=VERTEX$gender, vertex.size=EDGE$count*7, edge.arrow.size=.5) + +# +plot(g,vertex.color=VERTEX$gender, gsize = 10, vertex.size=EDGE$count*7, edge.arrow.size=.5) + +``` + * The vertices are colored according to major +```{r} +plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender, edge.width=EDGE$count) + +``` + * The vertices are sized according to the number of comments they have recieved +```{r} + +plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender, edge.width=EDGE$count) + +``` ## Part III @@ -112,12 +165,148 @@ Now practice with data from our class. This data is real class data directly exp 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} + +#questions +#how can i count the number of instances an issue exists in the data? e.g. double space? +#is there a way to have r give feedback on the number of changes it makes with each command? +#is there an easy way to undo a command? (https://stackoverflow.com/questions/3076526/undo-command-in-r) +# + + +#without the code workout... there is a strong possibly that this section would have taken me until president biden's second term. i'm grateful for the opportunity for 'guided learning' by watching you do it... i'm pretty sure the only credit i deserve here is that i am reasonably adept at copying your work. + + +library(tidyr) +library(dplyr) +library(stringr) +library(igraph) + +#examine the 'person network' (pn) dataset for certain Centrality Measures + +#spare import of the data +x_pn <- read.csv("hudk4050-classes.csv") + +#import the data, assign headers, and remove the first record, and, alla Dr Lang... leave everything that looks like a string as a string +pn1 <- read.csv("hudk4050-classes.csv", + skip = 1, + header = TRUE, + stringsAsFactors = FALSE) + +#remove the first record +pn1 <- pn1[-c(1), ] + + +#remove the last column +pn1 <- pn1[c(1:8)] + +#merge the name column +pn2 <- unite(pn1, "Full.Name", `First.Name`, `Last.Name`, sep = " ") + +#clean up Full.Name +#remove `` +pn2$Full.Name <- str_replace(pn2$Full.Name, "`","") +#remove double spaces +pn2$Full.Name <- str_replace(pn2$Full.Name, " "," ") +#remove leading and ending spaces +pn2$Full.Name <- trimws(pn2$Full.Name, which = c("both")) +#make Title Case +pn2$Full.Name <- str_to_title(pn2$Full.Name) + +#copy +pn3 <- pn2 + +#clean up classes columns +#remove spaces +#https://stackoverflow.com/questions/48953295/replace-a-specific-strings-from-multiple-columns-in-a-dataframe +pn3[,2:7] <- apply(pn3[,2:7],2,function(x) gsub(" ",'',x)) + +#remove leading and ending spaces +#busted +#pn3[,2:7] <- trimws(pn3[,2:7], which = c("both")) +#make UPPER Case +#busted +#pn3 <- str_to_upper(pn3[,2:7]) + +#make UPPER Case per Dr. Lang +pn3 <- pn3 %>% mutate_at(2:7, list(toupper)) + +``` + +```{r} +# Data Restructuring + +#create a dataframe with two variables, student and class +pn4 <- pn3 %>% gather(label, class, 2:7, na.rm = TRUE, convert = FALSE) %>% select(Full.Name,class) + +#create a new variable containing 1s that will become our counts +pn4$count <- 1 + +#remove blank classes +pn4 <- filter(pn4, class !="") + +#remove duplicates +pn4 <- unique(pn4) + +#spread 1s across classes to createa student x class dataframe +pn4 <- spread(pn4, class, count) + +#make row names student names +rownames(pn4) <- pn4$Full.Name + +#remove names column and HUDK4050 +pn4 <- select(pn4, -Full.Name, -HUDK4050) + +#replace blanks with zeros +pn4[is.na(pn4)] <- 0 + + +``` + +# Matrix operations +```{r} + +#convert to matrix +pn5 <-as.matrix(pn4) + +#create person-person matrix +pn5 <- pn5 %*% t(pn5) + + +``` + 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} + +g <- graph.adjacency(pn5, mode="undirected", diag = FALSE) + +plot(g,layout=layout.fruchterman.reingold, + vertex.size = 4, + #degree(g)*.7, + vertex.label.cex=0.8, + vertex.label.color="black", + vertex.color="gainsboro") + +``` + * 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} + +#Calculate the degree centrality of the nodes, showing who has the most connections +sort(degree(g), decreasing = TRUE) + + +#Calculate the betweenness centrality, showing how many 'shortest paths' pass through each node +sort(betweenness(g), decreasing = TRUE) + + +``` + + ### 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. diff --git a/Assignment-3.html b/Assignment-3.html new file mode 100644 index 0000000..81238ab --- /dev/null +++ b/Assignment-3.html @@ -0,0 +1,811 @@ + + + + +
+ + + + + + + + +#convert to matrix
+pn5 <-as.matrix(pn4)
+
+#create person-person matrix
+pn5 <- pn5 %*% t(pn5)
+Once you have done this, also look up how to generate the following network metrics:
+g <- graph.adjacency(pn5, mode="undirected", diag = FALSE)
+
+plot(g,layout=layout.fruchterman.reingold,
+ vertex.size = 4,
+ #degree(g)*.7,
+ vertex.label.cex=0.8,
+ vertex.label.color="black",
+ vertex.color="gainsboro")
+#Calculate the degree centrality of the nodes, showing who has the most connections
+sort(degree(g), 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
+#Calculate the betweenness centrality, showing how many 'shortest paths' pass through each node
+sort(betweenness(g), 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
+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.
+