Advertisement
matthewrmata

Late Movement

Jan 25th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 7.27 KB | None | 0 0
  1. ##################################
  2. # Late Movement For Each Version #
  3. ##################################
  4.  
  5. #############
  6. # LIBRARIES #
  7. #############
  8.  
  9. library(RODBC);
  10.  
  11. #########
  12. # FILES #
  13. #########
  14.  
  15. source("Movement Scripts/Mvt_Fcns.r");
  16.  
  17. ########
  18. # MAIN #
  19. ########
  20.  
  21. # Set the pitch type
  22. Pitch_Type <- 'SI';
  23.  
  24. # Set the handedness of the pitchers
  25. P_Throws <- 'R';
  26.  
  27. # Set the number of pitchers based on most of that pitch type thrown
  28. N_Pitchers <- 25;
  29.  
  30. # Year
  31. Year <- '2016';
  32.  
  33. # Set up the database name
  34. database <- paste("MLB",Year,sep="");
  35.  
  36. # Open the channel
  37. channel <- odbcConnect(database);
  38.  
  39. # Set the query to find the pitchers throwing the most type of the given pitch type
  40. Name_Query <- paste("SELECT players.first AS 'First', players.last AS 'Last', players.eliasid as 'eliasid', count(pitches.pitch_type) AS 'Number of Pitches'
  41.                     FROM players JOIN atbats ON players.eliasid  = atbats.pitcher JOIN pitches ON atbats.ab_id = pitches.ab_id
  42.                      WHERE pitches.pitch_type = '",Pitch_Type,"' AND atbats.p_throws = '",P_Throws,"'
  43.                      GROUP BY players.eliasid
  44.                      ORDER BY count(pitches.pitch_type) DESC
  45.                      LIMIT ",N_Pitchers,"",sep="");
  46.                      
  47. # Download the data from the query
  48. Names <- sqlQuery(channel,Name_Query);
  49.  
  50. # Close the channel
  51. close(channel);
  52.  
  53. # Set up arrays for late movement
  54. PFX_mvt <- array(0,dim=N_Pitchers);
  55. PFX_t_rem <- array(0,dim=N_Pitchers);
  56. PFX_arc_rem <- array(0,dim=N_Pitchers);
  57. PFX_pct <- array(0,dim=N_Pitchers);
  58. PFX_num <- array(0,dim=N_Pitchers);
  59. PFX_Drag_mvt <- array(0,dim=N_Pitchers);
  60. PFX_Drag_t_rem <- array(0,dim=N_Pitchers);
  61. PFX_Drag_arc_rem <- array(0,dim=N_Pitchers);
  62. PFX_Drag_pct <- array(0,dim=N_Pitchers);
  63. PFX_Drag_num <- array(0,dim=N_Pitchers);
  64. W_mvt <- array(0,dim=N_Pitchers);
  65. W_t_rem <- array(0,dim=N_Pitchers);
  66. W_arc_rem <- array(0,dim=N_Pitchers);
  67. W_pct <- array(0,dim=N_Pitchers);
  68. W_num <- array(0,dim=N_Pitchers);
  69. W_Drag_mvt <- array(0,dim=N_Pitchers);
  70. W_Drag_t_rem <- array(0,dim=N_Pitchers);
  71. W_Drag_arc_rem <- array(0,dim=N_Pitchers);
  72. W_Drag_pct <- array(0,dim=N_Pitchers);
  73. W_Drag_num <- array(0,dim=N_Pitchers);
  74.  
  75. # Loop over the pitchers to calculate the late movement
  76. for (i in 1:N_Pitchers){
  77.     # Open the channel
  78.     channel <- odbcConnect(database);
  79.     # Set the query to find the pitch data for each pitcher
  80.     Pitch_Query <- paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az FROM pitches WHERE
  81.                           ab_id IN (SELECT ab_id FROM atbats WHERE
  82.                           pitcher = ",Names$eliasid[i],") AND pitch_type = '",Pitch_Type,"'
  83.                           AND px IS NOT NULL",sep="");
  84.     # Download the individual pitch data
  85.     PFX_Data <- sqlQuery(channel,Pitch_Query);
  86.     # Close the channel
  87.     close(channel);
  88.     # Set the number of pitches
  89.     N <- length(PFX_Data$x0);
  90.     # Set arrays for temporary storage
  91.     PFX_loc <- array(0,dim=N);
  92.     Time_PFX <- array(0,dim=N);
  93.     Arc_PFX <- array(0,dim=N);
  94.     PFX_Drag_loc <- array(0,dim=N);
  95.     Time_PFX_Drag <- array(0,dim=N);
  96.     Arc_PFX_Drag <- array(0,dim=N);
  97.     W_loc <- array(0,dim=N);
  98.     Time_W <- array(0,dim=N);
  99.     Arc_W <- array(0,dim=N);
  100.     W_Drag_loc <- array(0,dim=N);
  101.     Time_W_Drag <- array(0,dim=N);
  102.     Arc_W_Drag <- array(0,dim=N);
  103.     # Loop over and process each pitch
  104.     for (j in 1:N){
  105.         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]);
  106.         # Run the late movement algorithm for each of the four versions of movement
  107.         LB_PFX <- Late_Break_PFX(PITCH);
  108.         LB_PFX_Drag <- Late_Break_PFX_Drag(PITCH);
  109.         LB_W <- Late_Break_W(PITCH);
  110.         LB_W_Drag <- Late_Break_W_Drag(PITCH);
  111.         # Populate the arrays
  112.         PFX_loc[j] <- LB_PFX$pfx_loc;
  113.         Time_PFX[j] <- LB_PFX$t_rem;
  114.         Arc_PFX[j] <- LB_PFX$arc_rem;
  115.         PFX_Drag_loc[j] <- LB_PFX_Drag$pfx_drag_loc;
  116.         Time_PFX_Drag[j] <- LB_PFX_Drag$t_rem;
  117.         Arc_PFX_Drag[j] <- LB_PFX_Drag$arc_rem;
  118.         W_loc[j] <- LB_W$w_loc;
  119.         Time_W[j] <- LB_W$t_rem;
  120.         Arc_W[j] <- LB_W$arc_rem;
  121.         W_Drag_loc[j] <- LB_W_Drag$w_drag_loc;
  122.         Time_W_Drag[j] <- LB_W_Drag$t_rem;
  123.         Arc_W_Drag[j] <- LB_W_Drag$arc_rem;
  124.     }
  125.     # Restrict the arrays to non-zero values
  126.     Time_PFX <- Time_PFX[Time_PFX > 0];
  127.     Arc_PFX <- Arc_PFX[Arc_PFX > 0];
  128.     Time_PFX_Drag <- Time_PFX_Drag[Time_PFX_Drag > 0];
  129.     Arc_PFX_Drag <- Arc_PFX_Drag[Arc_PFX_Drag > 0];
  130.     Time_W <- Time_W[Time_W > 0];
  131.     Arc_W <- Arc_W[Arc_W > 0];
  132.     Time_W_Drag <- Time_W_Drag[Time_W_Drag > 0];
  133.     Arc_W_Drag <- Arc_W_Drag[Arc_W_Drag > 0];
  134.     # Find the length of each new array
  135.     N_pfx <- length(Time_PFX);
  136.     N_pfx_d <- length(Time_PFX_Drag);
  137.     N_w <- length(Time_W);
  138.     N_w_d <- length(Time_W_Drag);
  139.     # Assign average values by pitcher
  140.     PFX_mvt[i] <- round(mean(PFX_loc),digits=2);
  141.     PFX_t_rem[i] <- round(mean(Time_PFX),digits=3);
  142.     PFX_arc_rem[i] <- round(mean(Arc_PFX),digits=2);
  143.     PFX_pct[i] <- round(N_pfx/N,digits=2);
  144.     PFX_num[i] <- N_pfx;
  145.     PFX_Drag_mvt[i] <- round(mean(PFX_Drag_loc),digits=2);
  146.     PFX_Drag_t_rem[i] <- round(mean(Time_PFX_Drag),digits=3);
  147.     PFX_Drag_arc_rem[i] <- round(mean(Arc_PFX_Drag),digits=2);
  148.     PFX_Drag_pct[i] <- round(N_pfx_d/N,digits=2);
  149.     PFX_Drag_num[i] <- N_pfx_d;
  150.     W_mvt[i] <- round(mean(W_loc),digits=2);
  151.     W_t_rem[i] <- round(mean(Time_W),digits=3);
  152.     W_arc_rem[i] <- round(mean(Arc_W),digits=2);
  153.     W_pct[i] <- round(N_w/N,digits=2);
  154.     W_num[i] <- N_w;
  155.     W_Drag_mvt[i] <- round(mean(W_Drag_loc),digits=2);
  156.     W_Drag_t_rem[i] <- round(mean(Time_W_Drag),digits=3);
  157.     W_Drag_arc_rem[i] <- round(mean(Arc_W_Drag),digits=2);
  158.     W_Drag_pct[i] <- round(N_w_d/N,digits=2);
  159.     W_Drag_num[i] <- N_w_d;
  160. }
  161.    
  162. # Set arrays for the pitcher names
  163. First <- Names$First;
  164. Last <- Names$Last;
  165.  
  166. # Set up a data frame for All
  167. 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,
  168.                             W_Drag_mvt,W_Drag_t_rem,W_Drag_arc_rem,W_Drag_pct,W_Drag_num);
  169.  
  170. # Write to file
  171. filename_All <- paste("Late_Mvt_All_",Pitch_Type,"_",P_Throws,"_",N_Pitchers,"_",Year,".csv",sep="");
  172. write.csv(df_late_break_All, file = filename_All);
  173.  
  174. # Set up a data frame for PFX
  175. df_late_break_PFX <- data.frame(First,Last,PFX_mvt,PFX_t_rem,PFX_arc_rem,PFX_pct,PFX_num);
  176.  
  177. # Write to file
  178. filename_PFX <- paste("Late_Mvt_PFX_",Pitch_Type,"_",P_Throws,"_",N_Pitchers,"_",Year,".csv",sep="");
  179. write.csv(df_late_break_PFX, file = filename_PFX);
  180.                            
  181. # Set up a data frame for PFX D
  182. 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);
  183.  
  184. # Write to file
  185. filename_PFX_D <- paste("Late_Mvt_PFX_D_",Pitch_Type,"_",P_Throws,"_",N_Pitchers,"_",Year,".csv",sep="");
  186. write.csv(df_late_break_PFX_D, file = filename_PFX_D);
  187.  
  188. # Set up a data frame for W
  189. df_late_break_W <- data.frame(First,Last,W_mvt,W_t_rem,W_arc_rem,W_pct,W_num);
  190.  
  191. # Write to file
  192. filename_W <- paste("Late_Mvt_W_",Pitch_Type,"_",P_Throws,"_",N_Pitchers,"_",Year,".csv",sep="");
  193. write.csv(df_late_break_W, file = filename_W);
  194.  
  195. # Set up a data from for W D
  196. 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);
  197.  
  198. # Write to file
  199. filename_W_D <- paste("Late_Mvt_W_D_",Pitch_Type,"_",P_Throws,"_",N_Pitchers,"_",Year,".csv",sep="");
  200. write.csv(df_late_break_W_D, file = filename_W_D);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement