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 #
- #############################################################################################
- #############
- # 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 a swing
- swing_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' OR
- des = 'Swinging Strike' OR
- des = 'Swinging Pitchout' OR
- des = 'Missed Bunt' OR
- des = 'Swinging Strike (Blocked)')";
- # Set the pitches that were taken
- take_cases <- "(des = 'Called Strike' OR
- des = 'Ball' OR
- des = 'Ball in Dirt' OR
- des = 'Hit By Pitch')";
- # Open the channel
- channel <- odbcConnect(database);
- # Set the query for pitches swung at
- Swing_Query = paste("SELECT ax, ay, az, vx0, vy0, vz0, x0, y0, z0
- FROM pitches WHERE pitch_type = '",pitch_type,"' AND
- ",swing_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 swing data
- Swing <- sqlQuery(channel,Swing_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
- Sw_Pitch_Prb_PFX <- Pitch_Track_PFX(Swing,Ny,stand);
- Sw_Pitch_Prb_PFX_Drag <- Pitch_Track_PFX_Drag(Swing,Ny,stand);
- Sw_Pitch_Prb_W <- Pitch_Track_W(Swing,Ny,stand);
- Sw_Pitch_Prb_W_Drag <- Pitch_Track_W_Drag(Swing,Ny,stand);
- # Average the values for each array
- Np <- length(Swing$x0);
- # Plot the average probability by distance for swings
- 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,"): Swings on ",pitch_type," v. ",stand,"HB",sep=""));
- 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='');
- 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='');
- 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='');
- 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='');
- 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 taken
- Take_Query = paste("SELECT ax, ay, az, vx0, vy0, vz0, x0, y0, z0
- FROM pitches WHERE pitch_type = '",pitch_type,"' AND
- ",take_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 taken data
- Take <- sqlQuery(channel,Take_Query);
- # Close the channel
- close(channel);
- # Track the probability of the extrapolation being a strike
- Tk_Pitch_Prb_PFX <- Pitch_Track_PFX(Take,Ny,stand);
- Tk_Pitch_Prb_PFX_Drag <- Pitch_Track_PFX_Drag(Take,Ny,stand);
- Tk_Pitch_Prb_W <- Pitch_Track_W(Take,Ny,stand);
- Tk_Pitch_Prb_W_Drag <- Pitch_Track_W_Drag(Take,Ny,stand);
- windows();
- # Plot the average probability by distance for pitches taken
- 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=""));
- 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='');
- 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='');
- 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='');
- 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='');
- 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'));
- # Merge the two data sets
- All_Pitch_Prb_PFX <- rbind(Sw_Pitch_Prb_PFX,Tk_Pitch_Prb_PFX);
- All_Pitch_Prb_PFX_Drag <- rbind(Sw_Pitch_Prb_PFX_Drag,Tk_Pitch_Prb_PFX_Drag);
- All_Pitch_Prb_W <- rbind(Sw_Pitch_Prb_W,Tk_Pitch_Prb_W);
- All_Pitch_Prb_W_Drag <- rbind(Sw_Pitch_Prb_W_Drag,Tk_Pitch_Prb_W_Drag);
- 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),
- z0=c(Swing$z0,Take$z0));
- # Determine the indices related to balls and strikes based on probabilities
- Index <- ball_strike_prob(All,stand);
- # Set the ball and strike data
- St_Pitch_Prb_PFX <- All_Pitch_Prb_PFX[Index$S,];
- St_Pitch_Prb_PFX_Drag <- All_Pitch_Prb_PFX_Drag[Index$S,];
- St_Pitch_Prb_W <- All_Pitch_Prb_W[Index$S,];
- St_Pitch_Prb_W_Drag <- All_Pitch_Prb_W_Drag[Index$S,];
- Ba_Pitch_Prb_PFX <- All_Pitch_Prb_PFX[Index$B,];
- Ba_Pitch_Prb_PFX_Drag <- All_Pitch_Prb_PFX_Drag[Index$B,];
- Ba_Pitch_Prb_W <- All_Pitch_Prb_W[Index$B,];
- Ba_Pitch_Prb_W_Drag <- All_Pitch_Prb_W_Drag[Index$B,];
- windows();
- # Plot the average probability by distance for strikes
- 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=""));
- 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='');
- 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='');
- 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='');
- 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='');
- 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'));
- windows();
- # Plot the average probability by distance for balls
- 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=""));
- 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='');
- 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='');
- 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='');
- 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='');
- 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