Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
|
@@ -2328,14 +2328,8 @@ create_advanced_pitch_characteristics <- function(data, player_name) {
|
|
| 2328 |
h <- pitcher_data$PitcherThrows[1]; if (is.na(h)) "Right" else h
|
| 2329 |
} else "Right"
|
| 2330 |
|
| 2331 |
-
if (nrow(pitcher_data) > 0) {
|
| 2332 |
-
pitcher_data <- ensure_stuff_inputs(pitcher_data)
|
| 2333 |
-
}
|
| 2334 |
-
|
| 2335 |
if (!is.null(stuffplus_model) && nrow(pitcher_data) > 0) {
|
| 2336 |
-
|
| 2337 |
-
if (!is.null(reference_data_for_stuff)) {
|
| 2338 |
-
|
| 2339 |
combined <- dplyr::bind_rows(
|
| 2340 |
reference_data_for_stuff$spring,
|
| 2341 |
reference_data_for_stuff$p5,
|
|
@@ -2343,9 +2337,9 @@ create_advanced_pitch_characteristics <- function(data, player_name) {
|
|
| 2343 |
)
|
| 2344 |
|
| 2345 |
pitcher_data <- standardize_stuffplus_to_league(pitcher_data, combined)
|
| 2346 |
-
|
| 2347 |
} else {
|
| 2348 |
-
|
| 2349 |
message("WARNING: No reference data available. Using local standardization.")
|
| 2350 |
|
| 2351 |
# Direct prediction for local standardization path
|
|
@@ -2355,7 +2349,7 @@ create_advanced_pitch_characteristics <- function(data, player_name) {
|
|
| 2355 |
message("Stuff+ prediction error: ", e$message)
|
| 2356 |
rep(NA_real_, nrow(pitcher_data))
|
| 2357 |
})
|
| 2358 |
-
|
| 2359 |
finite_raw <- pitcher_data$raw_stuff[is.finite(pitcher_data$raw_stuff)]
|
| 2360 |
if (length(finite_raw) == 0) {
|
| 2361 |
pitcher_data$stuff_plus <- NA_real_
|
|
@@ -2363,24 +2357,24 @@ create_advanced_pitch_characteristics <- function(data, player_name) {
|
|
| 2363 |
q <- quantile(finite_raw, probs = c(0.01, 0.99), na.rm = TRUE)
|
| 2364 |
lo <- q[1]; hi <- q[2]
|
| 2365 |
pitcher_data$raw_stuff_winz <- pmin(pmax(pitcher_data$raw_stuff, lo), hi)
|
| 2366 |
-
|
| 2367 |
raw_mean <- mean(pitcher_data$raw_stuff_winz)
|
| 2368 |
raw_sd <- sd(pitcher_data$raw_stuff_winz)
|
| 2369 |
if (!is.finite(raw_sd) || raw_sd == 0) raw_sd <- 1e-8
|
| 2370 |
-
|
| 2371 |
pitcher_data$stuff_plus <- ((pitcher_data$raw_stuff_winz - raw_mean) / raw_sd) * 10 + 100
|
| 2372 |
}
|
| 2373 |
}
|
| 2374 |
-
|
| 2375 |
} else {
|
| 2376 |
-
|
| 2377 |
if (is.null(stuffplus_model)) message("Stuff+ model not loaded")
|
| 2378 |
if (nrow(pitcher_data) == 0) message("No pitcher data for Stuff+ prediction")
|
| 2379 |
-
|
| 2380 |
pitcher_data$raw_stuff <- NA_real_
|
| 2381 |
pitcher_data$stuff_plus <- NA_real_
|
| 2382 |
}
|
| 2383 |
-
|
| 2384 |
|
| 2385 |
pitch_stats <- pitcher_data %>%
|
| 2386 |
dplyr::group_by(Pitch = TaggedPitchType) %>%
|
|
@@ -2482,6 +2476,7 @@ create_advanced_pitch_characteristics <- function(data, player_name) {
|
|
| 2482 |
list(stats = pitch_stats, colors = color_matrix)
|
| 2483 |
}
|
| 2484 |
|
|
|
|
| 2485 |
create_movement_plot <- create_pitcher_movement_plot
|
| 2486 |
|
| 2487 |
create_velocity_distribution_plot <- function(data, player_name, pitch_colors) {
|
|
|
|
| 2328 |
h <- pitcher_data$PitcherThrows[1]; if (is.na(h)) "Right" else h
|
| 2329 |
} else "Right"
|
| 2330 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2331 |
if (!is.null(stuffplus_model) && nrow(pitcher_data) > 0) {
|
| 2332 |
+
|
|
|
|
|
|
|
| 2333 |
combined <- dplyr::bind_rows(
|
| 2334 |
reference_data_for_stuff$spring,
|
| 2335 |
reference_data_for_stuff$p5,
|
|
|
|
| 2337 |
)
|
| 2338 |
|
| 2339 |
pitcher_data <- standardize_stuffplus_to_league(pitcher_data, combined)
|
| 2340 |
+
|
| 2341 |
} else {
|
| 2342 |
+
|
| 2343 |
message("WARNING: No reference data available. Using local standardization.")
|
| 2344 |
|
| 2345 |
# Direct prediction for local standardization path
|
|
|
|
| 2349 |
message("Stuff+ prediction error: ", e$message)
|
| 2350 |
rep(NA_real_, nrow(pitcher_data))
|
| 2351 |
})
|
| 2352 |
+
|
| 2353 |
finite_raw <- pitcher_data$raw_stuff[is.finite(pitcher_data$raw_stuff)]
|
| 2354 |
if (length(finite_raw) == 0) {
|
| 2355 |
pitcher_data$stuff_plus <- NA_real_
|
|
|
|
| 2357 |
q <- quantile(finite_raw, probs = c(0.01, 0.99), na.rm = TRUE)
|
| 2358 |
lo <- q[1]; hi <- q[2]
|
| 2359 |
pitcher_data$raw_stuff_winz <- pmin(pmax(pitcher_data$raw_stuff, lo), hi)
|
| 2360 |
+
|
| 2361 |
raw_mean <- mean(pitcher_data$raw_stuff_winz)
|
| 2362 |
raw_sd <- sd(pitcher_data$raw_stuff_winz)
|
| 2363 |
if (!is.finite(raw_sd) || raw_sd == 0) raw_sd <- 1e-8
|
| 2364 |
+
|
| 2365 |
pitcher_data$stuff_plus <- ((pitcher_data$raw_stuff_winz - raw_mean) / raw_sd) * 10 + 100
|
| 2366 |
}
|
| 2367 |
}
|
| 2368 |
+
|
| 2369 |
} else {
|
| 2370 |
+
|
| 2371 |
if (is.null(stuffplus_model)) message("Stuff+ model not loaded")
|
| 2372 |
if (nrow(pitcher_data) == 0) message("No pitcher data for Stuff+ prediction")
|
| 2373 |
+
|
| 2374 |
pitcher_data$raw_stuff <- NA_real_
|
| 2375 |
pitcher_data$stuff_plus <- NA_real_
|
| 2376 |
}
|
| 2377 |
+
|
| 2378 |
|
| 2379 |
pitch_stats <- pitcher_data %>%
|
| 2380 |
dplyr::group_by(Pitch = TaggedPitchType) %>%
|
|
|
|
| 2476 |
list(stats = pitch_stats, colors = color_matrix)
|
| 2477 |
}
|
| 2478 |
|
| 2479 |
+
|
| 2480 |
create_movement_plot <- create_pitcher_movement_plot
|
| 2481 |
|
| 2482 |
create_velocity_distribution_plot <- function(data, player_name, pitch_colors) {
|