TimStats commited on
Commit
8b83933
·
verified ·
1 Parent(s): eea07f7

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +60 -78
app.R CHANGED
@@ -4,12 +4,18 @@ library(data.table)
4
  library(xgboost)
5
  library(caret)
6
  library(shinythemes)
 
 
7
  calculate_timstuff <- function(data, pitch_type) {
8
  data_matrix <- as.matrix(data)
9
  if (pitch_type == "Offspeed") {
10
  mod <- xgb.load('Off.model')
11
  prediction <- predict(mod, data_matrix)
12
  timstuff <- (50 - (prediction - -0.002239657) / 0.01043216 * 10)
 
 
 
 
13
  } else {
14
  mod <- xgb.load('FB.model')
15
  prediction <- predict(mod, data_matrix)
@@ -17,17 +23,16 @@ calculate_timstuff <- function(data, pitch_type) {
17
  }
18
  return(timstuff)
19
  }
 
20
  ui <- fluidPage(
21
  theme = shinytheme("flatly"),
22
 
23
- # Application title
24
- titlePanel("TimStuff Fastball/Offspeed Calculator"),
25
 
26
- # Sidebar with inputs
27
  sidebarLayout(
28
  sidebarPanel(
29
  width = 4,
30
- radioButtons("pitch", "Pitch Type", choices = c("Fastball", "Offspeed"), inline = TRUE),
31
  radioButtons("phand", "Hand", choices = c("R", "L"), inline = TRUE),
32
 
33
  numericInput("velo", "Velocity (mph)", value = 90, min = 70, max = 110, step = 1),
@@ -44,18 +49,15 @@ ui <- fluidPage(
44
  "Horizontal Break" = "HB",
45
  "Spin Rate" = "spin_rate",
46
  "Extension" = "EAA",
47
- "Spin Axis Difference" = "SADiff",
48
- "Horizontal Release Point" = "x0",
49
- "Vertical Release Point" = "z0"))
50
  ),
51
 
52
- # Main panel with output
53
  mainPanel(
54
  width = 8,
55
  wellPanel(
56
  h3("Results"),
57
  verbatimTextOutput("stuff"),
58
- plotOutput("feature_impact", height = "400px") # Placeholder for future visualization
59
  )
60
  )
61
  )
@@ -67,7 +69,7 @@ server <- function(input, output) {
67
  data <- data.frame(
68
  start_speed = input$velo,
69
  IVB = input$ivb,
70
- HB = ifelse(input$phand == "L", -input$hb, input$hb),
71
  EAA = input$ext / 6.3,
72
  x0 = ifelse(input$phand == "L", -input$x0, input$x0),
73
  z0 = input$z0,
@@ -77,79 +79,59 @@ server <- function(input, output) {
77
 
78
  data <- as.matrix(data)
79
 
80
- if (input$pitch == "Offspeed") {
81
- mod <- xgb.load('Off.model')
82
- prediction <- predict(mod, data)
83
- TimStuff <- (50 - (prediction - -0.002239657) / 0.01043216 * 10)
84
- } else {
85
- mod <- xgb.load('FB.model')
86
- prediction <- predict(mod, data)
87
- TimStuff <- (50 - (prediction - -0.0011801) / 0.007989927 * 10)
88
- }
89
 
90
  paste("TimStuff:", round(TimStuff, 2), "\n20-80 Scale, 50 is Average, 1 SD is 10")
91
  })
92
 
93
- # Add this new reactive expression
94
- # Add this new reactive expression
95
- feature_range <- reactive({
96
- ranges <- list(
97
- start_speed = c(70, 110),
98
- IVB = c(-20, 30),
99
- HB = c(-20, 20),
100
- spin_rate = c(1000, 3500),
101
- EAA = c(5/6.3, 8/6.3),
102
- SADiff = c(-180, 180),
103
- x0 = c(-5, 5),
104
- z0 = c(3, 8)
105
- )
106
- ranges[[input$feature_to_vary]]
107
- })
108
 
109
- # Update the feature_impact plot
110
- output$feature_impact <- renderPlot({
111
- # Create base data frame with current inputs
112
- base_data <- data.frame(
113
- start_speed = input$velo,
114
- IVB = input$ivb,
115
- HB = ifelse(input$phand == "L", -input$hb, input$hb),
116
- EAA = input$ext / 6.3,
117
- x0 = ifelse(input$phand == "L", -input$x0, input$x0),
118
- z0 = input$z0,
119
- spin_rate = input$spinrate,
120
- SADiff = input$spinaxisdiff
121
- )
122
-
123
- # Generate sequence for the selected feature
124
- feature_seq <- seq(feature_range()[1], feature_range()[2], length.out = 100)
125
-
126
- # Calculate TimStuff for the sequence
127
- varied_data <- do.call(rbind, replicate(100, base_data, simplify = FALSE))
128
- varied_data[[input$feature_to_vary]] <- feature_seq
129
- timstuff <- calculate_timstuff(varied_data, input$pitch)
130
-
131
- # Create plot data
132
- plot_data <- data.frame(
133
- Feature = names(base_data)[which(names(base_data) == input$feature_to_vary)],
134
- Value = feature_seq,
135
- TimStuff = timstuff
136
- )
137
-
138
- # Create plot
139
- ggplot(plot_data, aes(x = Value, y = TimStuff)) +
140
- geom_line(color = "blue", size = 1) +
141
- geom_point(aes(x = base_data[[input$feature_to_vary]],
142
- y = calculate_timstuff(base_data, input$pitch)),
143
- color = "red", size = 3) +
144
- theme_minimal() +
145
- ylim(0,100) +
146
- labs(title = paste("Impact of", input$feature_to_vary, "on TimStuff"),
147
- x = input$feature_to_vary, y = "TimStuff") +
148
- theme(text = element_text(size = 14))
149
- })
150
  })
