Advertisement
matthewrmata

Projected Strike Probability on Swings

Jan 25th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 5.03 KB | None | 0 0
  1. #######################################################################################################
  2. # Compare the projected strike zone probabilities for each of the four versions of movement on swings #
  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 contact
  38. contact_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')";
  45.  
  46. # Set the pitches that resulted in a swing and miss
  47. miss_cases <- "(des = 'Swinging Strike' OR
  48.                 des = 'Swinging Pitchout' OR
  49.                 des = 'Missed Bunt' OR
  50.                 des = 'Swinging Strike (Blocked)')"
  51.  
  52. # Open the channel
  53. channel <- odbcConnect(database);
  54.                
  55. # Set the query for pitches that resulted in contact
  56. Contact_Query = paste("SELECT ax, ay, az, vx0, vy0, vz0, x0, y0, z0  
  57.                      FROM pitches WHERE pitch_type = '",pitch_type,"' AND
  58.                       ",contact_cases," AND ab_id IN
  59.                       (SELECT ab_id FROM atbats WHERE
  60.                       pitcher = (SELECT eliasid FROM players WHERE first = '",
  61.                       first,"' AND last = '",last,"') AND stand = '",stand,"')",sep="");
  62.                    
  63. # Download the contact data
  64. Contact <- sqlQuery(channel,Contact_Query);
  65.  
  66. # Close the channel
  67. close(channel);
  68.  
  69. # Set the number of points in y at which to sample
  70. Ny <- 100;
  71.  
  72. # Track the probability of the extrapolation being a strike
  73. Ct_Pitch_Prb_PFX <- Pitch_Track_PFX(Contact,Ny,stand);
  74. Ct_Pitch_Prb_PFX_Drag <- Pitch_Track_PFX_Drag(Contact,Ny,stand);
  75. Ct_Pitch_Prb_W <- Pitch_Track_W(Contact,Ny,stand);
  76. Ct_Pitch_Prb_W_Drag <- Pitch_Track_W_Drag(Contact,Ny,stand);
  77.  
  78. # Average the values for each array
  79. Np <- length(Contact$x0);
  80.  
  81. # Plot the average probability by distance for contact
  82. y_inc <- seq(17/12,55,length=Ny);
  83. 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,"): Contact on ",pitch_type," v. ",stand,"HB",sep=""));
  84. lines(y_inc,colMeans(Ct_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='');
  85. lines(y_inc,colMeans(Ct_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='');
  86. lines(y_inc,colMeans(Ct_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='');
  87. lines(y_inc,colMeans(Ct_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='');
  88. 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'));
  89.  
  90. # Open the channel
  91. channel <- odbcConnect(database);
  92.                
  93. # Set the query for pitches missed
  94. Miss_Query = paste("SELECT ax, ay, az, vx0, vy0, vz0, x0, y0, z0  
  95.                   FROM pitches WHERE pitch_type = '",pitch_type,"' AND
  96.                    ",miss_cases," AND ab_id IN
  97.                    (SELECT ab_id FROM atbats WHERE
  98.                    pitcher = (SELECT eliasid FROM players WHERE first = '",
  99.                    first,"' AND last = '",last,"') AND stand = '",stand,"')",sep="");
  100.                    
  101. # Download the pitches miss data
  102. Miss <- sqlQuery(channel,Miss_Query);
  103.  
  104. # Close the channel
  105. close(channel);
  106.  
  107. # Track the probability of the extrapolation being a strike
  108. Ms_Pitch_Prb_PFX <- Pitch_Track_PFX(Miss,Ny,stand);
  109. Ms_Pitch_Prb_PFX_Drag <- Pitch_Track_PFX_Drag(Miss,Ny,stand);
  110. Ms_Pitch_Prb_W <- Pitch_Track_W(Miss,Ny,stand);
  111. Ms_Pitch_Prb_W_Drag <- Pitch_Track_W_Drag(Miss,Ny,stand);
  112.  
  113. windows();
  114.  
  115. # Plot the average probability by distance for misses
  116. 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,"): Misses on ",pitch_type," v. ",stand,"HB",sep=""));
  117. lines(y_inc,colMeans(Ms_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='');
  118. lines(y_inc,colMeans(Ms_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='');
  119. lines(y_inc,colMeans(Ms_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='');
  120. lines(y_inc,colMeans(Ms_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='');
  121. 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'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement