TimStats commited on
Commit
7300318
·
verified ·
1 Parent(s): c8d82e0

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +79 -95
app.R CHANGED
@@ -6,50 +6,41 @@ 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)
22
- timstuff <- (50 - (prediction - -0.0011801) / 0.007989927 * 10)
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 = 95, min = 70, max = 110, step = 1),
39
- numericInput("ivb", "Induced Vertical Break (in)", value = 16, min = -20, max = 30, step = 0.1),
40
- numericInput("hb", "Horizontal Break +Arm/-Glove (in)", value = 6, min = -20, max = 20, step = 0.1),
41
  numericInput("spinrate", "Spin Rate (rpm)", value = 2300, min = 1000, max = 3500, step = 10),
42
  numericInput("ext", "Extension (ft)", value = 6.3, min = 5, max = 8, step = 0.1),
43
  numericInput("spinaxisdiff", "Spin Axis Difference (°)", value = 0, min = -180, max = 180, step = 1),
44
- numericInput("x0", "Horizontal Release Point (ft)", value = -1.5, min = -5, max = 5, step = 0.1),
45
  numericInput("z0", "Vertical Release Point (ft)", value = 5, min = 3, max = 8, step = 0.1),
 
 
 
46
  selectInput("feature_to_vary", "Select Feature to Vary:",
47
- choices = c("Velocity" = "start_speed",
48
- "Induced Vertical Break" = "IVB",
49
- "Horizontal Break" = "HB",
50
- "Spin Rate" = "spin_rate",
51
- "Extension" = "EAA",
52
- "Spin Axis Difference" = "SADiff"))
53
  ),
54
 
55
  mainPanel(
@@ -64,73 +55,66 @@ ui <- fluidPage(
64
  )
65
 
66
  server <- function(input, output) {
67
- observeEvent(input$pitch, {
68
- output$stuff <- renderText({
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,
76
- spin_rate = input$spinrate,
77
- SADiff = input$spinaxisdiff
78
- )
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
 
 
6
  library(shinythemes)
7
  library(ggplot2)
8
 
9
+ calculate_timstuff <- function(data) {
10
+ data_matrix <- as.matrix(data)
11
+ mod <- xgb.load('TimStuff2.model')
12
+ prediction <- predict(mod, data_matrix)
13
+ timstuff <- (100 - (prediction - -0.00249975) / 0.007566558 * 10)
14
+ return(timstuff)
15
+ }
 
 
 
 
 
 
 
 
 
 
16
 
17
  ui <- fluidPage(
18
  theme = shinytheme("flatly"),
19
 
20
+ titlePanel("TimStuff+ Calculator"),
21
 
22
  sidebarLayout(
23
  sidebarPanel(
24
  width = 4,
 
25
  radioButtons("phand", "Hand", choices = c("R", "L"), inline = TRUE),
 
26
  numericInput("velo", "Velocity (mph)", value = 95, min = 70, max = 110, step = 1),
27
+ numericInput("ivb", "Induced Vertical Break (in)", value = 16, min = -30, max = 30, step = 0.1),
28
+ numericInput("hb", "Horizontal Break (in.) (Pitcher's Perspective)", value = 6, min = -25, max = 25, step = 0.1),
29
  numericInput("spinrate", "Spin Rate (rpm)", value = 2300, min = 1000, max = 3500, step = 10),
30
  numericInput("ext", "Extension (ft)", value = 6.3, min = 5, max = 8, step = 0.1),
31
  numericInput("spinaxisdiff", "Spin Axis Difference (°)", value = 0, min = -180, max = 180, step = 1),
32
+ numericInput("x0", "Horizontal Release Point (ft) (Catcher's Perspective)", value = -1.5, min = -5, max = 5, step = 0.1),
33
  numericInput("z0", "Vertical Release Point (ft)", value = 5, min = 3, max = 8, step = 0.1),
34
+ numericInput("primvelo", "Average Velocity of Primary Pitch", value = 95, min = 70, max = 110, step = 1),
35
+ numericInput("primIVB", "Average IVB of Primary Pitch", value = 16, min = -30, max = 30, step = 0.1),
36
+ numericInput("primHB", "Average HB of Primary Pitch", value = 6, min = -25, max = 25, step = 0.1),
37
  selectInput("feature_to_vary", "Select Feature to Vary:",
38
+ choices = c("Velocity" = "start_speed",
39
+ "Induced Vertical Break" = "IVB",
40
+ "Horizontal Break" = "HB",
41
+ "Spin Rate" = "spin_rate",
42
+ "Extension" = "EAA",
43
+ "Spin Axis Difference" = "SADiff"))
44
  ),
45
 
46
  mainPanel(
 
55
  )
56
 
57
  server <- function(input, output) {
58
+ reactive_data <- reactive({
59
+ data.frame(
60
+ ishandL = ifelse(input$phand == "L", 1, 0),
61
+ start_speed = input$velo,
62
+ IVB = input$ivb,
63
+ HB = input$hb,
64
+ EAA = input$ext / 6.3,
65
+ x0 = input$x0,
66
+ z0 = input$z0,
67
+ spin_rate = input$spinrate,
68
+ SADiff = input$spinaxisdiff,
69
+ primary_speed = input$primvelo,
70
+ primary_IVB = input$primIVB,
71
+ primary_HB = input$primHB
72
+ )
73
+ })
74
+
75
+ output$stuff <- renderText({
76
+ data <- reactive_data()
77
+ TimStuff <- calculate_timstuff(data)
78
+ paste("TimStuff+:", round(TimStuff, 2), "\nTimStuff+ scale is 100 is Average, 1 SD is 10")
79
+ })
80
+
81
+ feature_range <- reactive({
82
+ ranges <- list(
83
+ start_speed = c(70, 110),
84
+ IVB = c(-20, 30),
85
+ HB = c(-20, 20),
86
+ spin_rate = c(1000, 3500),
87
+ EAA = c(5/6.3, 8/6.3),
88
+ SADiff = c(-180, 180)
89
+ )
90
+ ranges[[input$feature_to_vary]]
91
+ })
92
+
93
+ output$feature_impact <- renderPlot({
94
+ base_data <- reactive_data()
95
 
96
+ feature_seq <- seq(feature_range()[1], feature_range()[2], length.out = 100)
97
+
98
+ varied_data <- do.call(rbind, replicate(100, base_data, simplify = FALSE))
99
+ varied_data[[input$feature_to_vary]] <- feature_seq
100
+ timstuff <- calculate_timstuff(varied_data)
101
+
102
+ plot_data <- data.frame(
103
+ Feature = names(base_data)[which(names(base_data) == input$feature_to_vary)],
104
+ Value = feature_seq,
105
+ TimStuff = timstuff
106
+ )
107
+
108
+ ggplot(plot_data, aes(x = Value, y = TimStuff)) +
109
+ geom_line(color = "blue", size = 1) +
110
+ geom_point(aes(x = base_data[[input$feature_to_vary]],
111
+ y = calculate_timstuff(base_data)),
112
+ color = "red", size = 3) +
113
+ theme_minimal() +
114
+ ylim(50,150) +
115
+ labs(title = paste("Impact of", input$feature_to_vary, "on TimStuff"),
116
+ x = input$feature_to_vary, y = "TimStuff") +
117
+ theme(text = element_text(size = 14))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  })
119
  }
120