151
  }
152
 
153
- # Run the application
154
- shinyApp(ui = ui, server = server)
155
-
 
4
  library(xgboost)
5
  library(caret)
6
  library(shinythemes)
7
+ library(ggplot2)
8
+
9
  calculate_timstuff <- function(data, pitch_type) {
10
  data_matrix <- as.matrix(data)
11
  if (pitch_type == "Offspeed") {
12
  mod <- xgb.load('Off.model')
13
  prediction <- predict(mod, data_matrix)
14
  timstuff <- (50 - (prediction - -0.002239657) / 0.01043216 * 10)
15
+ } else if (pitch_type == "Breaking") {
16
+ mod <- xgb.load('Break.model')
17
+ prediction <- predict(mod, data_matrix)
18
+ timstuff <- (50 - (prediction - -0.004822031) / 0.007912765 * 10)
19
  } else {
20
  mod <- xgb.load('FB.model')
21
  prediction <- predict(mod, data_matrix)
 
23
  }
24
  return(timstuff)
25
  }
26
+
27
  ui <- fluidPage(
28
  theme = shinytheme("flatly"),
29
 
30
+ titlePanel("TimStuff Fastball/Offspeed/Breaking Calculator"),
 
31
 
 
32
  sidebarLayout(
33
  sidebarPanel(
34
  width = 4,
35
+ radioButtons("pitch", "Pitch Type", choices = c("Fastball", "Offspeed", "Breaking"), inline = TRUE),
36
  radioButtons("phand", "Hand", choices = c("R", "L"), inline = TRUE),
37
 
38
  numericInput("velo", "Velocity (mph)", value = 90, min = 70, max = 110, step = 1),
 
49
  "Horizontal Break" = "HB",
50
  "Spin Rate" = "spin_rate",
51
  "Extension" = "EAA",
52
+ "Spin Axis Difference" = "SADiff"))
 
 
53
  ),
54
 
 
55
  mainPanel(
56
  width = 8,
57
  wellPanel(
58
  h3("Results"),
59
  verbatimTextOutput("stuff"),
60
+ plotOutput("feature_impact", height = "400px")
61
  )
62
  )
63
  )
 
69
  data <- data.frame(
70
  start_speed = input$velo,
71
  IVB = input$ivb,
72
+ HB = input$hb,
73
  EAA = input$ext / 6.3,
74
  x0 = ifelse(input$phand == "L", -input$x0, input$x0),
75
  z0 = input$z0,
 
79
 
80
  data <- as.matrix(data)
81
 
82
+ TimStuff <- calculate_timstuff(data, input$pitch)
 
 
 
 
 
 
 
 
83
 
84
  paste("TimStuff:", round(TimStuff, 2), "\n20-80 Scale, 50 is Average, 1 SD is 10")
85
  })
86
 
87
+ feature_range <- reactive({
88
+ ranges <- list(
89
+ start_speed = c(70, 110),
90
+ IVB = c(-20, 30),
91
+ HB = c(-20, 20),
92
+ spin_rate = c(1000, 3500),
93
+ EAA = c(5/6.3, 8/6.3),
94
+ SADiff = c(-180, 180)
95
+ )
96
+ ranges[[input$feature_to_vary]]
97
+ })
 
 
 
 
98
 
99
+ output$feature_impact <- renderPlot({
100
+ base_data <- data.frame(
101
+ start_speed = input$velo,
102
+ IVB = input$ivb,
103
+ HB = input$hb,
104
+ EAA = input$ext / 6.3,
105
+ x0 = ifelse(input$phand == "L", -input$x0, input$x0),
106
+ z0 = input$z0,
107
+ spin_rate = input$spinrate,
108
+ SADiff = input$spinaxisdiff
109
+ )
110
+
111
+ feature_seq <- seq(feature_range()[1], feature_range()[2], length.out = 100)
112
+
113
+ varied_data <- do.call(rbind, replicate(100, base_data, simplify = FALSE))
114
+ varied_data[[input$feature_to_vary]] <- feature_seq
115
+ timstuff <- calculate_timstuff(varied_data, input$pitch)
116
+
117
+ plot_data <- data.frame(
118
+ Feature = names(base_data)[which(names(base_data) == input$feature_to_vary)],
119
+ Value = feature_seq,
120
+ TimStuff = timstuff
121
+ )
122
+
123
+ ggplot(plot_data, aes(x = Value, y = TimStuff)) +
124
+ geom_line(color = "blue", size = 1) +
125
+ geom_point(aes(x = base_data[[input$feature_to_vary]],
126
+ y = calculate_timstuff(base_data, input$pitch)),
127
+ color = "red", size = 3) +
128
+ theme_minimal() +
129
+ ylim(0,100) +
130
+ labs(title = paste("Impact of", input$feature_to_vary, "on TimStuff"),
131
+ x = input$feature_to_vary, y = "TimStuff") +
132
+ theme(text = element_text(size = 14))
133
+ })
 
 
 
 
 
 
134
  })
135
  }
136
 
137
+ shinyApp(ui = ui, server = server)