/
MLDM_20150105.html
568 lines (486 loc) · 16.4 KB
/
MLDM_20150105.html
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
<!DOCTYPE html>
<html>
<head>
<title>Play Data, Play Ball!</title>
<meta charset="utf-8">
<meta name="description" content="Play Data, Play Ball!">
<meta name="author" content="Summit Suen">
<meta name="generator" content="slidify" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<link rel="stylesheet" href="libraries/frameworks/io2012/css/default.css" media="all" >
<link rel="stylesheet" href="libraries/frameworks/io2012/css/phone.css"
media="only screen and (max-device-width: 480px)" >
<link rel="stylesheet" href="libraries/frameworks/io2012/css/slidify.css" >
<link rel="stylesheet" href="libraries/highlighters/highlight.js/css/tomorrow.css" />
<base target="_blank"> <!-- This amazingness opens all links in a new tab. --> <link rel=stylesheet href="./assets/css/ribbons.css"></link>
<!-- Grab CDN jQuery, fall back to local if offline -->
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.min.js"></script>
<script>window.jQuery || document.write('<script src="libraries/widgets/quiz/js/jquery.js"><\/script>')</script>
<script data-main="libraries/frameworks/io2012/js/slides"
src="libraries/frameworks/io2012/js/require-1.0.8.min.js">
</script>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<!-- LOGO SLIDE -->
<slide class="title-slide segue nobackground">
<hgroup class="auto-fadein">
<h1>Play Data, Play Ball!</h1>
<h2>Exploring Baseball Data with R</h2>
<p>Summit Suen<br/>Taiwan R User Group</p>
</hgroup>
<article></article>
</slide>
<!-- SLIDES -->
<slide class="" id="slide-1" style="background:;">
<article data-timings="">
<!-- Limit image width and height -->
<style type='text/css'>
img {
max-height: 560px;
max-width: 964px;
}
</style>
<h2>警告:今日技術成分偏低</h2>
<p><img src="https://1.bp.blogspot.com/_-o9_R5uz3oI/TPjcHJtDplI/AAAAAAAAAAQ/TUrQfHUytts/s1600/proper+warning+screen.bmp" alt=""></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-2" style="background:;">
<hgroup>
<h2>說到運動資料的分析,你會想到...</h2>
</hgroup>
<article data-timings="">
<p><img src="https://www.ocf.berkeley.edu/%7Esuperb/pics/moneyball.jpg" alt=""></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-3" style="background:;">
<hgroup>
<h2>我也是看報紙才知道(誤</h2>
</hgroup>
<article data-timings="">
<p><img src="assets/img/wc2014_01.png" alt=""></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-4" style="background:;">
<hgroup>
<h2>我也是看報紙才知道(誤</h2>
</hgroup>
<article data-timings="">
<p><img src="assets/img/wc2014_baidu.jpg" alt=""></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-5" style="background:;">
<hgroup>
<h2>Sabermatrics:賽伯計量學</h2>
</hgroup>
<article data-timings="">
<p><img src="https://bostonu.qualtrics.com/CP/Graphic.php?IM=IM_2h31EGcJG5TEfPf" alt=""></p>
<p><img src="assets/img/sabr_history.png" alt=""></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-6" style="background:;">
<hgroup>
<h2>前人寫扣,後人乘涼</h2>
</hgroup>
<article data-timings="">
<ul>
<li><a href="http://lahman.r-forge.r-project.org/">Lahman Database</a></li>
</ul>
<pre><code class="r">library(Lahman)
library(dplyr)
totalRS <- Teams %>% select(yearID, R, G) %>% mutate(AvgRperG = R/G) %>% group_by(yearID) %>% summarise(sum(AvgRperG))
names(totalRS) <- c("yearID", "RUN")
head(totalRS)
</code></pre>
<pre><code>## Source: local data frame [6 x 2]
##
## yearID RUN
## 1 1871 93.12897
## 2 1872 95.21474
## 3 1873 73.15998
## 4 1874 58.55903
## 5 1875 70.08774
## 6 1876 47.01267
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-7" style="background:;">
<hgroup>
<h2>前人寫扣,後人乘涼</h2>
</hgroup>
<article data-timings="">
<ul>
<li><a href="http://lahman.r-forge.r-project.org/">Lahman Database</a></li>
</ul>
<pre><code class="r">library(ggplot2)
ggplot(data = totalRS, aes(x = yearID, y = RUN)) + stat_smooth() + geom_line()
</code></pre>
<pre><code>## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
</code></pre>
<p><img src="assets/fig/unnamed-chunk-2-1.png" alt="plot of chunk unnamed-chunk-2"> </p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-8" style="background:;">
<hgroup>
<h2>前人寫扣,後人乘涼</h2>
</hgroup>
<article data-timings="">
<ul>
<li><a href="http://lahman.r-forge.r-project.org/">Lahman Database</a></li>
</ul>
<pre><code class="r">library(Lahman)
library(dplyr)
head(filter(Batting, playerID == "jeterde01"))
</code></pre>
<pre><code>## playerID yearID stint teamID lgID G G_batting AB R H X2B X3B HR
## 1 jeterde01 1995 1 NYA AL 15 15 48 5 12 4 1 0
## 2 jeterde01 1996 1 NYA AL 157 157 582 104 183 25 6 10
## 3 jeterde01 1997 1 NYA AL 159 159 654 116 190 31 7 10
## 4 jeterde01 1998 1 NYA AL 149 149 626 127 203 25 8 19
## 5 jeterde01 1999 1 NYA AL 158 158 627 134 219 37 9 24
## 6 jeterde01 2000 1 NYA AL 148 148 593 119 201 31 4 15
## RBI SB CS BB SO IBB HBP SH SF GIDP G_old
## 1 7 0 0 3 11 0 0 0 0 0 15
## 2 78 14 7 48 102 1 9 6 9 13 157
## 3 70 23 12 74 125 0 10 8 2 14 159
## 4 84 30 6 57 119 1 5 3 3 13 149
## 5 102 19 8 91 116 5 12 3 6 12 158
## 6 73 22 4 68 99 4 12 3 3 14 148
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-9" style="background:;">
<hgroup>
<h2>前人寫扣,後人繼續乘涼</h2>
</hgroup>
<article data-timings="">
<ul>
<li><a href="http://cpsievert.github.io/pitchRx/#2D_animation">pitchRx</a></li>
</ul>
<pre><code class="r">install.packages("pitchRx")
library(pitchRx)
</code></pre>
<p><img src="assets/img/pitchRx.png" alt=""></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-10" style="background:;">
<hgroup>
<h2>前人寫扣,後人繼續乘涼</h2>
</hgroup>
<article data-timings="">
<ul>
<li><a href="https://baseballwithr.wordpress.com/2014/03/17/introduction-to-openwar/">openWAR</a></li>
</ul>
<pre><code class="r">install.packages("Sxslt", repos = "http://www.omegahat.org/R", type = "source")
require(devtools)
install_github("openWAR", "beanumber")
</code></pre>
<p><img src="assets/img/OpenWAR.png" alt=""></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-11" style="background:red;">
<hgroup>
<h2>前人寫扣,後人不能一直乘涼!</h2>
</hgroup>
<article data-timings="">
<p><br><del>自己的國家自己救</del></br></p>
<p><br>自己的 crawler/analyzer 自己寫!</br></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-12" style="background:;">
<hgroup>
<h2>巧婦難為無米之炊:資料哪裡來?</h2>
</hgroup>
<article data-timings="">
<p><img src="assets/img/cpbl_web_snapshot.jpg" alt=""></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-13" style="background:;">
<hgroup>
<h2>第一次用R寫爬蟲就上手</h2>
</hgroup>
<article data-timings="">
<ul>
<li><a href="http://ropensci.github.io/RSelenium/">RSelenium</a></li>
<li><a href="http://phantomjs.org/">phantomJS</a></li>
</ul>
<pre><code class="r">## RSelenium + phantomJS
library(XML)
library(RSelenium)
pJS <- phantom()
Sys.sleep(5)
remDr <- remoteDriver(browserName = 'phantomjs')
remDr$open()
url <- 'http://www.cpbl.com.tw/stats_hr.aspx'
remDr$navigate(url)
hr_tables <- list()
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-14" style="background:;">
<hgroup>
<h2>第一次用R寫爬蟲就上手</h2>
</hgroup>
<article data-timings="">
<ul>
<li><a href="http://ropensci.github.io/RSelenium/">RSelenium</a></li>
<li><a href="http://phantomjs.org/">phantomJS</a></li>
</ul>
<pre><code class="r">year <- seq(1990, 2014)
for (i in 1:length(year)) {
option <- remDr$findElement(using = 'xpath', paste0("//*/option[@value = '",year[i],"']"))
option$clickElement()
src <- option$getPageSource()
hr_tables[i] <- readHTMLTable(src[[1]], stringAsFactors = FALSE, encoding = "utf8")
}
# View(hr_tables)
remDr$close()
pJS$stop()
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-15" style="background:;">
<hgroup>
<h2>資料在手,跟我走!</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">load("hr_tables.RData")
head(hr_tables[[25]])
</code></pre>
<pre><code>## NUMBER YEAR GID DATE STADIUM BATTER BATTERTEAM PITCHER
## 1 # YEAR GAME NO. DATE STADIUM PLAYER PLAYER TEAM PITCHER
## 2 1 2014 2 2014/3/23 天母 林 威 助 中信兄弟 費 古 洛
## 3 2 2014 3 2014/3/23 澄清湖 詹 智 堯 Lamigo 林 正 豐
## 4 3 2014 4 2014/3/25 新莊 張 志 豪 中信兄弟 林 晨 樺
## 5 4 2014 5 2014/3/26 嘉義市 林 泓 育 Lamigo 林 岳 平
## 6 5 2014 6 2014/3/27 新莊 張 志 豪 中信兄弟 黃 勝 雄
## PITCHERTEAM RBI REMARK
## 1 PITCHER TEAM RBI REMARK
## 2 統一7-ELEVEn 1
## 3 義大 1
## 4 義大 1
## 5 統一7-ELEVEn 1
## 6 義大 1
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-16" style="background:;">
<hgroup>
<h2>用R畫圖跟你想的不一樣</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">par(family = 'Heiti TC Light')
m <- ggplot(hr_table, aes(x = as.numeric(GID)))
m + geom_density(aes(fill = factor(BatterTeam), alpha = 0.01)) +
labs(title = paste0("HR Distribution of ", year[i]," Season 中華職棒", year[i], "賽季逐場全壘打分布")) +
theme(text = element_text(family="Heiti TC Light"), plot.title = element_text(size = 26), legend.text = element_text(size = 20))
</code></pre>
<pre><code>## Error in paste0("HR Distribution of ", year[i], " Season 中華職棒", : object 'year' not found
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-17" style="background:;">
<hgroup>
<h2>資料在手,繼續走!</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">load("sc_tables.RData")
# 賽伯計量學的畢達哥拉斯定理
win_pc <- function(RS, RA) {
perc <- RS^2 / (RS^2 + RA^2)
return(perc)
}
</code></pre>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/1/18/Pythagorean_proof.svg" alt=""></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-18" style="background:;">
<hgroup>
<h2>用R畫圖跟你想的不一樣趴兔</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">levels(win_prob$team)
</code></pre>
<pre><code>## [1] "統一7-ELEVEn" "義大" "中信兄弟" "Lamigo"
</code></pre>
<pre><code class="r"># fig <- ggplot(data = win_prob, aes(x = team, y = real, fill = factor(half)))
# fig + geom_bar(stat = "identity", position="dodge")
ggplot(data = win_prob, aes(x = expd, y = real, color = factor(team))) + stat_smooth() + geom_point() + coord_fixed()
</code></pre>
<pre><code>## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
</code></pre>
<p><img src="assets/fig/unnamed-chunk-13-1.png" alt="plot of chunk unnamed-chunk-13"> </p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-19" style="background:;">
<hgroup>
<h2>(並沒有)令十三億人都震驚的事實</h2>
</hgroup>
<article data-timings="">
<p><img src="http://i1294.photobucket.com/albums/b618/fhoneck/Value_Howard_Trout_zps64c30bff.png" alt=""> <a href="http://www.fangraphs.com/community/2014s-most-underpaid-and-overpaid-hitters/">example</a></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-20" style="background:;">
<article data-timings="">
<p><img src="http://sd.keepcalm-o-matic.co.uk/i/keep-calm-and-play-ball-50.png" alt=""></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="backdrop"></slide>
</slides>
<div class="pagination pagination-small" id='io2012-ptoc' style="display:none;">
<ul>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=1 title=''>
1
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=2 title='說到運動資料的分析,你會想到...'>
2
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=3 title='我也是看報紙才知道(誤'>
3
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=4 title='我也是看報紙才知道(誤'>
4
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=5 title='Sabermatrics:賽伯計量學'>
5
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=6 title='前人寫扣,後人乘涼'>
6
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=7 title='前人寫扣,後人乘涼'>
7
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=8 title='前人寫扣,後人乘涼'>
8
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=9 title='前人寫扣,後人繼續乘涼'>
9
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=10 title='前人寫扣,後人繼續乘涼'>
10
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=11 title='前人寫扣,後人不能一直乘涼!'>
11
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=12 title='巧婦難為無米之炊:資料哪裡來?'>
12
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=13 title='第一次用R寫爬蟲就上手'>
13
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=14 title='第一次用R寫爬蟲就上手'>
14
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=15 title='資料在手,跟我走!'>
15
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=16 title='用R畫圖跟你想的不一樣'>
16
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=17 title='資料在手,繼續走!'>
17
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=18 title='用R畫圖跟你想的不一樣趴兔'>
18
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=19 title='(並沒有)令十三億人都震驚的事實'>
19
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=20 title=''>
20
</a>
</li>
</ul>
</div> <!--[if IE]>
<script
src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js">
</script>
<script>CFInstall.check({mode: 'overlay'});</script>
<![endif]-->
</body>
<!-- Load Javascripts for Widgets -->
<!-- LOAD HIGHLIGHTER JS FILES -->
<script src="libraries/highlighters/highlight.js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<!-- DONE LOADING HIGHLIGHTER JS FILES -->
</html>