Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##################################
- # Late Movement For Each Version #
- ##################################
- #############
- # LIBRARIES #
- #############
- library(RODBC);
- #########
- # FILES #
- #########
- source("Movement Scripts/Mvt_Fcns.r");
- ########
- # MAIN #
- ########
- # Set the pitch type
- Pitch_Type <- 'SI';
- # Set the handedness of the pitchers
- P_Throws <- 'R';
- # Set the number of pitchers based on most of that pitch type thrown
- N_Pitchers <- 25;
- # Year
- Year <- '2016';
- # Set up the database name
- database <- paste("MLB",Year,sep="");
- # Open the channel
- channel <- odbcConnect(database);
- # Set the query to find the pitchers throwing the most type of the given pitch type
- Name_Query <- paste("SELECT players.first AS 'First', players.last AS 'Last', players.eliasid as 'eliasid', count(pitches.pitch_type) AS 'Number of Pitches'
- FROM players JOIN atbats ON players.eliasid = atbats.pitcher JOIN pitches ON atbats.ab_id = pitches.ab_id
- WHERE pitches.pitch_type = '",Pitch_Type,"' AND atbats.p_throws = '",P_Throws,"'
- GROUP BY players.eliasid
- ORDER BY count(pitches.pitch_type) DESC
- LIMIT ",N_Pitchers,"",sep="");
- # Download the data from the query
- Names <- sqlQuery(channel,Name_Query);
- # Close the channel
- close(channel);
- # Set up arrays for late movement
- PFX_mvt <- array(0,dim=N_Pitchers);
- PFX_t_rem <- array(0,dim=N_Pitchers);
- PFX_arc_rem <- array(0,dim=N_Pitchers);
- PFX_pct <- array(0,dim=N_Pitchers);
- PFX_num <- array(0,dim=N_Pitchers);
- PFX_Drag_mvt <- array(0,dim=N_Pitchers);
- PFX_Drag_t_rem <- array(0,dim=N_Pitchers);
- PFX_Drag_arc_rem <- array(0,dim=N_Pitchers);
- PFX_Drag_pct <- array(0,dim=N_Pitchers);
- PFX_Drag_num <- array(0,dim=N_Pitchers);
- W_mvt <- array(0,dim=N_Pitchers);
- W_t_rem <- array(0,dim=N_Pitchers);
- W_arc_rem <- array(0,dim=N_Pitchers);
- W_pct <- array(0,dim=N_Pitchers);
- W_num <- array(0,dim=N_Pitchers);
- W_Drag_mvt <- array(0,dim=N_Pitchers);
- W_Drag_t_rem <- array(0,dim=N_Pitchers);
- W_Drag_arc_rem <- array(0,dim=N_Pitchers);
- W_Drag_pct <- array(0,dim=N_Pitchers);
- W_Drag_num <- array(0,dim=N_Pitchers);
- # Loop over the pitchers to calculate the late movement
- for (i in 1:N_Pitchers){
- # Open the channel
- channel <- odbcConnect(database);
- # Set the query to find the pitch data for each pitcher
- Pitch_Query <- paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az FROM pitches WHERE
- ab_id IN (SELECT ab_id FROM atbats WHERE
- pitcher = ",Names$eliasid[i],") AND pitch_type = '",Pitch_Type,"'
- AND px IS NOT NULL",sep="");
- # Download the individual pitch data
- PFX_Data <- sqlQuery(channel,Pitch_Query);
- # Close the channel
- close(channel);
- # Set the number of pitches
- N <- length(PFX_Data$x0);
- # Set arrays for temporary storage
- PFX_loc <- array(0,dim=N);
- Time_PFX <- array(0,dim=N);
- Arc_PFX <- array(0,dim=N);
- PFX_Drag_loc <- array(0,dim=N);
- Time_PFX_Drag <- array(0,dim=N);
- Arc_PFX_Drag <- array(0,dim=N);
- W_loc <- array(0,dim=N);
- Time_W <- array(0,dim=N);
- Arc_W <- array(0,dim=N);
- W_Drag_loc <- array(0,dim=N);
- Time_W_Drag <- array(0,dim=N);
- Arc_W_Drag <- array(0,dim=N);
- # Loop over and process each pitch
- for (j in 1:N){
- PITCH <- list(ax = PFX_Data$ax[j], ay = PFX_Data$ay[j], az = PFX_Data$az[j], vx0 = PFX_Data$vx0[j], vy0 = PFX_Data$vy0[j], vz0 = PFX_Data$vz0[j], x0 = PFX_Data$x0[j], y0 = PFX_Data$y0[j], z0 = PFX_Data$z0[j]);
- # Run the late movement algorithm for each of the four versions of movement
- LB_PFX <- Late_Break_PFX(PITCH);
- LB_PFX_Drag <- Late_Break_PFX_Drag(PITCH);
- LB_W <- Late_Break_W(PITCH);
- LB_W_Drag <- Late_Break_W_Drag(PITCH);
- # Populate the arrays
- PFX_loc[j] <- LB_PFX$pfx_loc;
- Time_PFX[j] <- LB_PFX$t_rem;
- Arc_PFX[j] <- LB_PFX$arc_rem;
- PFX_Drag_loc[j] <- LB_PFX_Drag$pfx_drag_loc;
- Time_PFX_Drag[j] <- LB_PFX_Drag$t_rem;
- Arc_PFX_Drag[j] <- LB_PFX_Drag$arc_rem;
- W_loc[j] <- LB_W$w_loc;
- Time_W[j] <- LB_W$t_rem;
- Arc_W[j] <- LB_W$arc_rem;
- W_Drag_loc[j] <- LB_W_Drag$w_drag_loc;
- Time_W_Drag[j] <- LB_W_Drag$t_rem;
- Arc_W_Drag[j] <- LB_W_Drag$arc_rem;
- }
- # Restrict the arrays to non-zero values
- Time_PFX <- Time_PFX[Time_PFX > 0];
- Arc_PFX <- Arc_PFX[Arc_PFX > 0];
- Time_PFX_Drag <- Time_PFX_Drag[Time_PFX_Drag > 0];
- Arc_PFX_Drag <- Arc_PFX_Drag[Arc_PFX_Drag > 0];
- Time_W <- Time_W[Time_W > 0];
- Arc_W <- Arc_W[Arc_W > 0];
- Time_W_Drag <- Time_W_Drag[Time_W_Drag > 0];
- Arc_W_Drag <- Arc_W_Drag[Arc_W_Drag > 0];
- # Find the length of each new array
- N_pfx <- length(Time_PFX);
- N_pfx_d <- length(Time_PFX_Drag);
- N_w <- length(Time_W);
- N_w_d <- length(Time_W_Drag);
- # Assign average values by pitcher
- PFX_mvt[i] <- round(mean(PFX_loc),digits=2);
- PFX_t_rem[i] <- round(mean(Time_PFX),digits=3);
- PFX_arc_rem[i] <- round(mean(Arc_PFX),digits=2);
- PFX_pct[i] <- round(N_pfx/N,digits=2);
- PFX_num[i] <- N_pfx;
- PFX_Drag_mvt[i] <- round(mean(PFX_Drag_loc),digits=2);
- PFX_Drag_t_rem[i] <- round(mean(Time_PFX_Drag),digits=3);
- PFX_Drag_arc_rem[i] <- round(mean(Arc_PFX_Drag),digits=2);
- PFX_Drag_pct[i] <- round(N_pfx_d/N,digits=2);
- PFX_Drag_num[i] <- N_pfx_d;
- W_mvt[i] <- round(mean(W_loc),digits=2);
- W_t_rem[i] <- round(mean(Time_W),digits=3);
- W_arc_rem[i] <- round(mean(Arc_W),digits=2);
- W_pct[i] <- round(N_w/N,digits=2);
- W_num[i] <- N_w;
- W_Drag_mvt[i] <- round(mean(W_Drag_loc),digits=2);
- W_Drag_t_rem[i] <- round(mean(Time_W_Drag),digits=3);
- W_Drag_arc_rem[i] <- round(mean(Arc_W_Drag),digits=2);
- W_Drag_pct[i] <- round(N_w_d/N,digits=2);
- W_Drag_num[i] <- N_w_d;
- }
- # Set arrays for the pitcher names
- First <- Names$First;
- Last <- Names$Last;
- # Set up a data frame for All
- df_late_break_All <- data.frame(First,Last,PFX_mvt,PFX_t_rem,PFX_arc_rem,PFX_pct,PFX_num,PFX_Drag_mvt,PFX_Drag_t_rem,PFX_Drag_arc_rem,PFX_Drag_pct,PFX_Drag_num,W_mvt,W_t_rem,W_arc_rem,W_pct,W_num,
- W_Drag_mvt,W_Drag_t_rem,W_Drag_arc_rem,W_Drag_pct,W_Drag_num);
- # Write to file
- filename_All <- paste("Late_Mvt_All_",Pitch_Type,"_",P_Throws,"_",N_Pitchers,"_",Year,".csv",sep="");
- write.csv(df_late_break_All, file = filename_All);
- # Set up a data frame for PFX
- df_late_break_PFX <- data.frame(First,Last,PFX_mvt,PFX_t_rem,PFX_arc_rem,PFX_pct,PFX_num);
- # Write to file
- filename_PFX <- paste("Late_Mvt_PFX_",Pitch_Type,"_",P_Throws,"_",N_Pitchers,"_",Year,".csv",sep="");
- write.csv(df_late_break_PFX, file = filename_PFX);
- # Set up a data frame for PFX D
- df_late_break_PFX_D <- data.frame(First,Last,PFX_Drag_mvt,PFX_Drag_t_rem,PFX_Drag_arc_rem,PFX_Drag_pct,PFX_Drag_num);
- # Write to file
- filename_PFX_D <- paste("Late_Mvt_PFX_D_",Pitch_Type,"_",P_Throws,"_",N_Pitchers,"_",Year,".csv",sep="");
- write.csv(df_late_break_PFX_D, file = filename_PFX_D);
- # Set up a data frame for W
- df_late_break_W <- data.frame(First,Last,W_mvt,W_t_rem,W_arc_rem,W_pct,W_num);
- # Write to file
- filename_W <- paste("Late_Mvt_W_",Pitch_Type,"_",P_Throws,"_",N_Pitchers,"_",Year,".csv",sep="");
- write.csv(df_late_break_W, file = filename_W);
- # Set up a data from for W D
- df_late_break_W_D <- data.frame(First,Last,W_Drag_mvt,W_Drag_t_rem,W_Drag_arc_rem,W_Drag_pct,W_Drag_num);
- # Write to file
- filename_W_D <- paste("Late_Mvt_W_D_",Pitch_Type,"_",P_Throws,"_",N_Pitchers,"_",Year,".csv",sep="");
- write.csv(df_late_break_W_D, file = filename_W_D);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement