Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 3 fangqi liu #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions Assignment 3.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,151 @@ 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}

comment_received <- count(D1,comment.to)
names(comment_received) <- c('id','n')
join_table <- left_join(VERTEX,comment_received,by = c('id'))
join_table$n <- ifelse(is.na(join_table$n) == TRUE, 0, as.factor(as.character(join_table$n)))

plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$major, edge.arrow.size =0.3, vertex.size=4*join_table$n)



```



## 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.


```{r}
library(tidyr)
library(dplyr)
library(stringr)
library(igraph)

#Input data
C1 <- read.csv("hudk4050-classes.csv", stringsAsFactors = FALSE, header = TRUE)

#Copy to play with data
C2 <- C1

```

#Dara Tyding
```{r}
#Make header first row
colnames(C2) <- C2[1,]
#Remove unwanted rows
C2 <- slice(C2, 3:49)
#Remove last column
C2 <- select(C2, 1:8)
#Merge 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))
#Remove whitespace between letters and numbers in class
C2 <- C2 %>% mutate_at(2:7, str_replace_all, " ","")

```

#Data restructuring
```{r}
#Create a dataframe with 2 variables, student and class
C3 <- C2 %>% gather(label, class, 2:7, na.rm = TRUE, convert = FALSE) %>% select(name, class)

#Crate a new variable containing is that will become our 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

```{r}
#Convert to matrix
C4 <- as.matrix(C3)

#Create person-person matrix
C4 <- C4 %*% t(C4)

```

#Graphing

```{r}
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")

```


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**
#According to these two metrics, Yifei Zhang is the most central person in that this person has high vlues of both degree centrality and betweeness centrality.

#Centrality
```{r}
#Calculating the degree centrality of the nodes, most connections
sort(degree(g), decreasing = TRUE)

#Betweeness centrality, shortest paths
sort(betweenness(g), decreasing = TRUE)

```



* 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.

#There is no cluter of interest in this network in that students who connnected to each other do not share common interests as shown by different interest colors.

```{r}
C5 <- C1
C5 <- slice(C5, 3:49)
C5 <- select(C5, c(1,2,9))
C5 <- unite(C5, "name", `Q8`, `Q9`, sep = " ")
C2$name <- str_replace(C2$name, "`", "")
C2$name <- str_to_title(C2$name)
colnames(C5) <- c("name","interest")
interestcolor <- as.factor(C5$interest)
plot(g, layout=layout.fruchterman.reingold,
vertex.size=6,
vertex.label.cex=0.8,
vertex.label.color="black",
vertex.color=interestcolor)

```


### 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.
Loading