-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths2-datavolley-basics.Rmd
610 lines (404 loc) · 12 KB
/
s2-datavolley-basics.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
---
title: "Session 2: The R datavolley package"
author: "Ben Raymond, Adrien Ickowicz"
date: "<div id='logo_footer'></div>"
output:
xaringan::moon_reader:
lib_dir: libs
seal: false
self_contained: false
css: ["extra/extra.css"]
chakra: extra/remark-latest.min.js
nature:
highlightStyle: github
highlightLines: true
highlightSpans: true
mathjax: null
---
layout: true
<div class="my-footer">
<div class="my-footer-box"><a href="https://openvolley.org/"><img style="display:inline;" src="extra/ovoutline-w.png"/>openvolley.org</a></div>
<div class="my-footer-box"><a href="https://https://volleyball.ca/"><img src="extra/vc-w-wide.png"/></a></div>
<div class="my-footer-box"><a href="https://untan.gl/"><img src="extra/su_title-w.png"/></a></div>
</div>
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
options(knitr.kable.NA="")
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE, cache = FALSE, dpi = 120, comment = "")
library(dplyr)
library(knitr)
library(ggplot2)
library(datavolley)
options(tibble.width = 65, tibble.print_max = 10, tibble.print_min = 10, width = 80)
```
```{r xaringanExtra-clipboard, echo=FALSE}
xaringanExtra::use_clipboard()
#xaringanExtra::use_panelset()
```
class: inverse, logo, center
<img src="extra/3logo2.png" style="width:65%; margin-bottom:50px;" />
## Session 2: The R datavolley package
### Ben Raymond, Adrien Ickowicz
##### with valuable contributions from many others...
---
## What are we going to talk about
- reading datavolley files into R
- the structure of the data
- error-checking your scout files
- basic analyses
- general principles
- working with multiple matches
- exercises
---
## Session 2 setup
Update your copy of the workshop repo:
If you are in the 'R_Workshop_2022' project, then from the RStudio menu:
`Tools` -> `Version Control` -> `Pull Branches`
Or from the command line in your 'R_Workshop_2022' directory: `git pull`
<br />
Install the datavolley and ovlytics packages if you don't already have them:
```{r eval = FALSE}
install.packages(c("datavolley", "ovlytics"),
repos = c("https://openvolley.r-universe.dev",
"https://cloud.r-project.org"))
```
---
## dvw file structure
Open the `example_data/DE Women 2020/&20191020 SSC Palmber vs Allianz MTV.dvw` file in your text editor.
```{r echo = FALSE}
dvw <- readLines("example_data/DE Women 2020/&20191020 SSC Palmber vs Allianz MTV.dvw")
dvw <- stringi::stri_trans_general(dvw, "latin-ascii")
```
<div style="font-size:12px; height:45vh; overflow:auto; border:1px solid black;">
<tt>
`r paste(dvw, collapse = "<br />")`
</tt>
</div>
---
## dv_read
```{r eval = FALSE}
library(datavolley)
? dv_read
```
```{r eval = FALSE}
Usage
read_dv(
filename,
insert_technical_timeouts = TRUE,
do_warn = FALSE,
do_transliterate = FALSE,
encoding = "guess",
date_format = "guess",
extra_validation = 2,
validation_options = list(),
surname_case = "asis",
skill_evaluation_decode = "default",
custom_code_parser,
metadata_only = FALSE,
verbose = FALSE,
edited_meta
)
```
---
## dv_read
```{r}
x <- dv_read("example_data/DE Women 2020/&20191003 SSC Palmber vs Ladies in b.dvw")
```
Print a summary of the match:
```{r}
summary(x)
```
---
## dv_read
Parts:
```{r}
names(x)
```
---
## dv_read
```{r eval = FALSE}
str(x$file_meta)
```
```{r eval = FALSE}
## or in RStudio
View(x$file_meta)
```
```{r echo = FALSE}
str(x$file_meta)
```
---
## dv_read
```{r}
names(x$meta)
```
---
## dv_read
```{r eval = FALSE}
View(x$meta$match)
```
```{r echo = FALSE}
knitr::kable(x$meta$match[, names(x$meta$match)[!grepl("^X", names(x$meta$match))]], row.names = FALSE)
```
---
## dv_read — Date formats
```{r eval = FALSE}
Usage
read_dv(
filename,
insert_technical_timeouts = TRUE,
do_warn = FALSE,
do_transliterate = FALSE,
encoding = "guess",
`date_format = "guess"`,
extra_validation = 2,
validation_options = list(),
surname_case = "asis",
skill_evaluation_decode = "default",
custom_code_parser,
metadata_only = FALSE,
verbose = FALSE,
edited_meta
)
```
---
## dv_read — Date formats
Look at the file contents for the match date:
```{r}
x$raw[15:16]
```
<br />
Is that the 3rd of October or the 10th of March?
We can force a particular interpretation:
```
x <- dv_read(..., date_format = "mdy")
```
---
## dv_read
```{r eval = FALSE}
View(x$meta$teams)
```
```{r echo = FALSE}
knitr::kable(x$meta$teams[, names(x$meta$teams)[!grepl("^X", names(x$meta$teams))]], row.names = FALSE)
```
---
## dv_read
```{r eval = FALSE}
View(x$meta$players_h)
```
```{r echo = FALSE}
knitr::kable(x$meta$players_h[, names(x$meta$players_h)[!grepl("^X", names(x$meta$players_h))]], row.names = FALSE)
##str(x$meta$players_h)
```
---
## dv_read — Text encoding
```{r eval = FALSE}
Usage
read_dv(
filename,
insert_technical_timeouts = TRUE,
do_warn = FALSE,
do_transliterate = FALSE,
`encoding = "guess"`,
date_format = "guess",
extra_validation = 2,
validation_options = list(),
surname_case = "asis",
skill_evaluation_decode = "default",
custom_code_parser,
metadata_only = FALSE,
verbose = FALSE,
edited_meta
)
```
---
## dv_read — Text encoding
Normally `dv_read` should guess the correct text encoding used in the file:
```{r}
x$meta$players_h$name[c(1, 12)]
```
<br />
Use `verbose = TRUE` when reading:
```{r message = TRUE}
x <- dv_read("example_data/DE Women 2020/&20191003 SSC Palmber vs Ladies in b.dvw",
verbose = TRUE)
```
---
## dv_read — Text encoding
If we get the text encoding wrong:
```{r}
x2 <- dv_read("example_data/DE Women 2020/&20191003 SSC Palmber vs Ladies in b.dvw",
encoding = "iso-8859-8")
x2$meta$players_h$name[c(1, 12)]
```
---
## dv_read — Text encoding
TL/DR: if you have weird characters in player or team names, check the encoding used:
```{r eval = FALSE}
x <- dv_read("myfile.dvw", verbose = TRUE)
```
And force `dv_read` to use the correct encoding:
```{r eval = FALSE}
x <- dv_read("myfile.dvw", encoding = "whatever")
```
Optionally [submit a GitHub issue](https://github.com/openvolley/datavolley/issues) with your dvw file and we'll try and fix `dv_read` to accommodate it.
(You can also check
```{r eval = FALSE}
x$meta$match$text_encoding
```
and the output of `iconvlist()` for list of possible encodings.)
---
## dv_read — Skill evaluations
```{r eval = FALSE}
Usage
read_dv(
filename,
insert_technical_timeouts = TRUE,
do_warn = FALSE,
do_transliterate = FALSE,
encoding = "guess",
date_format = "guess",
extra_validation = 2,
validation_options = list(),
surname_case = "asis",
`skill_evaluation_decode = "default"`,
custom_code_parser,
metadata_only = FALSE,
verbose = FALSE,
edited_meta
)
```
---
## dv_read — Skill evaluations
What does a `B/` scout code mean?
--
- invasion (blocking fault)?
- attack kill off hands?
- poor block back to the opposition?
- something else?
<br />
- inconsistent between scouts, across files
- don't want to rely on this, particularly for multi-match analyses
---
## dv_read — Skill evaluations
```{r}
plays(x) %>% count(skill, evaluation_code, evaluation)
```
---
## dv_read — Skill evaluations
- translation from `evaluation_code` to `evaluation` is governed by the `skill_evaluation_decode` parameter to `dv_read`
- current options are
- `"default"` (per the DV manual)
- `"volleymetrics"`
- `"german"`
- or you can provide your own custom function
<br />
- look again at the breakdown in the previous slide, using the `"default"` conventions
---
## dv_read — Skill evaluations
```{r}
x <- dv_read("example_data/DE Women 2020/&20191003 SSC Palmber vs Ladies in b.dvw",
skill_evaluation_decode = "german")
plays(x) %>% count(skill, evaluation_code, evaluation)
```
---
## dv_read — File validation
```{r, eval = FALSE}
View(x$messages)
```
```{r echo = FALSE}
knitr::kable(x$messages[c(1, 2, 13, 66), ], row.names = FALSE)
```
---
## dv_read — File validation
See also: https://apps.untan.gl/dvalidate
~500 files / month, ~37k files total so far
<br />
<img src="extra/dval-hits.png" style="height:40vh;" />
---
## dv_read — Match result
```{r eval = FALSE}
View(x$meta$result)
```
```{r echo = FALSE}
knitr::kable(x$meta$result, row.names = FALSE)
```
---
## dv_read — The play-by-play data
```{r}
px <- plays(x)
```
- `names(plays(x))`
- `inspect(plays(x))`
---
## Play-by-play data — Identifiers
- `match_id` uniquely identifies the match
- `set_number` is the set number (1–5) within a match. If you want to uniquely identify a particular set (from amongst many matches) use the combination of `match_id` and `set_number`
- `point_id` identifies the rally number (point) within a match. Each rally (point) begins with a serve (or rotation fault). Timeouts and other non-action points might have their own `point_id`, so don't rely on `point_id` values being consecutive from one rally to the next. `point_id` values are only unique within a match (so e.g. two different matches will both have a point with `point_id` value of 1
---
## Play-by-play data — Identifiers
- `team_touch_id` identifies the touches that a team makes while the ball is on their side of the net. So a serve will have a certain `team_touch_id`, then the reception, set, and attack made by the receiving team will all have the same `team_touch_id` (different to the serve's `team_touch_id`). The following block, dig, set, and attack will have another `team_touch_id`, and so on. `team_touch_id` values are also only unique within a match
- the `phase` column identifies the phase of play: it can take the values `Serve`, `Reception`, or `Transition`. Note that a block made against a reception attack (the attack made by the receiving team immediately after receiving the serve) is considered to be `Reception` phase, but the dig made by the blocking team immediately after that (and every subsequent action in the rally) are `Transition`
---
## Play-by-play data — more variables
```{r eval = FALSE}
install.packages("ovlytics",
repos = c("https://openvolley.r-universe.dev",
"https://cloud.r-project.org"))
```
```{r eval = FALSE}
library(ovlytics)
px <- ov_augment_plays(px, to_add = "all")
```
Adds these columns:
```{r echo = FALSE}
library(ovlytics)
px <- plays(x)
old_names <- names(px)
px <- ov_augment_plays(px, to_add = "all")
setdiff(names(px), old_names)
```
---
## Analyses — General principles
Translating data into information
- what might we want to know about a particular skill/facet of the game?
- what data do we therefore need to collect?
- who is the intended audience?
- what analysis techniques might we use, what statistics/summaries might we generate?
---
## Analyses — General principles
Basis of reporting, e.g.
- attack kills per set?
- attack kills per 50 rallies played?
- kills per 100 attempts (attack kill percentage)?
---
## Analyses — General principles
- identifying teams
- `team` (name) and `team_id`
- (also `serving_team`, `home_team`, `home_team_id`, `visiting_team`, `visiting_team_id`, etc)
- identifying players
- `player_number`
- `player_id`
- `player_name`
- (also `home_p1`, `home_player_id1`, etc)
---
## Analyses — General principles
Skill outcomes: use `evaluation` rather than `evaluation_code` where possible
For example, find block faults by
```{r eval = FALSE}
px %>% dplyr::filter(skill == "Block" & evaluation == "Invasion")
```
rather than by
```{r eval = FALSE}
px %>% dplyr::filter(skill == "Block" & evaluation_code == "/")
```
---
## Working with multiple matches
- consistent team IDs, names, player IDs, names, numbers
- find name or ID variants using e.g. `count` or `distinct`
- renaming teams and players
- edit the dvw files directly, then reload into R
- or use `remap_team_names` and `remap_player_names` in R
---
## R — Exercises
Open `s2-exercises.Rmd` in Rstudio