forked from garysutton/statisticsplaybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CH19.R
188 lines (148 loc) · 4.76 KB
/
CH19.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
library(tidyverse)
dat1 <- read_csv("/Users/garysutton/Library/Mobile Documents/com~apple~CloudDocs/seasons_stats.csv")
dim(dat1)
dat1 %>%
select(Year, Player, Pos, Tm, G, PTS) -> dat1
dat1 %>%
filter(Year <= 1999) -> dat1
glimpse(dat1)
dat1$Year <- as.factor(dat1$Year)
dat1$Pos <- as.factor(dat1$Pos)
dat1$Tm <- as.factor(dat1$Tm)
dat1 %>%
distinct(Year, Player, Pos) -> test1
dim(test1)
dat1 %>%
filter(Tm == "TOT") %>%
tally()
dat1 %>%
filter(Year == 1950, Player == "Ed Bartels") -> test2
print(test2)
dat1 %>%
filter(Tm != "TOT") -> dat1
dim(dat1)
dat1 %>%
group_by(Year, Player, Pos) %>%
summarize(G = sum(G), PTS = sum(PTS)) -> dat2
dim(dat2)
dat2 %>%
filter(Year == 1950, Player == "Ed Bartels") -> test3
print(test3)
dat1 %>%
group_by(Year) %>%
filter(Player == "George Mikan*") -> test4
colorDF::highlight(test4, test4$Year == 1950)
dat2 %>%
group_by(Year) %>%
filter(Player == "George Mikan*") -> test5
colorDF::highlight(test5, test5$Year == 1950)
arsenal::comparedf(test4, test5)
dat2 %>%
mutate(PPG = format(PTS/G, digits = 1, nsmall = 1)) -> dat2
head(dat2)
dat2 %>%
filter(PPG >= 2 & G >= 20) -> dat2
dim(dat2)
dat2 %>%
select(Year, Player, PPG) -> dat2
dat2$PPG <- as.numeric(dat2$PPG)
dat2$Year_Player <- paste0(as.character(dat2$Year)," ",
as.character(dat2$Player))
head(dat2, n = 3)
tail(dat2, n = 3)
dat2 %>%
group_by(Year) %>%
slice(which.max(PPG)) -> dat3
p1 <- ggplot(dat3, aes(x = Year_Player, y = PPG)) +
geom_bar(stat = "identity", color = "dodgerblue", fill = "dodgerblue") +
geom_text(aes(label = PPG),
position = position_dodge(width = 0.8), vjust = -0.3,
fontface = "bold", size = 2) +
labs(title = "Leading Scorer (PPG) by Year",
subtitle = "1950 to 1999",
caption = "* Hall of Fame member",
x = "Year and Leading Scorer",
y = "Points per Game") +
theme(plot.title = element_text(face = "bold")) +
theme(axis.text.x = element_text(angle = 90))
print(p1)
dat2 %>%
group_by(Year) %>%
mutate(z_ppg = round((PPG - mean(PPG)) / sd(PPG), digits = 1)) -> dat4a
head(dat4a, n = 3)
tail(dat4a, n = 3)
mean(dat4a$z_ppg)
var(dat4a$z_ppg)
dat4a %>%
group_by(Year) %>%
slice(which.max(z_ppg)) -> dat4b
p2 <- ggplot(dat4b, aes(x = Year_Player, y = z_ppg)) +
geom_bar(stat = "identity", color = "darkorange", fill = "darkorange") +
geom_text(aes(label = z_ppg),
position = position_dodge(width = 0.8), vjust = -0.3,
fontface = "bold", size = 2) +
labs(title = "Leading Scorer (Z-Score) by Year",
subtitle = "1950 to 1999",
caption = "* Hall of Fame member",
x = "Year and Leading Scorer",
y = "Z-Score (standard deviations from mean)") +
theme(plot.title = element_text(face = "bold")) +
theme(axis.text.x = element_text(angle = 90))
print(p2)
dat2 %>%
group_by(Year) %>%
mutate(sd_ppg = round((PPG / sd(PPG)), digits = 1)) -> dat5a
head(dat5a, n = 3)
tail(dat5a, n = 3)
var(dat5a$sd_ppg)
dat5a %>%
group_by(Year) %>%
slice(which.max(sd_ppg)) -> dat5b
p3 <- ggplot(dat5b, aes(x = Year_Player, y = sd_ppg)) +
geom_bar(stat = "identity", color = "salmon3", fill = "salmon3") +
geom_text(aes(label = sd_ppg),
position = position_dodge(width = 0.8), vjust = -0.3,
fontface = "bold", size = 2) +
labs(title = "Leading Scorer (Standard Deviation Method) by Year",
subtitle = "1950 to 1999", caption = "* Hall of Fame member",
x = "Year and Leading Scorer",
y = "PPG / Standard Deviation") +
theme(plot.title = element_text(face = "bold")) +
theme(axis.text.x = element_text(angle = 90))
print(p3)
dat2 %>%
group_by(Year) %>%
summarize(mean = round(mean(PPG), digits = 1)) -> dat6a
head(dat6a, n = 3)
tail(dat6a, n = 3)
left_join(dat3, dat6a, by = "Year") -> dat6b
head(dat6b, n = 3)
tail(dat6b, n = 3)
dat6b %>%
mutate(c_ppg = PPG - mean) -> dat6b
head(dat6b, n = 3)
tail(dat6b, n = 3)
p4 <- ggplot(dat6b, aes(x = Year_Player, y = c_ppg)) +
geom_bar(stat = "identity", color = "aquamarine4",
fill = "aquamarine4") +
geom_text(aes(label = c_ppg),
position = position_dodge(width = 0.8), vjust = -0.3,
fontface = "bold", size = 2) +
labs(title = "Leading Scorer (Centering Method) by Year",
subtitle = "1950 to 2017",
caption = "* Hall of Fame member",
x = "Year and Leading Scorer",
y = "PPG - Annual Mean") +
theme(plot.title = element_text(face = "bold")) +
theme(axis.text.x = element_text(angle = 90))
print(p4)
dat2 %>%
group_by(Year) %>%
mutate(r_ppg = round((PPG) / (max(PPG) - min(PPG)), digits = 1)) -> dat7a
head(dat7a, n = 3)
tail(dat7a, n = 3)
dat7a %>%
group_by(Year) %>%
slice(which.max(r_ppg)) -> dat7b
head(dat7b, n = 10)
tail(dat7b, n = 10)