forked from core-methods-in-edm/assignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment 2.Rmd
287 lines (168 loc) · 9.33 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
---
output:
html_document:
keep_md: yes
toc: yes
word_document: default
pdf_document: default
---
# Assignment 2 - Social Network Analysis
## Part I
Start by installing the "igraph" package. Once you have installed igraph, load the package.
```{r}
#install.packages("igraph")
D1<-read.csv("discipline-data.csv",header = TRUE)
library(igraph)
```
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}
#Get the imported data file called D1
D1$stid <- as.factor(D1$stid)
#then we have a D1 file and edited it
View(D1)
```
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}
# Then we have dplyr function need to use
library(dplyr)
# we have the new D2 new data file
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.
gender_colours =ifelse(VERTEX$gender == "female", yes="blue",no = "red")
#inclue colours in the vertex DF
VERTEX = cbind(VERTEX,gender_colours)
plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender_colours)
#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_colours, 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}
#We have to count the number of the time
#So, we have to get teachers and students sum
student_sum<- EDGE %>% group_by(to) %>% summarise(sum(count))
names(student_sum)<-c("id","count")
teacher_sum<-EDGE %>% group_by(from) %>% summarise(sum(count))
names(teacher_sum)<-c("id","count")
#Make a new Vertex
VERTEX_new<-bind_rows(student_sum,teacher_sum)
VERTEX_new
#VERTEX
library(igraph)
#Make new graph DF
g<- graph.data.frame(EDGE,directed = TRUE,vertices = VERTEX_new)
#Time to plot
plot(g,layout = layout.fruchterman.reingold,
edge.width = EDGE$count,
vertex.size = VERTEX_new$count*2)
```
## 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.
```{r}
#load package we instaled
library(igraph)
library(dplyr)
library(tidyr)
#load the data file
HUDK_data<-read.csv("hudk4050-classes Add CRNS.csv",header = TRUE)
#selcet varibles and clean spaces
Class_kind<- HUDK_data %>% select(First.Name = 'First.Name',
Last.Name ='Last.Name',
class1 = 'Class.1',
class2 = 'Class.2',
class3 = 'Class.3',
class4 = 'Class.4',
class5 = 'Class.5',
class6 = 'Class.6'
)
Class_kind<-as.data.frame(Class_kind)
#GET SEP
Class_kind<-unite(Class_kind,Name,"First.Name","Last.Name",sep="")
#Filter
#For our convience, I just add the HUDK in front of the ZIMO
Class_gather<- gather(Class_kind,class.number,class.Name,-Name)
Class_gather<-filter(Class_gather,class.Name>0)
Class_gather<-select(Class_gather,Name,class.Name)
# We need to aviod the gap on the course names, so we need coherence them
gather_class<-function(a){gsub("","",a)}
Class_gather<-lapply(Class_gather,gather_class)
Class_gather$count<-1
Class_gather<-as.data.frame(Class_gather)
Class_gather<-filter(Class_gather,class.Name!="HUDK4050")
Class_gather$class.Name<- gsub(pattern = "QMSS-",
replacement = "",
x=Class_gather$class.Name)
Class_gather$class.Name <- gsub(pattern = "QMSS",
replacement = "G",
x = Class_gather$class.Name)
Class_gather$class.Name <- gsub(pattern = "GG",
replacement = "G",
x = Class_gather$class.Name)
Class_gather$class.Name <- gsub(pattern = "GR",
replacement = "G",
x = Class_gather$class.Name)
#The row of output must be identified by a unique key
Class_gather<-unique(Class_gather)
#Then we need to make matrix
Class_gather_matrix<- spread(Class_gather,class.Name,count,fill=0)
#Rename
row.names(Class_gather_matrix)<- Class_gather_matrix$Name
Class_gather_matrix$Name<-NULL
Class_gather_matrix<-as.matrix(Class_gather_matrix)
Class_gather_matrix2<-Class_gather_matrix %*% t(Class_gather_matrix)
# Then aviod problem change to NA
diag(x=Class_gather_matrix2)<- NA
#Get a new graph data
Graph_data<- graph.adjacency(Class_gather_matrix2,mode = "undirected")
#Plog into data
plot.igraph(Graph_data, layout = layout.fruchterman.reingold, vertex.size = 15,
vertex.label.cex = 0.5)
```
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}
#betweens centrality
centrality<- betweenness(Graph_data)
centrality[centrality == max(centrality)]
```
```{r}
#dregree
degrees <- degree(Graph_data)
degrees[degrees == max(degrees)]
```
### To Submit Your Assignment
Please submit your assignment by first "knitting" your R Markdown document into an html file and then comit, push and pull request both the RMarkdown file and the html file.