-
Notifications
You must be signed in to change notification settings - Fork 2
/
4_modeling.R
287 lines (220 loc) · 9.19 KB
/
4_modeling.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
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
library(dplyr)
library(tidyr)
library(dbplyr)
library(rlang)
library(glue)
library(ggplot2)
library(readr)
options(scipen=999)
final_table <- read_csv('prepared_data.csv.gz')
final_table$player_1_wins <- factor(final_table$player_1_wins)
# =========
# Modelling
# =========
# We're now ready to model:
# - player_1_wins is our target
# - match_id and current_time are ignored as features
# - all other columns are diff columns acting as features
library(caret)
# We'll work on a sample to speed the modelling up a bit
# Note that, by nature of this prediction task, we want to have a balanced target outcome
sample <- final_table %>% group_by(player_1_wins) %>% sample_n(size = 30000) %>% as.data.frame
sample$player_1_wins %>% table
partition <- createDataPartition(sample$player_1_wins)
sample.train <- sample[partition$Resample1,]
sample.test <- sample[-partition$Resample1,]
sample.train$player_1_wins %>% table
sample.test$player_1_wins %>% table
# There is another trick we can apply to make the modelling more robust
# Note that every instance can represent two instances by flipping the sign of all diff stats
# And flipping the target outcome
mirror_data <- function(ds, prefix='diff_', target='player_1_wins') {
ds.m <- ds %>%
mutate_at(vars(starts_with(prefix)), (function(x) -x)) %>%
mutate_at(vars(target), (function(x) factor(abs( as.numeric(levels(x))[x] - 1))))
return(bind_rows(ds, ds.m))
}
# Only apply this on the train set
sample.train <- mirror_data(sample.train)
sample.train$player_1_wins %>% table
sample.test$player_1_wins %>% table
# Small function to report results
report_results <- function(data, predfunc, nt=200) {
data$predictions <- predfunc(data)
print(confusionMatrix(data$predictions, data$player_1_wins))
plot <- data %>%
mutate(gr_time=ntile(.$current_time, nt)) %>%
mutate(gr_correct=as.numeric(predictions == player_1_wins)) %>%
group_by(gr_time) %>%
summarise(gr_acc=sum(gr_correct)/n()) %>%
ggplot(aes(x=gr_time,y=gr_acc)) + geom_line() + stat_smooth()
data$predictions <- NULL
return(plot)
}
# First try with logistic regression
model.glm <- glm(player_1_wins ~ ., family=binomial(link='logit'),
data=sample.train %>% select(-match_id, -current_time))
report_results(sample.train, function(x) {
(predict(model.glm, type='response', newdata=x) >= 0.5) %>%
as.numeric %>% factor
})
report_results(sample.test, function(x) {
(predict(model.glm, type='response', newdata=x) >= 0.5) %>%
as.numeric %>% factor
})
# About 71% accuracy on train and test
# Let's see if random forest does better
library(randomForest)
model.rf <- randomForest(player_1_wins ~ .,
data=sample.train %>% select(-match_id, -current_time),
ntree=300, importance=T)
report_results(sample.train, function(x) {
(predict(model.rf, type='response', newdata=x)) %>%
factor
})
report_results(sample.test, function(x) {
(predict(model.rf, type='response', newdata=x)) %>%
factor
})
report_results(final_table, function(x) {
(predict(model.rf, type='response', newdata=x)) %>%
factor
})
# 83% accuracy on test: let's go with this model (same accuracy on complete set)
# ==========
# Inspection
# ==========
imp <- importance(model.rf, type=1, scale = F)
ggplot(data.frame(imp=imp[,1], var=names(imp[,1])) %>% filter(imp>=0.01), aes(x=var, y=imp)) +
geom_bar(stat='identity') +
coord_flip()
partialPlot(model.rf, sample.train %>% select(-match_id, -current_time, -player_1_wins),
x.var='diff_score_total')
partialPlot(model.rf, sample.train %>% select(-match_id, -current_time, -player_1_wins),
x.var='diff_population_total')
# Try some test matches
library(zoo)
show_match <- function(data, match_id, predfunc) {
# Average: 20 ticks per minute
match <- data %>% filter(match_id==!!match_id) %>%
arrange(current_time) %>%
mutate(prediction=predfunc(.)) %>%
mutate(correct=ifelse((prediction>=0.5)==(player_1_wins=='1'), 'correct', 'incorrect')) %>%
mutate(predmavg=rollapply(prediction,40,mean,align='right',fill=0.5))
clr <- ifelse('0'==match$player_1_wins[1], 'red', 'blue')
match %>%
ggplot(aes(x=current_time/60000, y=prediction, color=correct)) +
geom_line(aes(group=1), size=1) +
geom_line(aes(x=current_time/60000, y=predmavg), color='purple', size=1) +
geom_line(aes(x=current_time/60000, y=((diff_score_total/max(abs(diff_score_total)))+1)/2), color='orange') +
scale_x_continuous(name="Time") +
scale_y_continuous(name="Probability", sec.axis=sec_axis(~.*2-1, name = "Score Balance")) +
geom_hline(yintercept = 0.5) +
theme_minimal()
}
show_match(final_table, 16720682, function(x) {
predict(model.rf, type='prob', newdata=x)[,'1']
})
show_match(final_table, 16722679, function(x) {
predict(model.rf, type='prob', newdata=x)[,'1']
})
db.matches %>% filter(match_id == 16722679) %>% collect %>% select(match_winner, contains("name"))
extra_info %>% filter(match_id == 16722679)
diff_table %>% filter(match_id == 16722679) %>% select(contains("name"))
# Get an idea of accuracy on the last state
last_outcome <- function(data, predfunc) {
data$predictions <- predfunc(data)
data <- data %>% group_by(match_id) %>% filter(current_time == max(current_time))
print(confusionMatrix(data$predictions, data$player_1_wins))
data <- data %>% filter(predictions != player_1_wins)
return(data)
}
inc <- last_outcome(final_table, function(x) {
predict(model.rf, type='response', newdata=x) %>%
factor
})
# 94% accuracy
inc %>% head
# This seems completely weird. After investigation, seems like the wrong person resigned
show_match(final_table, 16721231, function(x) {
predict(model.rf, type='prob', newdata=x)[,'1']
})
db.matches %>% filter(match_id == 16721231) %>% collect %>% select(match_winner, contains("name"))
extra_info %>% filter(match_id == 16721231)
diff_table %>% filter(match_id == 16721231) %>% select(contains("name"))
# Of course, everyone can predict the outcome when looking at the score, rating, vil count and total pop
# Let's see how much worse such a model would be
model.rf.simple <- randomForest(player_1_wins ~ .,
data=sample.train %>% select(diff_score_total, diff_rating,
diff_population_total, diff_population_civilian,
player_1_wins),
ntree=300, importance=T)
report_results(sample.train, function(x) {
(predict(model.rf.simple, type='response', newdata=x)) %>%
factor
})
report_results(sample.test, function(x) {
(predict(model.rf.simple, type='response', newdata=x)) %>%
factor
})
report_results(final_table, function(x) {
(predict(model.rf.simple, type='response', newdata=x)) %>%
factor
})
# 79% accuracy, so we are doing a bit better
inc.simple <- last_outcome(final_table, function(x) {
predict(model.rf.simple, type='response', newdata=x) %>%
factor
})
# And 91% accuracy on the final step
# What we would like to know, however, is how fast we are in terms of predicting the outcome
# For instance as a percentage of the total match running time
# We do this both for the predictions as is and a smoothed variant
better_rate <- function(data, predfunc) {
data$predictions <- predfunc(data)
data <- data %>% group_by(match_id) %>%
mutate(outcome = predictions >= 0.5) %>%
mutate(correct = outcome == (player_1_wins == '1')) %>%
mutate(correct_before = correct & lag(correct)) %>%
mutate(ratio = current_time/max(current_time)) %>%
select(match_id, current_time, predictions, correct, correct_before, ratio) %>%
filter(correct, !correct_before) %>%
arrange(desc(ratio)) %>% slice(1)
return(data)
}
show_match(final_table, 16720682, function(x) {
predict(model.rf, type='prob', newdata=x)[,'1']
})
show_match(final_table, 17582436, function(x) {
predict(model.rf, type='prob', newdata=x)[,'1']
})
predrate.normal <- better_rate(final_table, function(x) {
predict(model.rf, type='prob', newdata=x)[,'1']
}) %>% pull(ratio)
predrate.normalsmooth <- better_rate(final_table, function(x) {
predict(model.rf, type='prob', newdata=x)[,'1'] %>%
rollapply(40, mean, align='right', fill=0.5)
}) %>% pull(ratio)
predrate.simple <- better_rate(final_table, function(x) {
predict(model.rf.simple, type='prob', newdata=x)[,'1']
}) %>% pull(ratio)
predrate.simplesmooth <- better_rate(final_table, function(x) {
predict(model.rf.simple, type='prob', newdata=x)[,'1'] %>%
rollapply(40, mean, align='right', fill=0.5)
}) %>% pull(ratio)
predrate <- data.frame(type='normal', val=predrate.normal) %>%
bind_rows(data.frame(type='snormal', val=predrate.normalsmooth)) %>%
bind_rows(data.frame(type='simple', val=predrate.simple)) %>%
bind_rows(data.frame(type='ssimple', val=predrate.simplesmooth))
ggplot(predrate, aes(x=val)) +
geom_density(aes(fill=type), alpha=0.7) + theme_minimal()
# ==========
# Deployment
# ==========
# Save model
save(model.rf, file="model.rda")
# Ideas for expansion:
# - Age differences and time difference in terms reached
# - Incorporate tick time as feature: there might be interactions
# - Incorporate civs as features
# - Incorporate trend information: might accomodate for smoothing effects