TimStats commited on
Commit
fd9a5eb
·
verified ·
1 Parent(s): 44e6c8e

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +63 -25
app.R CHANGED
@@ -114,7 +114,53 @@ pitcher_summary <- function(game_pk,date){
114
  return(tmilb)
115
  }
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  calculate_timstuff <- function(game) {
 
118
  game <- game %>%
119
  mutate(VAA = calculate_VAA(vz0, ay, az, vy0, y0),
120
  EAA = calculate_EAA(extension),
@@ -122,37 +168,31 @@ calculate_timstuff <- function(game) {
122
  team_fielding_id = ifelse(description %in% c("Called Strike", "Swinging Strike", "Swinging Strike (Blocked)"), 1, 0),
123
  swing = ifelse(description %in% c("Foul", "Foul Pitchout", "In play, no out", "In play, out(s)", "In play, run(s)", "Swinging Strike", "Swinging Strike (Blocked)", "Foul Tip"), 1, 0),
124
  is_strike_swinging = ifelse(is_strike_swinging, 1, 0),
125
- Pitch = pitch_name)
126
-
127
- feature_vars <- c("start_speed", "IVB", "HB", "EAA", "x0", "z0", "spin_rate", "SADiff")
 
 
128
  complete_rows <- complete.cases(game[, feature_vars])
129
  game_complete <- game[complete_rows, ]
130
  game_na <- game[!complete_rows,]
131
  game_na$TimStuff <- NA
132
 
133
- lhp_fb <- game_complete[game_complete$phand == 'L' & game_complete$Pitch %in% c("Four-Seam Fastball", "Sinker", "Cutter", "Fastball"), ]
134
- lhp_off <- game_complete[game_complete$phand == 'L' & game_complete$Pitch %in% c("Changeup", "Splitter", "Screwball", "Forkball"), ]
135
- lhp_break <- game_complete[game_complete$phand == 'L' & game_complete$Pitch %in% c("Slider", "Slurve", "Sweeper", "Curveball", "Knuckle Curve", "Slow Curve", "Knuckleball"), ]
136
-
137
- rhp_fb <- game_complete[game_complete$phand == 'R' & game_complete$Pitch %in% c("Four-Seam Fastball", "Sinker", "Cutter", "Fastball"), ]
138
- rhp_off <- game_complete[game_complete$phand == 'R' & game_complete$Pitch %in% c("Changeup", "Splitter", "Screwball", "Forkball"), ]
139
- rhp_break <- game_complete[game_complete$phand == 'R' & game_complete$Pitch %in% c("Slider", "Slurve", "Sweeper", "Curveball", "Knuckle Curve", "Slow Curve", "Knuckleball"), ]
140
-
141
- lhp_fb$TimStuff <- scale_TimStuff(predict(FB, as.matrix(cbind(lhp_fb$start_speed, lhp_fb$IVB, -lhp_fb$HB, lhp_fb$EAA, -lhp_fb$x0, lhp_fb$z0, lhp_fb$spin_rate, lhp_fb$SADiff))), -0.0011801, 0.007989927)
142
- lhp_off$TimStuff <- scale_TimStuff(predict(Off, as.matrix(cbind(lhp_off$start_speed, lhp_off$IVB, -lhp_off$HB, lhp_off$EAA, -lhp_off$x0, lhp_off$z0, lhp_off$spin_rate, lhp_off$SADiff))), -0.002239657, 0.01043216)
143
- lhp_break$TimStuff <- scale_TimStuff(predict(Break, as.matrix(cbind(lhp_break$start_speed, lhp_break$IVB, -lhp_break$HB, lhp_break$EAA, -lhp_break$x0, lhp_break$z0, lhp_break$spin_rate, lhp_break$SADiff))), -0.004822031, 0.007912765)
144
-
145
- rhp_fb$TimStuff <- scale_TimStuff(predict(FB, as.matrix(cbind(rhp_fb$start_speed, rhp_fb$IVB, rhp_fb$HB, rhp_fb$EAA, rhp_fb$x0, rhp_fb$z0, rhp_fb$spin_rate, rhp_fb$SADiff))), -0.0011801, 0.007989927)
146
- rhp_off$TimStuff <- scale_TimStuff(predict(Off, as.matrix(cbind(rhp_off$start_speed, rhp_off$IVB, rhp_off$HB, rhp_off$EAA, rhp_off$x0, rhp_off$z0, rhp_off$spin_rate, rhp_off$SADiff))), -0.002239657, 0.01043216)
147
- rhp_break$TimStuff <- scale_TimStuff(predict(Break, as.matrix(cbind(rhp_break$start_speed, rhp_break$IVB, rhp_break$HB, rhp_break$EAA, rhp_break$x0, rhp_break$z0, rhp_break$spin_rate, rhp_break$SADiff))), -0.004822031, 0.007912765)
148
-
149
- game_complete <- rbind(lhp_fb, lhp_off, lhp_break, rhp_fb, rhp_off, rhp_break,game_na)
150
  return(game_complete)
151
  }
152
 
153
  scale_TimStuff <- function(raw_score, model_mean, model_sd) {
154
  scaled_score <- (raw_score - model_mean) / model_sd
155
- result <- 50 - (scaled_score * 10)
156
  return(result)
157
  }
158
 
@@ -178,9 +218,7 @@ pitch_plot <- function(game){
178
  }
179
 
180
  # Load models
181
- FB <- xgb.load('FB.model')
182
- Off <- xgb.load('Off.model')
183
- Break <- xgb.load('Break.model')
184
 
185
  summary_table <- function(data,pitchern){
186
  # rows <- nrow(data)
@@ -200,7 +238,7 @@ summary_table <- function(data,pitchern){
200
  'VAA' = round(mean(VAA, na.rm = TRUE), digits = 1),
201
  'CSW%' = round(sum(team_fielding_id, na.rm = TRUE) / sum(!is.na(team_fielding_id)) * 100, digits = 1),
202
  'Whiff%' = round(sum(is_strike_swinging, na.rm = TRUE) / sum(swing, na.rm = TRUE) * 100, digits = 1),
203
- 'TimStuff' = round(mean(TimStuff, na.rm = TRUE), digits = 0)
204
  ) %>%
205
  arrange(-Pitches)
206
 
 
114
  return(tmilb)
115
  }
116
 
117
+ calculate_primary <- function(data){
118
+ data <- data %>%
119
+ # Group by pitch_name, Pitcher Name, Pitcher Id, and date
120
+ group_by(pitch_name, `Pitcher Name`, `Pitcher ID`, date) %>%
121
+
122
+ # Count occurrences and calculate average start_speed, IVB, and HB for each group
123
+ mutate(
124
+ pitch_count = n(),
125
+ avg_start_speed = mean(start_speed, na.rm = TRUE),
126
+ avg_IVB = mean(IVB, na.rm = TRUE),
127
+ avg_HB = mean(HB, na.rm = TRUE)
128
+ ) %>%
129
+
130
+ ungroup() %>%
131
+ # Group by Pitcher Name, Pitcher Id, and date
132
+ group_by(`Pitcher Name`, `Pitcher ID`, date) %>%
133
+
134
+ # Add a column to identify the highest occurrence
135
+ mutate(
136
+ is_highest_occurrence = case_when(
137
+ pitch_count == max(pitch_count) ~ 1,
138
+ TRUE ~ 0
139
+ )
140
+ ) %>%
141
+
142
+ # If there's a tie, use avg_start_speed as a tiebreaker
143
+ mutate(
144
+ is_highest_occurrence = case_when(
145
+ is_highest_occurrence == 1 & pitch_count == max(pitch_count[is_highest_occurrence == 1]) &
146
+ avg_start_speed == max(avg_start_speed[is_highest_occurrence == 1]) ~ 1,
147
+ TRUE ~ 0
148
+ )
149
+ ) %>%
150
+
151
+ # Calculate primary pitch metrics
152
+ mutate(
153
+ primary_speed = avg_start_speed[is_highest_occurrence == 1][1],
154
+ primary_IVB = avg_IVB[is_highest_occurrence == 1][1],
155
+ primary_HB = avg_HB[is_highest_occurrence == 1][1]
156
+ ) %>%
157
+
158
+ # Ungroup to remove grouping structure
159
+ ungroup()
160
+ }
161
+
162
  calculate_timstuff <- function(game) {
163
+ game <- calculate_primary(game)
164
  game <- game %>%
165
  mutate(VAA = calculate_VAA(vz0, ay, az, vy0, y0),
166
  EAA = calculate_EAA(extension),
 
168
  team_fielding_id = ifelse(description %in% c("Called Strike", "Swinging Strike", "Swinging Strike (Blocked)"), 1, 0),
169
  swing = ifelse(description %in% c("Foul", "Foul Pitchout", "In play, no out", "In play, out(s)", "In play, run(s)", "Swinging Strike", "Swinging Strike (Blocked)", "Foul Tip"), 1, 0),
170
  is_strike_swinging = ifelse(is_strike_swinging, 1, 0),
171
+ Pitch = pitch_name,
172
+ ishandL = ifelse(phand == "L",1,0))
173
+ # game <- calculate_primary(game)
174
+
175
+ feature_vars <- c("ishandL","start_speed", "IVB", "HB", "EAA", "x0", "z0", "spin_rate","SADiff","primary_speed","primary_IVB","primary_HB")
176
  complete_rows <- complete.cases(game[, feature_vars])
177
  game_complete <- game[complete_rows, ]
178
  game_na <- game[!complete_rows,]
179
  game_na$TimStuff <- NA
180
 
181
+ rhp <- game_complete
182
+
183
+ # rhp <- game_complete[game_complete$ishandL == 0]
184
+ #
185
+ # lhp$TimStuff <- scale_TimStuff(predict(model, as.matrix(cbind(lhp$ishandL,lhp$start_speed, lhp$IVB, lhp$HB, lhp$EAA, lhp$x0, lhp$z0, lhp$spin_rate, lhp$SADiff,lhp$primary_speed,lhp$primary_IVB,lhp$primary_HB))), -0.00249975, 0.007566558)
186
+
187
+ rhp$TimStuff <- scale_TimStuff(predict(model, as.matrix(cbind(rhp$ishandL,rhp$start_speed, rhp$IVB, rhp$HB, rhp$EAA, rhp$x0, rhp$z0, rhp$spin_rate, rhp$SADiff,rhp$primary_speed,rhp$primary_IVB,rhp$primary_HB))), -0.00249975, 0.007566558)
188
+
189
+ game_complete <- rbind(rhp,game_na)
 
 
 
 
 
 
 
 
190
  return(game_complete)
191
  }
192
 
193
  scale_TimStuff <- function(raw_score, model_mean, model_sd) {
194
  scaled_score <- (raw_score - model_mean) / model_sd
195
+ result <- 100 - (scaled_score * 10)
196
  return(result)
197
  }
198
 
 
218
  }
219
 
220
  # Load models
221
+ model <- xgb.load('TimStuff2.model')
 
 
222
 
223
  summary_table <- function(data,pitchern){
224
  # rows <- nrow(data)
 
238
  'VAA' = round(mean(VAA, na.rm = TRUE), digits = 1),
239
  'CSW%' = round(sum(team_fielding_id, na.rm = TRUE) / sum(!is.na(team_fielding_id)) * 100, digits = 1),
240
  'Whiff%' = round(sum(is_strike_swinging, na.rm = TRUE) / sum(swing, na.rm = TRUE) * 100, digits = 1),
241
+ 'TimStuff+' = round(mean(TimStuff, na.rm = TRUE), digits = 0)
242
  ) %>%
243
  arrange(-Pitches)
244