-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsourcecode.R
More file actions
270 lines (224 loc) · 9.11 KB
/
Copy pathsourcecode.R
File metadata and controls
270 lines (224 loc) · 9.11 KB
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
library(dplyr)
library(ggplot2)
library(summarytools)
library(MASS)
library(fitdistrplus)
library(patchwork)
library(viridis)
df <- read.csv("deliveries.csv", stringsAsFactors = FALSE)
death_overs <- df %>%
filter(!is.na(batsman),
!is.na(batsman_runs),
!is.na(over),
over >= 16, over <= 20)
x <- death_overs$batsman_runs
mean_x <- mean(x, na.rm = TRUE)
var_x <- var(x, na.rm = TRUE)
sd_x <- sd(x, na.rm = TRUE)
skewness_x <- sum((x - mean_x)^3, na.rm = TRUE) / length(x) / (sd_x^3)
descriptive_stats <- data.frame(
Statistic = c("Mean", "Variance", "SD", "Skewness"),
Value = c(mean_x, var_x, sd_x, skewness_x)
)
print(descriptive_stats)
cat("\n=== Checking for Overdispersion ===\n")
cat("Mean:", round(mean_x, 2), "\n")
cat("Variance:", round(var_x, 2), "\n")
cat("Variance/Mean ratio:", round(var_x/mean_x, 2), "\n")
cat("If ratio >> 1, Negative Binomial is appropriate\n\n")
Q1 <- quantile(x, 0.25, na.rm = TRUE)
Q3 <- quantile(x, 0.75, na.rm = TRUE)
IQR_x <- Q3 - Q1
lower_bound <- Q1 - 1.5 * IQR_x
upper_bound <- Q3 + 1.5 * IQR_x
x_clean <- x[x >= lower_bound & x <= upper_bound]
cat("Outliers removed:", length(x) - length(x_clean), "out of", length(x), "\n")
cat("Clean data size:", length(x_clean), "\n\n")
p_raw <- ggplot(data.frame(x = x_clean), aes(x = x)) +
geom_histogram(aes(y = after_stat(density)),
bins = 20,
fill = "skyblue",
color = "black",
alpha = 0.8) +
geom_density(color = "red", linewidth = 1) +
geom_vline(xintercept = mean(x_clean), color = "blue", linetype = "dashed", linewidth = 1) +
annotate("text",
x = mean(x_clean),
y = Inf,
label = paste0("Mean = ", round(mean(x_clean), 2)),
vjust = 2,
color = "blue",
fontface = "bold") +
annotate("text",
x = min(x_clean),
y = Inf,
label = paste0("Var = ", round(var(x_clean), 2),
"\nSD = ", round(sd(x_clean), 2),
"\nSkew = ", round(skewness_x, 2)),
hjust = 0,
vjust = 2,
color = "darkgreen",
fontface = "bold") +
labs(title = "Distribution of Batsman Runs in Death Overs",
subtitle = "Empirical histogram with density curve (outliers removed)",
x = "batsman_runs", y = "Density") +
theme_minimal()
print(p_raw)
ggsave("raw_distribution.png", p_raw, width = 10, height = 6, dpi = 300)
cat("=== Fitting Negative Binomial Distribution ===\n")
fit_nbinom <- fitdist(x_clean, "nbinom")
print(fit_nbinom)
size_param <- fit_nbinom$estimate["size"]
prob_param <- fit_nbinom$estimate["prob"]
cat("\nNegative Binomial Parameters:\n")
cat("size (dispersion):", round(size_param, 4), "\n")
cat("prob (success probability):", round(prob_param, 4), "\n")
cat("\n=== Goodness-of-Fit Tests ===\n")
print(summary(fit_nbinom))
p_nbinom_dense <- denscomp(fit_nbinom,
legendtext = "Negative Binomial",
main = "Negative Binomial Fit for Death Overs Runs") +
theme_minimal()
p_nbinom_qq <- qqcomp(fit_nbinom,
legendtext = "Negative Binomial",
main = "QQ Plot - Negative Binomial Fit") +
theme_minimal()
p_nbinom_pp <- ppcomp(fit_nbinom,
legendtext = "Negative Binomial",
main = "PP Plot - Negative Binomial Fit") +
theme_minimal()
print(p_nbinom_dense)
ggsave("nbinom_density_fit.png", p_nbinom_dense, width = 8, height = 6, dpi = 300)
print(p_nbinom_qq)
ggsave("nbinom_qq_plot.png", p_nbinom_qq, width = 8, height = 6, dpi = 300)
print(p_nbinom_pp)
ggsave("nbinom_pp_plot.png", p_nbinom_pp, width = 8, height = 6, dpi = 300)
clutch_sr <- death_overs %>%
group_by(batsman) %>%
summarise(
balls_faced = n(),
runs_scored = sum(batsman_runs, na.rm = TRUE),
clutch_sr = 100 * runs_scored / balls_faced,
.groups = "drop"
) %>%
filter(balls_faced >= 30, !is.infinite(clutch_sr), !is.na(clutch_sr))
league_avg <- mean(clutch_sr$clutch_sr, na.rm = TRUE)
clutch_sr_sd <- sd(clutch_sr$clutch_sr, na.rm = TRUE)
shrink_prior <- 50
clutch_sr <- clutch_sr %>%
mutate(
weight = balls_faced / (balls_faced + shrink_prior),
shrunk_sr = weight * clutch_sr + (1 - weight) * league_avg,
cpi = (shrunk_sr - league_avg) / clutch_sr_sd
)
cat("\n=== Top 10 Players by Shrunk Strike Rate ===\n")
print(head(clutch_sr %>% arrange(desc(shrunk_sr)), 10))
cat("\n=== Summary of Shrunk Strike Rate ===\n")
print(descr(clutch_sr$shrunk_sr,
stats = c("mean", "sd", "min", "max", "n"),
transpose = TRUE))
mean_shrunk <- mean(clutch_sr$shrunk_sr, na.rm = TRUE)
var_shrunk <- var(clutch_sr$shrunk_sr, na.rm = TRUE)
sd_shrunk <- sd(clutch_sr$shrunk_sr, na.rm = TRUE)
p_shrunk <- ggplot(clutch_sr, aes(x = shrunk_sr)) +
geom_histogram(aes(y = after_stat(density)),
bins = 25,
fill = "steelblue",
alpha = 0.7,
color = "black") +
geom_density(color = "red", linewidth = 1) +
geom_vline(xintercept = mean_shrunk, color = "blue", linetype = "dashed", linewidth = 1) +
annotate("text",
x = mean_shrunk,
y = Inf,
label = paste0("Mean = ", round(mean_shrunk, 2)),
vjust = 2,
color = "blue",
fontface = "bold") +
annotate("text",
x = min(clutch_sr$shrunk_sr),
y = Inf,
label = paste0("Var = ", round(var_shrunk, 2),
"\nSD = ", round(sd_shrunk, 2)),
hjust = 0,
vjust = 2,
color = "darkgreen",
fontface = "bold") +
labs(title = "Distribution of Shrunk Clutch SR",
subtitle = "Bayesian-adjusted performance in death overs",
x = "shrunk_sr", y = "Density") +
theme_minimal()
print(p_shrunk)
ggsave("shrunk_sr_distribution.png", p_shrunk, width = 10, height = 6, dpi = 300)
top_players <- clutch_sr %>%
filter(balls_faced >= 100) %>%
arrange(desc(cpi)) %>%
slice_head(n = 100)
cat("\n=== Top Clutch Performers ===\n")
cat("TOP", nrow(top_players), "players generated!\n")
top20 <- top_players %>% slice_head(n = 20)
p_top20 <- ggplot(top20, aes(x = reorder(batsman, cpi), y = cpi)) +
geom_col(fill = "darkgreen", alpha = 0.8) +
coord_flip() +
labs(title = "Top 20 Clutch Finishers",
subtitle = "IPL Death Overs 16-20 | Min 100 balls",
x = "Batsman", y = "CPI") +
theme_minimal() +
theme(axis.text.y = element_text(size = 9, fontface = "bold"))
print(p_top20)
ggsave("slide1_top20.png", p_top20, width = 10, height = 8, dpi = 300)
top10 <- top_players %>% slice_head(n = 10)
p_top10 <- ggplot(top10, aes(x = reorder(batsman, cpi), y = cpi)) +
geom_col(aes(fill = batsman), alpha = 0.9, width = 0.7, color = "black") +
scale_fill_viridis_d(option = "plasma", direction = -1) +
geom_text(aes(label = round(cpi, 2)), hjust = -0.05, fontface = "bold", size = 4, color = "white") +
labs(title = "Top 10 Clutch Finishers (Bayesian CPI)",
subtitle = "IPL Death Overs 16-20 | Min 100 balls",
x = "Batsman", y = "CPI Score") +
theme_minimal() +
theme(legend.position = "none",
axis.text.y = element_text(size = 12, fontface = "bold"),
plot.title = element_text(size = 18, face = "bold"),
plot.subtitle = element_text(size = 12)) +
coord_flip()
print(p_top10)
ggsave("slide2_top10.png", p_top10, width = 12, height = 9, dpi = 300)
cat("\n=== Creating Simple Final Plot ===\n")
# Simple, clean top 10 bar chart with extra stats
top10_final <- top_players %>%
slice_head(n = 10) %>%
mutate(
batsman = reorder(batsman, cpi),
sr_label = round(shrunk_sr, 1)
)
p_final <- ggplot(top10_final, aes(x = batsman, y = cpi, fill = cpi)) +
geom_col(width = 0.7, color = "black", alpha = 0.9) +
geom_text(aes(label = paste0("SR: ", sr_label)),
vjust = -0.5,
fontface = "bold",
size = 3.5,
color = "black") +
scale_fill_viridis(option = "plasma", direction = -1, alpha = 0.8) +
labs(
title = "🏏 Top 10 Clutch Finishers in IPL Death Overs",
subtitle = "Bayesian-adjusted CPI | Min 100 balls | Overs 16-20",
x = "Batsman",
y = "Clutch Performance Index (CPI)"
) +
theme_minimal(base_size = 14) +
theme(
legend.position = "none",
axis.text.x = element_text(size = 10, fontface = "bold", angle = 45, hjust = 1),
axis.text.y = element_text(size = 11, fontface = "bold"),
plot.title = element_text(size = 20, face = "bold", family = "sans"),
plot.subtitle = element_text(size = 13, color = "darkgray"),
plot.margin = margin(20, 20, 20, 20)
) +
coord_flip()
print(p_final)
ggsave("final_clutch_finishers.png", p_final, width = 12, height = 8, dpi = 300)
cat("\n=== Files Generated ===\n")
print(list.files(pattern = "*.png"))
cat("\n=== Top 5 Players Summary ===\n")
print(head(top_players, 5))
cat("\n=== Analysis Complete ===\n")