[R]MLBにおける2001~2010年の得点差分と勝率の散布図(「Rによるセイバーメトリクス入門」(技術評論社)pp.95-99)
> library(tidyverse)
> library(Lahman)
> my_teams <- Teams %>% filter(yearID > 2000 & yearID < 2011) %>%
+ select(teamID, yearID, lgID, G, W, L, R, RA)
> my_teams %>% tail()
teamID yearID lgID G W L R RA
295 SFN 2010 NL 162 92 70 697 583
296 SLN 2010 NL 162 86 76 736 641
297 TBA 2010 AL 162 96 66 802 649
298 TEX 2010 AL 162 90 72 787 687
299 TOR 2010 AL 162 85 77 755 728
300 WAS 2010 NL 162 69 93 655 742
> my_teams <- my_teams %>% mutate(RD = R - RA, Wpct = W / (W + L))
> run_diff <- ggplot(my_teams, aes(x = RD, y = Wpct)) + geom_point() +
+ scale_x_continuous("Run differential") +
+ scale_y_continuous("Winning percentage")
> print(run_diff)
> crcblue <- "#2905A1"
> linfit <- lm(Wpct ~ RD, data = my_teams)
> print(linfit)
Call:
lm(formula = Wpct ~ RD, data = my_teams)
Coefficients:
(Intercept) RD
0.4999909 0.0006216
> run_diff + geom_smooth(method = "lm", se = FALSE, color = crcblue)
`geom_smooth()` using formula = 'y ~ x'
« [Python]一般逆行列を求める | トップページ | [Python]ファイルのハッシュ値を得る »
「R(本の計算を再現)」カテゴリの記事
- [R]ガンマ関数の値の計算(「入門 統計解析 -医学・自然科学編」(東京図書)pp.120-121)(2025.01.24)
- [R]グループに対するダミー変数(性別)(「44の例題で学ぶ計量経済学」(オーム社)pp.231-232)(2023.11.28)
- [R]重回帰モデルにおける仮説検定(「44の例題で学ぶ計量経済学」(オーム社)pp.205-206)(2023.11.27)
- [R]重回帰モデルにおけるt検定(「44の例題で学ぶ計量経済学」(オーム社)pp.185-188)(2023.11.26)
- [R]平均勤続年数と所定内賃金(「線形回帰分析」(朝倉書店)pp.116-118)(2023.11.06)


コメント