forked from core-methods-in-edm/assignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment 2.Rmd
155 lines (96 loc) · 6.95 KB
/
Assignment 2.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Assignment 2 - Social Network Analysis
## Part I
Start by installing the "igraph" package. Once you have installed igraph, load the package.
Now upload the data file "discipline-data.csv" as a data frame called "D1". Each row is a disciplinary action from a teacher to a student so the first line shows that teacher "E" sent student "21" to the principal. It also shows the gender of both the teacher and student and the student's main elective field of study ("major"") and the field that the teacher instructs in ("t.expertise").
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:
```{r}
D1<-read.csv("discipline-data.csv", header = TRUE)
D1$stid <- as.factor(D1$stid)
```
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". As you might imagine the edge list contains a list of all the relationships between students and teachers 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 (a disciplinary action is given "from" and teacher "to" a student). While the vertix 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: tid and stid
```{r}
library(dplyr)
D2 <- select(D1, tid, stid)
```
Since our data represnts every time a teacher sends a student to the principal there are multiple rows when the same teacher sends the same student. We want to collapse these into a single row, with a variable that shows how many times a teacher-student pair appears.
```{r}
EDGE <- count(D2, tid, stid)
names(EDGE) <- c("from", "to", "count")
```
EDGE is your edge list. Now we need to make the vertex list, a list of all the teachers and students and their characteristics in our network.
```{r}
#First we will separate the teachers from our original data frame
V.TCH <- select(D1, tid, t.gender, t.expertise)
#Remove all the repeats so that we just have a list of each teacher and their characteristics
V.TCH <- unique(V.TCH)
#Add a variable that describes that they are teachers
V.TCH$group <- "teacher"
#Now repeat this process for the students
V.STD <- select(D1, stid, s.gender, s.major)
V.STD <- unique(V.STD)
V.STD$group <- "student"
#Make sure that the student and teacher data frames have the same variables names
names(V.TCH) <- c("id", "gender", "topic", "group")
names(V.STD) <- c("id", "gender", "topic", "group")
#Bind the two data frames together (you will get a warning because the teacher data frame has 5 types of id (A,B,C,D,E) and the student has 25 (1-30), this isn't a problem)
VERTEX <- bind_rows(V.TCH, V.STD)
```
Now we have both a Vertex and Edge list it is time to plot our graph!
```{r}
#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 discipline is being given from a teacher to a student.
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 teacher has sent a particular student to the principal.
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](http://igraph.org/r/) in the igraph documentation and create a graph that sizes the student vertices in terms of the number of disciplinary actions they have recieved, and the teachers in terms of the number of disciplinary actions they have given out.
```{r}
?count
library (tidyr)
sum.t <- EDGE%>%group_by(from)%>%summarize(sum(count))
names(sum.t) <- c("id", "count")
sum.s <- EDGE%>%group_by(to)%>%summarize(sum(count))
names(sum.s) <- c("id", "count")
total <- bind_rows(sum.t, sum.s)
vertex2 <-full_join(VERTEX, total, by ="id")
g2 <- graph.data.frame(EDGE, directed=TRUE, vertices=vertex2)
plot(g2,layout=layout.fruchterman.reingold, vertex.color=vertex2$gender,vertex.size=vertex2$count)
```
## Part III
Now practice with data from our class. 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 data frame using igraph.
Once you have done this, also [look up](http://igraph.org/r/) how to generate the following network metrics: betweeness centrality and dregree. **Who is the most central person in the network?**
```{r}
classdata <- read.csv("hudk4050-classes.csv",header=TRUE, na.strings = "")
classdata2 <-classdata%>%gather(classtype, coursename, Class.1, Class.2, Class.3, Class.4, Class.5, Class.6) %>% select(-c(classtype)) %>% filter(First.Name != "ZIMO")
classdata3 <- classdata2%>%unite(Name,First.Name,Last.Name,sep = "_")
classdata3$coursename <- gsub(" ", "", classdata3$coursename)
classdata3$count <-1
classdata3 <- classdata3 %>% filter(coursename != "HUDK4050")
classdata4 <- classdata3 %>% spread(coursename, count)
row.names(classdata4) <- classdata4$Name
classdata4$Name <- NULL
classdata4 <- ifelse (is.na(classdata4), 0, 1)
classmatrix <- as.matrix(classdata4)
classmatrixa <- t(classmatrix)
classmatrix2 <- classmatrix%*%classmatrixa
diag(classmatrix2) <- NA
classgraph <- graph.adjacency(classmatrix2, mode = "undirected")
plot(classgraph,layout=layout.fruchterman.reingold, vertex.size=5)
degree(classgraph)
betweenness(classgraph)
which.max(degree(classgraph))
which.max(betweenness(classgraph))
```
```{r}
#Lintong has the highest overlap with other classmates, which means she shares most of her classes with other classmates.
```
### 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.