-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploratory.R
237 lines (168 loc) · 5.8 KB
/
exploratory.R
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
source("C:/Users/Cody/Dropbox/functions.R")
setwd("C:/Users/Cody/Dropbox/debate_r")
setwd("/Users/codycrunkilton/Dropbox/debate_r")
library(ggraph)
library(tidygraph)
library(tidyverse)
# looking at stuff --------------------------------------------------------
season <- read_csv("season_2019.csv") %>% rename(judge_id = judge)
season
# formatting df -----------------------------------------------------------
## making team identifiers
season %>%
select(school, schoolname, first, last) %>% unique
## making sure there are not duplicate names
season %>%
select(school, schoolname) %>% unique %>%
group_by(school) %>%
summarise(n = n_distinct(schoolname)) %>%
arrange(n %>% desc)
## creating identifiers
team_ids <- season %>%
select(school, schoolname, fullname) %>% unique %>%
mutate(team = paste0(schoolname, " ") %>%
paste0(fullname %>% substr(1,2), fullname %>% str_remove(".* & ") %>% substr(1,2)),
team_id = seq(1, nrow(.), 1))
## creating judge names
judge_ids <- season %>%
select(j_first, j_last, j_id) %>% unique %>%
mutate(judge = paste(j_first, j_last))
## splitting down to only the team results, not individual ones, and merging correct team identifiers
season2 <- season %>%
select(-c(entry_student_id, first, last)) %>%
left_join(team_ids) %>%
left_join(judge_ids)
season2
## making dataframe more usable
s <- season2 %>%
select(tournname, eventname, school, team, team_id, judge, judge_id, panel, side, win, timeslotname, rd_name, judgesperpanel, schoolname, fullname) %>%
rename(tournament = tournname, division = eventname, round = timeslotname, round_number = rd_name) %>%
unique
# mutate(elim = case_when(judgesperpanel > 1 ~ "elim",
# judgesperpanel == 1 ~ "prelim",
# TRUE ~ "error"))
## number of rounds and win percent
s %>%
filter(side %>% is.na == F) %>%
group_by(team) %>%
summarise(winpct = mean(win, na.rm = T),
rounds = n_distinct(panel)) %>%
arrange(rounds %>% desc, winpct %>% desc)
## 16k rounds this year
## win percent: about 50-50, out of 7500 rounds
s %>%
mutate(side = case_when(side == 1 ~ "Aff",
side == 2 ~ "Neg")) %>%
filter(side %>% is.na == F & judge != "NA NA") %>%
group_by(side) %>%
summarise(win = mean(win, na.rm = T),
rounds = n_distinct(panel))
## looking at a person
season %>%
filter(fullname %>% str_detect("Kostelny") == T) %>%
select(j_first, j_last, round, win, tournname) %>%
print(n = Inf)
## most times judged by someone
s %>%
filter(judge != "NA NA") %>%
group_by(team, judge) %>%
summarise(ct = n(),
winpct = mean(win)) %>%
arrange(ct %>% desc) %>%
print(n = 30)
s %>%
filter(judge == "David Cram Helwich") %>%
group_by(team) %>%
summarise(win = mean(win),
rounds = n()) %>%
arrange(rounds %>% desc)
# Networks ----------------------------------------------------------------
library(tidygraph)
library(igraph)
d <- s %>%
filter(team %>% str_detect("BYE") == F &
judge %>% str_detect("NA NA") == F)
## number of judges and teams
d %>%
summarise(
n_judges = n_distinct(judge),
n_teams = n_distinct(team)
)
## Who has been judged by whom?
edgelist <- d %>%
# filter(judge %>% str_detect("Crunkilton") == T) %>%
# filter(judge != -1) %>%
group_by(team, judge) %>%
summarise(weight = n()) %>%
arrange(weight %>% desc)
## making the graph
# type 0 = debaters, 1 = judges
g <- graph_from_data_frame(edgelist, directed = F)
# V(g)$type <- ifelse(V(g)$name %in% edgelist$judge == T, 1, 0) # this doesn't work for some reason
V(g)$type <- V(g)$name %in% edgelist$judge
plot(g, vertex.label.cex = 1, vertex.color = V(g)$type %>% as.factor, layout = layout.bipartite) # not pretty
cluster_algos <- function(g) {
walktrap = cluster_walktrap(g)
louvain = cluster_louvain(g)
infomap = cluster_infomap(g)
fast_greedy = cluster_fast_greedy(g)
V(g)$walktrap = walktrap$membership
V(g)$louvain = louvain$membership
V(g)$infomap = infomap$membership
V(g)$fast_greedy = fast_greedy$membership
return(g)
}
## Tidygraph
tidy_algos <- function(g) {
t <- g %>% as_tbl_graph()
cluster_nodes <- t %>%
activate(nodes) %>%
as_tibble() %>%
gather("cluster_algorithm", "community", -c(name, type))
return(cluster_nodes)
}
cluster_nodes %>%
group_by(cluster_algorithm, community) %>%
summarise(count = n())
cluster_nodes %>%
group_by(cluster_algorithm) %>%
summarise(count = n_distinct(community))
## not pretty graph that is too big
ggraph(t) +
geom_node_point(size = .01) +
geom_edge_link(width = .0001) +
g #+
# geom_node_text(aes(label = name), size = .001)
# looking at subgraph - only 3+ connections --------------------------------------------------------
el <- edgelist %>%
filter(weight > 2)
e <- graph_from_data_frame(el, directed = FALSE)
V(e)$type <- V(e)$name %in% edgelist$judge
pic <- ggraph(e, layout = 'bipartite') +
geom_node_point(aes(color = type), size = .5) +
geom_edge_link(aes(width = weight)) +
scale_edge_width(range = c(.1, 1))+ # control size
geom_node_text(aes(label = name), size = 1) +
theme_graph()
png('/Users/codycrunkilton/Desktop/g1.png', width = 100000, height = 100000)
pic
dev.off()
## cluster try2
e2 <- cluster_algos(e)
e3 <- tidy_algos(e2)
e3 %>%
group_by(cluster_algorithm, community) %>%
summarise(count = n())
e3 %>%
group_by(cluster_algorithm) %>%
summarise(count = n_distinct(community))
e3 %>%
arrange(cluster_algorithm, community) %>%
filter(type == TRUE) %>%
#filter(cluster_algorithm == "louvain") %>%
print(n = 300)
e3 %>%
arrange(cluster_algorithm %>% desc, community) %>%
group_by(cluster_algorithm, community) %>%
summarise(ct = n()) %>%
arrange(ct %>% desc)