diff --git a/Assignment 3.Rmd b/Assignment 3.Rmd index 649407e..db2f831 100644 --- a/Assignment 3.Rmd +++ b/Assignment 3.Rmd @@ -106,18 +106,120 @@ In Part II your task is to [look up](http://igraph.org/r/) in the igraph documen * The vertices are colored according to major * The vertices are sized according to the number of comments they have recieved +```{r} +plot(g, edge.arrow.size=.4, edge.curved=.1) +``` + +```{r} +colrs <- c("gray50", "pink", "gold") + +V(g)$color <- colrs[V(g)$major] + +E(g)$arrow.size <- .2 + +V(g)$size <- V(g)$comment.to*0.7 + +E(g)$width <- 1+E(g)$comment.from/12 + +plot(g) + + +``` + + +```{r} +plot(g, vertex.shape="none", vertex.label=V(g)$first.name, + + vertex.label.font=2, vertex.label.color="gray40", + + vertex.label.cex=.7, edge.color="gray85") +``` + + + ## 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](http://igraph.org/r/) how to generate the following network metrics: +library(tidyr) +library(dplyr) +library(stringr) +library(igraph) + + +#Input Data + +C1 <- read.csv("hudk4050-classes.csv", stringsAsFactors = FALSE, header = TRUE) + +C2 <- C1 +View(C2) + + +# Data Tidying +# Make header first row +colnames(C2) <- C2[1,] + +# Remove unwanted column +C2 <- slice(C2, 3:49) + +# Remove last column +C2 <- select(C2, 1:8) + +# Merger name columns +C2 <- unite(C2, "name", 'First Name','Last Name', sep =" ") + +# Remove unpredictable characters from names +C2$name <- str_replace(C2$name, "`","") + +# Make all names capitalized first letters only +C2$name <- str_to_title(C2$name) + +# Make all class letters capitals +C2 <- C2 %>% mutate_at(2:7, list(toupper) -* 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** +# Remove whitespace between letters and numbers in class +C2 <- C2 %>% mutate_at(2:7, str_replace_all, " ", "") -* 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. +# Data restructing +#Create a new variable with two variables, student and class +C3 <- C2 %>% gather(label1, class, 2:7, na.rm = TRUE, convert =FALSE) %>% select(name, class) + +#Create a new variable containing 1s that will become counts +C3$count <- 1 + +#Remove blank classes +C3 <- filter(C3, class != "") + +#Remove duplicates (Danny!) +C3 <-unique(C3) + +#Spread 1s across classes to create a student x class dataframe +C3 <- spread(C3, class, count) + +#Make row names student names +rownames(C3) <- C3$name + +#Remove names column AND HUDK 4050 +C3 <- select(C3, -name, -HUDK4050) +#Shortest: +C3[is.na(C3)] <- 0 + +#Matrix Operations +#Convert to Matrix +C4 <- as.matrix(C3) +C4 <- C4 %*% t(C4) + +#Graphing +g <- graph.adjacency(C4, mode="undirected", diag =FALSE) +plot(g, layout=layout.fruchterman.reingold, vertex.size=4, vertex.label.cex=0.8,vertex.label.color="black",vertex,color="gainsboro") + +#Centrality +```{r} +sort(degree(g), decreasing= TRUE) +sort(betweenness(g),decreasing= TRUE) +``` -### To Submit Your Assignment +Yifei has the highest betweeness in centrality measurement because she gets the score of 260.614, which is a lot higher than other students. This implies that she could be the connect person for our class. And there is a couple of students (i.e.Guoliang, Hangshi, etc) has the high degree. -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..c2e6891 --- /dev/null +++ b/Assignment-3.html @@ -0,0 +1,608 @@ + + + + +
+ + + + + + + + +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\(name <- str_to_title(C2\)name)
+C2 <- C2 %>% mutate_at(2:7, list(toupper)
+C2 <- C2 %>% mutate_at(2:7, str_replace_all, " “,”")
+#Create a new variable with two variables, student and class C3 <- C2 %>% gather(label1, class, 2:7, na.rm = TRUE, convert =FALSE) %>% select(name, class)
+#Create a new variable containing 1s that will become counts C3$count <- 1
+#Remove blank classes C3 <- filter(C3, class != "")
+#Remove duplicates (Danny!) C3 <-unique(C3)
+#Spread 1s across classes to create a student x class dataframe C3 <- spread(C3, class, count)
+#Make row names student names rownames(C3) <- C3$name
+#Remove names column AND HUDK 4050 C3 <- select(C3, -name, -HUDK4050) #Shortest: C3[is.na(C3)] <- 0
+#Matrix Operations #Convert to Matrix C4 <- as.matrix(C3) C4 <- C4 %*% t(C4)
+#Graphing g <- graph.adjacency(C4, mode=“undirected”, diag =FALSE) plot(g, layout=layout.fruchterman.reingold, vertex.size=4, vertex.label.cex=0.8,vertex.label.color=“black”,vertex,color=“gainsboro”)
+#Centrality
+sort(degree(g), decreasing= TRUE)
+## 7 16 15 27 21 24 4 23 29 18 28 6 17 20 26 25 11 5 2 19 12 3 10 1 22 8
+## 8 7 6 6 6 6 5 5 5 5 4 4 4 4 4 4 3 3 3 3 3 2 2 2 2 2
+## 14 13 9
+## 2 1 1
+sort(betweenness(g),decreasing= TRUE)
+## 7 16 23 20 24 26 27
+## 127.666667 119.916667 80.333333 79.166667 71.833333 65.833333 61.333333
+## 18 15 21 6 28 10 12
+## 54.000000 47.583333 27.916667 17.000000 16.000000 15.833333 12.000000
+## 29 4 19 3 11 17 5
+## 6.583333 6.500000 4.500000 0.000000 0.000000 0.000000 0.000000
+## 2 13 1 9 22 25 8
+## 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
+## 14
+## 0.000000
+Yifei has the highest betweeness in centrality measurement because she gets the score of 260.614, which is a lot higher than other students. This implies that she could be the connect person for our class. And there is a couple of students (i.e.Guoliang, Hangshi, etc) has the high degree.
+