Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -1,26 +1,14 @@
|
|
| 1 |
# Load required libraries
|
| 2 |
library(shiny)
|
| 3 |
library(plotly)
|
| 4 |
-
# library(gridlayout)
|
| 5 |
library(bslib)
|
| 6 |
library(DT)
|
| 7 |
library(rsconnect)
|
| 8 |
library(baseballr)
|
| 9 |
library(dplyr)
|
| 10 |
-
# library(tidyverse)
|
| 11 |
-
# library(rvest)
|
| 12 |
library(ggplot2)
|
| 13 |
-
# library(janitor)
|
| 14 |
-
# library(ggthemes)
|
| 15 |
-
# library(ggpubr)
|
| 16 |
-
# library(jsonlite)
|
| 17 |
-
# library(utils)
|
| 18 |
-
# library(grid)
|
| 19 |
-
# library(gridExtra)
|
| 20 |
-
# library(png)
|
| 21 |
library(xgboost)
|
| 22 |
library(httr)
|
| 23 |
-
# library(jpeg)
|
| 24 |
|
| 25 |
#pdf(file = NULL)
|
| 26 |
Sys.setenv(TZ='EST')
|
|
@@ -114,57 +102,8 @@ pitcher_summary <- function(game_pk,date){
|
|
| 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 |
-
mutate(primvelodiff = primary_speed - start_speed,
|
| 161 |
-
primivbdiff = primary_IVB - IVB,
|
| 162 |
-
primhbdiff = primary_HB - HB)
|
| 163 |
-
|
| 164 |
-
}
|
| 165 |
-
|
| 166 |
calculate_timstuff <- function(game) {
|
| 167 |
-
|
| 168 |
game <- game %>%
|
| 169 |
mutate(VAA = calculate_VAA(vz0, ay, az, vy0, y0),
|
| 170 |
EAA = calculate_EAA(extension),
|
|
@@ -176,7 +115,7 @@ calculate_timstuff <- function(game) {
|
|
| 176 |
ishandL = ifelse(phand == "L",1,0))
|
| 177 |
# game <- calculate_primary(game)
|
| 178 |
|
| 179 |
-
feature_vars <- c("ishandL","start_speed", "IVB", "HB", "EAA", "x0", "z0", "spin_rate","SADiff"
|
| 180 |
complete_rows <- complete.cases(game[, feature_vars])
|
| 181 |
game_complete <- game[complete_rows, ]
|
| 182 |
game_na <- game[!complete_rows,]
|
|
@@ -188,7 +127,7 @@ calculate_timstuff <- function(game) {
|
|
| 188 |
#
|
| 189 |
# 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)
|
| 190 |
|
| 191 |
-
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
|
| 192 |
|
| 193 |
game_complete <- rbind(rhp,game_na)
|
| 194 |
return(game_complete)
|
|
|
|
| 1 |
# Load required libraries
|
| 2 |
library(shiny)
|
| 3 |
library(plotly)
|
|
|
|
| 4 |
library(bslib)
|
| 5 |
library(DT)
|
| 6 |
library(rsconnect)
|
| 7 |
library(baseballr)
|
| 8 |
library(dplyr)
|
|
|
|
|
|
|
| 9 |
library(ggplot2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
library(xgboost)
|
| 11 |
library(httr)
|
|
|
|
| 12 |
|
| 13 |
#pdf(file = NULL)
|
| 14 |
Sys.setenv(TZ='EST')
|
|
|
|
| 102 |
return(tmilb)
|
| 103 |
}
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
calculate_timstuff <- function(game) {
|
| 106 |
+
# game <- calculate_primary(game)
|
| 107 |
game <- game %>%
|
| 108 |
mutate(VAA = calculate_VAA(vz0, ay, az, vy0, y0),
|
| 109 |
EAA = calculate_EAA(extension),
|
|
|
|
| 115 |
ishandL = ifelse(phand == "L",1,0))
|
| 116 |
# game <- calculate_primary(game)
|
| 117 |
|
| 118 |
+
feature_vars <- c("ishandL","start_speed", "IVB", "HB", "EAA", "x0", "z0", "spin_rate","SADiff")
|
| 119 |
complete_rows <- complete.cases(game[, feature_vars])
|
| 120 |
game_complete <- game[complete_rows, ]
|
| 121 |
game_na <- game[!complete_rows,]
|
|
|
|
| 127 |
#
|
| 128 |
# 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)
|
| 129 |
|
| 130 |
+
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.002549184, 0.006202895)
|
| 131 |
|
| 132 |
game_complete <- rbind(rhp,game_na)
|
| 133 |
return(game_complete)
|