Spaces:
Runtime error
Runtime error
Update app.R
Browse files
app.R
CHANGED
|
@@ -21,16 +21,66 @@ library(ggplot2)
|
|
| 21 |
# phand = c("R", "R", "L", "L")
|
| 22 |
# )
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
MLB24T <- read.csv("MLB24T.csv", header = TRUE, check.names = FALSE, fileEncoding = "UTF-8")
|
| 32 |
|
| 33 |
-
MLB24T <-
|
| 34 |
|
| 35 |
pitcher_data <- MLB24T %>%
|
| 36 |
group_by(`Pitcher Name`,pitch_name,phand) %>%
|
|
@@ -46,6 +96,15 @@ pitcher_data <- MLB24T %>%
|
|
| 46 |
)
|
| 47 |
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
ui <- fluidPage(
|
| 50 |
theme = shinytheme("flatly"),
|
| 51 |
|
|
|
|
| 21 |
# phand = c("R", "R", "L", "L")
|
| 22 |
# )
|
| 23 |
|
| 24 |
+
calculate_EAA <- function(extension) {
|
| 25 |
+
extension / 6.3
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
calculate_SADiff <- function(pfxX, pfxZ, spinDirection) {
|
| 29 |
+
inSA <- atan2(pfxZ, pfxX) * 180/pi + 90
|
| 30 |
+
inSA <- ifelse(inSA < 0, inSA + 360, inSA)
|
| 31 |
+
SADiff <- spinDirection - inSA
|
| 32 |
+
SADiff <- ifelse(SADiff > 180, SADiff - 360, SADiff)
|
| 33 |
+
SADiff <- ifelse(SADiff < -180, SADiff + 360, SADiff)
|
| 34 |
+
return(SADiff)
|
| 35 |
}
|
| 36 |
+
|
| 37 |
+
calculate_VAA <- function(vz0, ay, az, vy0, y0) {
|
| 38 |
+
-atan((vz0+(az*(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))-vy0)/
|
| 39 |
+
ay))/(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))))*(180/pi)
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
scale_TimStuff <- function(raw_score, model_mean, model_sd) {
|
| 43 |
+
scaled_score <- (raw_score - model_mean) / model_sd
|
| 44 |
+
result <- 100 - (scaled_score * 10)
|
| 45 |
+
return(result)
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
timstuff <- function(game) {
|
| 49 |
+
# game <- calculate_primary(game)
|
| 50 |
+
game <- game %>%
|
| 51 |
+
mutate(VAA = calculate_VAA(vz0, ay, az, vy0, y0),
|
| 52 |
+
SADiff = calculate_SADiff(pfxX, pfxZ, spinDirection),
|
| 53 |
+
EAA = calculate_EAA(extension),
|
| 54 |
+
#SADiff = calculate_SADiff(pfxX, pfxZ, spinDirection),
|
| 55 |
+
team_fielding_id = ifelse(description %in% c("Called Strike", "Swinging Strike", "Swinging Strike (Blocked)"), 1, 0),
|
| 56 |
+
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),
|
| 57 |
+
is_strike_swinging = ifelse(is_strike_swinging, 1, 0),
|
| 58 |
+
Pitch = pitch_name,
|
| 59 |
+
ishandL = ifelse(phand == "L",1,0))
|
| 60 |
+
#mutate(SADiff = calculate_SADiff(pfxX, pfxZ,spinDirection))
|
| 61 |
+
# game <- calculate_primary(game)
|
| 62 |
+
|
| 63 |
+
feature_vars <- c("ishandL","start_speed", "IVB", "HB", "EAA", "x0", "z0", "spin_rate","SADiff")
|
| 64 |
+
complete_rows <- complete.cases(game[, feature_vars])
|
| 65 |
+
game_complete <- game[complete_rows, ]
|
| 66 |
+
game_na <- game[!complete_rows,]
|
| 67 |
+
game_na$TimStuff <- NA
|
| 68 |
+
|
| 69 |
+
rhp <- game_complete
|
| 70 |
+
|
| 71 |
+
# rhp <- game_complete[game_complete$ishandL == 0]
|
| 72 |
+
#
|
| 73 |
+
# 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)
|
| 74 |
+
|
| 75 |
+
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))), -0.002620635, 0.006021368)
|
| 76 |
+
|
| 77 |
+
game_complete <- rbind(rhp,game_na)
|
| 78 |
+
return(game_complete)
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
MLB24T <- read.csv("MLB24T.csv", header = TRUE, check.names = FALSE, fileEncoding = "UTF-8")
|
| 82 |
|
| 83 |
+
MLB24T <- timstuff(MLB24T)
|
| 84 |
|
| 85 |
pitcher_data <- MLB24T %>%
|
| 86 |
group_by(`Pitcher Name`,pitch_name,phand) %>%
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
|
| 99 |
+
|
| 100 |
+
calculate_timstuff <- function(data) {
|
| 101 |
+
data_matrix <- as.matrix(data)
|
| 102 |
+
mod <- xgb.load('TimStuff2.model')
|
| 103 |
+
prediction <- predict(mod, data_matrix)
|
| 104 |
+
timstuff <- (100 - (prediction - -0.002620635) / 0.006021368 * 10)
|
| 105 |
+
return(timstuff)
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
ui <- fluidPage(
|
| 109 |
theme = shinytheme("flatly"),
|
| 110 |
|