Advertisement
matthewrmata

Projected Strike Probability

Jan 25th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 8.04 KB | None | 0 0
  1. #############################################################################################
  2. # Compare the projected strike zone probabilities for each of the four versions of movement #
  3. #############################################################################################
  4.  
  5. #############
  6. # LIBRARIES #
  7. #############
  8.  
  9. library(RODBC);
  10.  
  11. ####################
  12. # ADDITIONAL FILES #
  13. ####################
  14.  
  15. source("Movement Scripts/Mvt_Fcns.r")
  16.  
  17. ########
  18. # MAIN #
  19. ########
  20.  
  21. # Player's name
  22. first <- 'Julio';
  23. last <- 'Urias';
  24.  
  25. # Year
  26. year <- '2016';
  27.  
  28. # Pitch type
  29. pitch_type <- 'CU';
  30.  
  31. # Batters' handedness
  32. stand <- 'R';
  33.  
  34. # Set up the database name
  35. database <- paste("MLB",year,sep="");
  36.  
  37. # Set the pitches that resulted in a swing
  38. swing_cases <- "(des = 'In play, no out' OR
  39.                  des = 'In play, out(s)' OR
  40.                  des = 'In play, run(s)' OR
  41.                  des = 'Foul' OR
  42.                  des = 'Foul (Runner Going)' OR
  43.                  des = 'Foul Bunt' OR
  44.                  des = 'Foul Tip' OR
  45.                  des = 'Swinging Strike' OR
  46.                  des = 'Swinging Pitchout' OR
  47.                  des = 'Missed Bunt' OR
  48.                  des = 'Swinging Strike (Blocked)')";
  49.  
  50. # Set the pitches that were taken
  51. take_cases <- "(des = 'Called Strike' OR
  52.                 des = 'Ball' OR
  53.                 des = 'Ball in Dirt' OR
  54.                 des = 'Hit By Pitch')";
  55.  
  56. # Open the channel
  57. channel <- odbcConnect(database);
  58.                
  59. # Set the query for pitches swung at
  60. Swing_Query = paste("SELECT ax, ay, az, vx0, vy0, vz0, x0, y0, z0  
  61.                    FROM pitches WHERE pitch_type = '",pitch_type,"' AND
  62.                     ",swing_cases," AND ab_id IN
  63.                     (SELECT ab_id FROM atbats WHERE
  64.                     pitcher = (SELECT eliasid FROM players WHERE first = '",
  65.                     first,"' AND last = '",last,"') AND stand = '",stand,"')",sep="");
  66.                    
  67. # Download the swing data
  68. Swing <- sqlQuery(channel,Swing_Query);
  69.  
  70. # Close the channel
  71. close(channel);
  72.  
  73. # Set the number of points in y at which to sample
  74. Ny <- 100;
  75.  
  76. # Track the probability of the extrapolation being a strike
  77. Sw_Pitch_Prb_PFX <- Pitch_Track_PFX(Swing,Ny,stand);
  78. Sw_Pitch_Prb_PFX_Drag <- Pitch_Track_PFX_Drag(Swing,Ny,stand);
  79. Sw_Pitch_Prb_W <- Pitch_Track_W(Swing,Ny,stand);
  80. Sw_Pitch_Prb_W_Drag <- Pitch_Track_W_Drag(Swing,Ny,stand);
  81.  
  82. # Average the values for each array
  83. Np <- length(Swing$x0);
  84.  
  85. # Plot the average probability by distance for swings
  86. y_inc <- seq(17/12,55,length=Ny);
  87. plot(0,0,,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xlab="Distance from Home Plate (feet)", ylab="Called Strike Probability", main=paste(first," ",last," (",year,"): Swings on ",pitch_type," v. ",stand,"HB",sep=""));
  88. lines(y_inc,colMeans(Sw_Pitch_Prb_PFX),col="red",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  89. lines(y_inc,colMeans(Sw_Pitch_Prb_PFX_Drag),col="blue",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  90. lines(y_inc,colMeans(Sw_Pitch_Prb_W),col="green",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  91. lines(y_inc,colMeans(Sw_Pitch_Prb_W_Drag),col="black",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  92. legend(2,0.205,c('PFX','PFX D','W','W D'),lty=c(1,1,1,1),lwd=c(1,1,1,1),col=c('red','blue','green','black'));
  93.  
  94. # Open the channel
  95. channel <- odbcConnect(database);
  96.                
  97. # Set the query for pitches taken
  98. Take_Query = paste("SELECT ax, ay, az, vx0, vy0, vz0, x0, y0, z0  
  99.                   FROM pitches WHERE pitch_type = '",pitch_type,"' AND
  100.                    ",take_cases," AND ab_id IN
  101.                    (SELECT ab_id FROM atbats WHERE
  102.                    pitcher = (SELECT eliasid FROM players WHERE first = '",
  103.                    first,"' AND last = '",last,"') AND stand = '",stand,"')",sep="");
  104.                    
  105. # Download the pitches taken data
  106. Take <- sqlQuery(channel,Take_Query);
  107.  
  108. # Close the channel
  109. close(channel);
  110.  
  111. # Track the probability of the extrapolation being a strike
  112. Tk_Pitch_Prb_PFX <- Pitch_Track_PFX(Take,Ny,stand);
  113. Tk_Pitch_Prb_PFX_Drag <- Pitch_Track_PFX_Drag(Take,Ny,stand);
  114. Tk_Pitch_Prb_W <- Pitch_Track_W(Take,Ny,stand);
  115. Tk_Pitch_Prb_W_Drag <- Pitch_Track_W_Drag(Take,Ny,stand);
  116.  
  117. windows();
  118.  
  119. # Plot the average probability by distance for pitches taken
  120. plot(0,0,,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xlab="Distance from Home Plate (feet)", ylab="Called Strike Probability", main=paste(first," ",last," (",year,"): Takes on ",pitch_type," v. ",stand,"HB",sep=""));
  121. lines(y_inc,colMeans(Tk_Pitch_Prb_PFX),col="red",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  122. lines(y_inc,colMeans(Tk_Pitch_Prb_PFX_Drag),col="blue",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  123. lines(y_inc,colMeans(Tk_Pitch_Prb_W),col="green",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  124. lines(y_inc,colMeans(Tk_Pitch_Prb_W_Drag),col="black",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  125. legend(2,0.205,c('PFX','PFX D','W','W D'),lty=c(1,1,1,1),lwd=c(1,1,1,1),col=c('red','blue','green','black'));
  126.  
  127. # Merge the two data sets
  128. All_Pitch_Prb_PFX <- rbind(Sw_Pitch_Prb_PFX,Tk_Pitch_Prb_PFX);
  129. All_Pitch_Prb_PFX_Drag <- rbind(Sw_Pitch_Prb_PFX_Drag,Tk_Pitch_Prb_PFX_Drag);
  130. All_Pitch_Prb_W <- rbind(Sw_Pitch_Prb_W,Tk_Pitch_Prb_W);
  131. All_Pitch_Prb_W_Drag <- rbind(Sw_Pitch_Prb_W_Drag,Tk_Pitch_Prb_W_Drag);
  132. All <- list(ax=c(Swing$ax,Take$ax), ay=c(Swing$ay,Take$ay), az=c(Swing$az,Take$az), vx0=c(Swing$vx0,Take$vx0), vy0=c(Swing$vy0,Take$vy0), vz0=c(Swing$vz0,Take$vz0), x0=c(Swing$x0,Take$x0), y0=c(Swing$y0,Take$y0),
  133.             z0=c(Swing$z0,Take$z0));
  134.  
  135. # Determine the indices related to balls and strikes based on probabilities
  136. Index <- ball_strike_prob(All,stand);
  137.  
  138. # Set the ball and strike data
  139. St_Pitch_Prb_PFX <- All_Pitch_Prb_PFX[Index$S,];
  140. St_Pitch_Prb_PFX_Drag <- All_Pitch_Prb_PFX_Drag[Index$S,];
  141. St_Pitch_Prb_W <- All_Pitch_Prb_W[Index$S,];
  142. St_Pitch_Prb_W_Drag <- All_Pitch_Prb_W_Drag[Index$S,];
  143. Ba_Pitch_Prb_PFX <- All_Pitch_Prb_PFX[Index$B,];
  144. Ba_Pitch_Prb_PFX_Drag <- All_Pitch_Prb_PFX_Drag[Index$B,];
  145. Ba_Pitch_Prb_W <- All_Pitch_Prb_W[Index$B,];
  146. Ba_Pitch_Prb_W_Drag <- All_Pitch_Prb_W_Drag[Index$B,];
  147.  
  148. windows();
  149.  
  150. # Plot the average probability by distance for strikes
  151. plot(0,0,,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xlab="Distance from Home Plate (feet)", ylab="Called Strike Probability", main=paste(first," ",last," (",year,"): Strikes on ",pitch_type," v. ",stand,"HB",sep=""));
  152. lines(y_inc,colMeans(St_Pitch_Prb_PFX),col="red",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  153. lines(y_inc,colMeans(St_Pitch_Prb_PFX_Drag),col="blue",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  154. lines(y_inc,colMeans(St_Pitch_Prb_W),col="green",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  155. lines(y_inc,colMeans(St_Pitch_Prb_W_Drag),col="black",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  156. legend(2,0.205,c('PFX','PFX D','W','W D'),lty=c(1,1,1,1),lwd=c(1,1,1,1),col=c('red','blue','green','black'));
  157.  
  158. windows();
  159.  
  160. # Plot the average probability by distance for balls
  161. plot(0,0,,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xlab="Distance from Home Plate (feet)", ylab="Called Strike Probability", main=paste(first," ",last," (",year,"): Balls on ",pitch_type," v. ",stand,"HB",sep=""));
  162. lines(y_inc,colMeans(Ba_Pitch_Prb_PFX),col="red",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  163. lines(y_inc,colMeans(Ba_Pitch_Prb_PFX_Drag),col="blue",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  164. lines(y_inc,colMeans(Ba_Pitch_Prb_W),col="green",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  165. lines(y_inc,colMeans(Ba_Pitch_Prb_W_Drag),col="black",pch=20,xlim=c(17/12,55),ylim=c(0,1),xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='');
  166. legend(2,0.985,c('PFX','PFX D','W','W D'),lty=c(1,1,1,1),lwd=c(1,1,1,1),col=c('red','blue','green','black'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement