Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #######################################################################################################
- # Compare the projected strike zone probabilities for each of the four versions of movement on swings #
- #######################################################################################################
- #############
- # LIBRARIES #
- #############
- library(RODBC);
- ####################
- # ADDITIONAL FILES #
- ####################
- source("Movement Scripts/Mvt_Fcns.r")
- ########
- # MAIN #
- ########
- # Player's name
- first <- 'Julio';
- last <- 'Urias';
- # Year
- year <- '2016';
- # Pitch type
- pitch_type <- 'CU';
- # Batters' handedness
- stand <- 'R';
- # Set up the database name
- database <- paste("MLB",year,sep="");
- # Set the pitches that resulted in contact
- contact_cases <- "(des = 'In play, no out' OR
- des = 'In play, out(s)' OR
- des = 'In play, run(s)' OR
- des = 'Foul' OR
- des = 'Foul (Runner Going)' OR
- des = 'Foul Bunt' OR
- des = 'Foul Tip')";
- # Set the pitches that resulted in a swing and miss
- miss_cases <- "(des = 'Swinging Strike' OR
- des = 'Swinging Pitchout' OR
- des = 'Missed Bunt' OR
- des = 'Swinging Strike (Blocked)')"
- # Open the channel
- channel <- odbcConnect(database);
- # Set the query for pitches that resulted in contact
- Contact_Query = paste("SELECT ax, ay, az, vx0, vy0, vz0, x0, y0, z0
- FROM pitches WHERE pitch_type = '",pitch_type,"' AND
- ",contact_cases," AND ab_id IN
- (SELECT ab_id FROM atbats WHERE
- pitcher = (SELECT eliasid FROM players WHERE first = '",
- first,"' AND last = '",last,"') AND stand = '",stand,"')",sep="");
- # Download the contact data
- Contact <- sqlQuery(channel,Contact_Query);
- # Close the channel
- close(channel);
- # Set the number of points in y at which to sample
- Ny <- 100;
- # Track the probability of the extrapolation being a strike
- Ct_Pitch_Prb_PFX <- Pitch_Track_PFX(Contact,Ny,stand);
- Ct_Pitch_Prb_PFX_Drag <- Pitch_Track_PFX_Drag(Contact,Ny,stand);
- Ct_Pitch_Prb_W <- Pitch_Track_W(Contact,Ny,stand);
- Ct_Pitch_Prb_W_Drag <- Pitch_Track_W_Drag(Contact,Ny,stand);
- # Average the values for each array
- Np <- length(Contact$x0);
- # Plot the average probability by distance for contact
- y_inc <- seq(17/12,55,length=Ny);
- 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=""));
- 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='');
- 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='');
- 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='');
- 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='');
- 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'));
- # Open the channel
- channel <- odbcConnect(database);
- # Set the query for pitches missed
- Miss_Query = paste("SELECT ax, ay, az, vx0, vy0, vz0, x0, y0, z0
- FROM pitches WHERE pitch_type = '",pitch_type,"' AND
- ",miss_cases," AND ab_id IN
- (SELECT ab_id FROM atbats WHERE
- pitcher = (SELECT eliasid FROM players WHERE first = '",
- first,"' AND last = '",last,"') AND stand = '",stand,"')",sep="");
- # Download the pitches miss data
- Miss <- sqlQuery(channel,Miss_Query);
- # Close the channel
- close(channel);
- # Track the probability of the extrapolation being a strike
- Ms_Pitch_Prb_PFX <- Pitch_Track_PFX(Miss,Ny,stand);
- Ms_Pitch_Prb_PFX_Drag <- Pitch_Track_PFX_Drag(Miss,Ny,stand);
- Ms_Pitch_Prb_W <- Pitch_Track_W(Miss,Ny,stand);
- Ms_Pitch_Prb_W_Drag <- Pitch_Track_W_Drag(Miss,Ny,stand);
- windows();
- # Plot the average probability by distance for misses
- 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=""));
- 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='');
- 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='');
- 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='');
- 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='');
- 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