Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
|
@@ -2241,7 +2241,7 @@ has_col_index <- function(idx) {
|
|
| 2241 |
|
| 2242 |
|
| 2243 |
stuffplus_model <- tryCatch({
|
| 2244 |
-
readr::read_rds("
|
| 2245 |
}, error = function(e) {
|
| 2246 |
message("Stuff+ model not found. Predictions will be unavailable.")
|
| 2247 |
NULL
|
|
@@ -2299,68 +2299,40 @@ preflight_predictor_check <- function(model, newdata) {
|
|
| 2299 |
}
|
| 2300 |
|
| 2301 |
ensure_stuff_inputs <- function(df) {
|
| 2302 |
-
core <- c("ZoneTime","ax0","az0","RelHeight","RelSide","SpinRate","SpinAxis","Extension")
|
| 2303 |
-
missing_core <- setdiff(core, names(df))
|
| 2304 |
-
if (length(missing_core)) {
|
| 2305 |
-
message("Stuff+ required raw columns missing: ", paste(missing_core, collapse = ", "))
|
| 2306 |
-
}
|
| 2307 |
-
|
| 2308 |
-
if ("TaggedPitchType" %in% names(df) && nrow(df) > 0) {
|
| 2309 |
-
pitch_counts <- df %>%
|
| 2310 |
-
filter(!is.na(TaggedPitchType), TaggedPitchType != "Other") %>%
|
| 2311 |
-
count(TaggedPitchType, sort = TRUE)
|
| 2312 |
-
|
| 2313 |
-
if (nrow(pitch_counts) > 0) {
|
| 2314 |
-
primary_pitch <- pitch_counts$TaggedPitchType[1]
|
| 2315 |
-
message("Primary pitch detected: ", primary_pitch)
|
| 2316 |
-
|
| 2317 |
-
primary_velo <- mean(df$RelSpeed[df$TaggedPitchType == primary_pitch], na.rm = TRUE)
|
| 2318 |
-
primary_az0 <- mean(df$az0[df$TaggedPitchType == primary_pitch], na.rm = TRUE)
|
| 2319 |
-
|
| 2320 |
-
if (!"velo_diff" %in% names(df)) {
|
| 2321 |
-
if ("RelSpeed" %in% names(df)) {
|
| 2322 |
-
if (is.finite(primary_velo)) {
|
| 2323 |
-
df$velo_diff <- df$RelSpeed - primary_velo
|
| 2324 |
-
message("velo_diff calculated relative to primary pitch (mean=", round(primary_velo, 2), ")")
|
| 2325 |
-
} else {
|
| 2326 |
-
message("Primary velo not finite; setting velo_diff = 0")
|
| 2327 |
-
df$velo_diff <- 0
|
| 2328 |
-
}
|
| 2329 |
-
} else {
|
| 2330 |
-
message("RelSpeed not found; setting velo_diff = 0 as fallback")
|
| 2331 |
-
df$velo_diff <- 0
|
| 2332 |
-
}
|
| 2333 |
-
}
|
| 2334 |
-
|
| 2335 |
-
if (!"az0_diff" %in% names(df)) {
|
| 2336 |
-
if ("az0" %in% names(df)) {
|
| 2337 |
-
if (is.finite(primary_az0)) {
|
| 2338 |
-
df$az0_diff <- df$az0 - primary_az0
|
| 2339 |
-
message("az0_diff calculated relative to primary pitch (mean=", round(primary_az0, 2), ")")
|
| 2340 |
-
} else {
|
| 2341 |
-
message("Primary az0 not finite; setting az0_diff = 0")
|
| 2342 |
-
df$az0_diff <- 0
|
| 2343 |
-
}
|
| 2344 |
-
} else {
|
| 2345 |
-
message("az0 not found; setting az0_diff = 0 as fallback")
|
| 2346 |
-
df$az0_diff <- 0
|
| 2347 |
-
}
|
| 2348 |
-
}
|
| 2349 |
-
} else {
|
| 2350 |
-
message("No valid pitch types found; setting engineered features = 0")
|
| 2351 |
-
if (!"velo_diff" %in% names(df)) df$velo_diff <- 0
|
| 2352 |
-
if (!"az0_diff" %in% names(df)) df$az0_diff <- 0
|
| 2353 |
-
}
|
| 2354 |
-
} else {
|
| 2355 |
-
message("No TaggedPitchType column; setting engineered features = 0")
|
| 2356 |
-
if (!"velo_diff" %in% names(df)) df$velo_diff <- 0
|
| 2357 |
-
if (!"az0_diff" %in% names(df)) df$az0_diff <- 0
|
| 2358 |
-
}
|
| 2359 |
|
| 2360 |
df <- df %>%
|
| 2361 |
-
mutate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2362 |
|
| 2363 |
df
|
|
|
|
| 2364 |
}
|
| 2365 |
|
| 2366 |
standardize_stuffplus_to_league <- function(data, league_comparison_data) {
|
|
|
|
| 2241 |
|
| 2242 |
|
| 2243 |
stuffplus_model <- tryCatch({
|
| 2244 |
+
readr::read_rds("new_stuffplus_model.rds")
|
| 2245 |
}, error = function(e) {
|
| 2246 |
message("Stuff+ model not found. Predictions will be unavailable.")
|
| 2247 |
NULL
|
|
|
|
| 2299 |
}
|
| 2300 |
|
| 2301 |
ensure_stuff_inputs <- function(df) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2302 |
|
| 2303 |
df <- df %>%
|
| 2304 |
+
mutate(RelSide = case_when(
|
| 2305 |
+
PitcherThrows == "Right" ~ RelSide,
|
| 2306 |
+
PitcherThrows == "Left" ~ -RelSide,
|
| 2307 |
+
PitcherThrows %in% c("Both", "Undefined") & RelSide > 0 ~ RelSide,
|
| 2308 |
+
PitcherThrows %in% c("Both", "Undefined") & RelSide < 0 ~ -RelSide),
|
| 2309 |
+
ax0 = case_when(
|
| 2310 |
+
PitcherThrows == "Right" ~ ax0,
|
| 2311 |
+
PitcherThrows == "Left" ~ -ax0,
|
| 2312 |
+
PitcherThrows %in% c("Both", "Undefined") & ax0 > 0 ~ ax0,
|
| 2313 |
+
PitcherThrows %in% c("Both", "Undefined") & ax0 < 0 ~ -ax0),
|
| 2314 |
+
PlateLocHeight = PlateLocHeight*12,
|
| 2315 |
+
PlateLocSide = PlateLocSide*12,
|
| 2316 |
+
ax0 = -ax0) %>%
|
| 2317 |
+
group_by(Pitcher, GameID) %>%
|
| 2318 |
+
mutate(
|
| 2319 |
+
primary_pitch = case_when(
|
| 2320 |
+
any(TaggedPitchType == "Fastball") ~ "Fastball",
|
| 2321 |
+
any(TaggedPitchType == "Sinker") ~ "Sinker",
|
| 2322 |
+
TRUE ~ names(sort(table(TaggedPitchType), decreasing = TRUE))[1]
|
| 2323 |
+
)
|
| 2324 |
+
) %>%
|
| 2325 |
+
group_by(Pitcher, GameID, primary_pitch) %>%
|
| 2326 |
+
mutate(
|
| 2327 |
+
primary_az0 = mean(az0[TaggedPitchType == primary_pitch], na.rm = TRUE),
|
| 2328 |
+
primary_velo = mean(RelSpeed[TaggedPitchType == primary_pitch], na.rm = TRUE)
|
| 2329 |
+
) %>%
|
| 2330 |
+
ungroup() %>%
|
| 2331 |
+
mutate(az0_diff = az0 - primary_az0,
|
| 2332 |
+
velo_diff = RelSpeed - primary_velo)
|
| 2333 |
|
| 2334 |
df
|
| 2335 |
+
|
| 2336 |
}
|
| 2337 |
|
| 2338 |
standardize_stuffplus_to_league <- function(data, league_comparison_data) {
|