clear clear matrix set matsize 800 set mem 500m cd your partition use [lanham pitching data] ** back out hitter ABs * replace BAOpp = . if BAOpp == 9.99 gen abf= H/BAOpp drop if abf==. // sum data per pitcher collapse (sum) W L G GS CG SHO SV H ER HR BB SO IBB WP HBP BK BFP GF R GIDP IPouts abf, by(playerID yearID teamID) // IP gen IP=IPouts/3 // merege Lahman names to integrate 2024 data merge m:m playerID using [lahman names],update drop _merge merge m:m bbrefID yearID using bb_ref_sh_sf patch,update drop _merge drop if yearID==. /// BIP files merge m:m playerID yearID using 1930-79 BIP data,update drop _merge merge m:m playerID yearID using 1980-99 BIP data,update drop _merge merge m:m playerID yearID using 1912-29 BIP data ,update drop _merge merge m:m playerID yearID using 2000-2024 BIP data, update drop _merge // correct a possible glitch in Lahman team-name key for old retrosheet; I think this was fixed upstream but let's be sure replace teamID ="MIL" if teamID == "ML4" & year > 1997 replace teamID ="ML4" if teamID == "MIL" & year <1998 /// merge bbref 2024 pitcher data merge m:m bbrefID yearID using bb_ref_2024_pitcher_patch,update drop _merge drop if yearID==. /// narrow to ip 100+ drop if IP<100 drop if ifpop==. drop if SO ==. /// abf for 2024 replace abf=(H+(IP*3)) if year ==2024 *** determine performance values as rates gen k_pip=SO/IP gen bb_pip=BB/IP gen hr_pip=HR/IP gen hbp_pip=HBP/IP drop if IP <100 gen rapg=9*R/IP gen obabip=(H-HR)/(abf-(SO+HR)+SF) gen gb_pct= gb/(gb+airb) gen ifgb_pip=ifgb/IP gen ifpop_pip=ifpop/IP gen airb_pip=airb/IP gen ofair_pip=ofair/IP // fipr scores regress rapg k_pip bb_pip hr_pip hbp_pip [pweight=IP] predict fipr *********************************** * Population-Weighted means/z-scores *********************************** * 1. Summarize IP within each year egen total_IP = sum(IP), by(year) * 2. Compute weighted mean of fipr generate double weighted_fipr_product = fipr * IP egen double total_weighted_fipr = sum(weighted_fipr_product), by(year) generate double weighted_mean_fipr = total_weighted_fipr / total_IP * 3. Compute weighted sum of squared deviations * (fipr - weighted_mean_fipr)^2 * IP generate double wssd = (fipr - weighted_mean_fipr)^2 * IP egen double total_wssd = sum(wssd), by(year) * 4. population variance: divide by total weight generate double popvar_fipr = total_wssd / total_IP * 5. Population weighted standard deviation generate double pfiprd_fipr = sqrt(popvar_fipr) * 6. [Optional] Clean up intermediate variables drop wssd weighted_fipr_product total_weighted_fipr rename weighted_mean_fipr wvar_fipr rename pfiprd_fipr wsd_fipr drop total_wssd *** zscore gen z_fipr = (fipr-wvar_fipr)/wsd_fipr * 2. Compute weighted mean of gb_pct generate double weighted_gb_pct_product = gb_pct * IP egen double total_weighted_gb_pct = sum(weighted_gb_pct_product), by(year) generate double weighted_mean_gb_pct = total_weighted_gb_pct / total_IP * 3. Compute weighted sum of squared deviations * (gb_pct - weighted_mean_gb_pct)^2 * IP generate double wssd = (gb_pct - weighted_mean_gb_pct)^2 * IP egen double total_wssd = sum(wssd), by(year) * 4. population variance: divide by total weight generate double popvar_gb_pct = total_wssd / total_IP * 5. Population weighted standard deviation generate double pgb_pctd_gb_pct = sqrt(popvar_gb_pct) * 6. [Optional] Clean up intermediate variables drop wssd weighted_gb_pct_product total_weighted_gb_pct rename weighted_mean_gb_pct wvar_gb_pct rename pgb_pctd_gb_pct wsd_gb_pct drop total_wssd *** zscore gen z_gb_pct = (gb_pct-wvar_gb_pct)/wsd_gb_pct * 2. Compute weighted mean of ofair_pip generate double weighted_ofair_pip_product = ofair_pip * IP egen double total_weighted_ofair_pip = sum(weighted_ofair_pip_product), by(year) generate double weighted_mean_ofair_pip = total_weighted_ofair_pip / total_IP * 3. Compute weighted sum of squared deviations * (ofair_pip - weighted_mean_ofair_pip)^2 * IP generate double wssd = (ofair_pip - weighted_mean_ofair_pip)^2 * IP egen double total_wssd = sum(wssd), by(year) * 4. population variance: divide by total weight generate double popvar_ofair_pip = total_wssd / total_IP * 5. Population weighted standard deviation generate double pofair_pipd_ofair_pip = sqrt(popvar_ofair_pip) * 6. [Optional] Clean up intermediate variables drop wssd weighted_ofair_pip_product total_weighted_ofair_pip rename weighted_mean_ofair_pip wvar_ofair_pip rename pofair_pipd_ofair_pip wsd_ofair_pip drop total_wssd *** zscore gen z_ofair_pip = (ofair_pip-wvar_ofair_pip)/wsd_ofair_pip * 2. Compute weighted mean of obabip generate double weighted_obabip_product = obabip * IP egen double total_weighted_obabip = sum(weighted_obabip_product), by(year) generate double weighted_mean_obabip = total_weighted_obabip / total_IP * 3. Compute weighted sum of squared deviations * (obabip - weighted_mean_obabip)^2 * IP generate double wssd = (obabip - weighted_mean_obabip)^2 * IP egen double total_wssd = sum(wssd), by(year) * 4. population variance: divide by total weight generate double popvar_obabip = total_wssd / total_IP * 5. Population weighted standard deviation generate double pobabipd_obabip = sqrt(popvar_obabip) * 6. [Optional] Clean up intermediate variables drop wssd weighted_obabip_product total_weighted_obabip rename weighted_mean_obabip wvar_obabip rename pobabipd_obabip wsd_obabip drop total_wssd *** zscore gen z_obabip = (obabip-wvar_obabip)/wsd_obabip * 2. Compute weighted mean of rapg generate double weighted_rapg_product = rapg * IP egen double total_weighted_rapg = sum(weighted_rapg_product), by(year) generate double weighted_mean_rapg = total_weighted_rapg / total_IP * 3. Compute weighted sum of squared deviations * (rapg - weighted_mean_rapg)^2 * IP generate double wssd = (rapg - weighted_mean_rapg)^2 * IP egen double total_wssd = sum(wssd), by(year) * 4. population variance: divide by total weight generate double popvar_rapg = total_wssd / total_IP * 5. Population weighted standard deviation generate double prapgd_rapg = sqrt(popvar_rapg) * 6. [Optional] Clean up intermediate variables drop wssd weighted_rapg_product total_weighted_rapg rename weighted_mean_rapg wvar_rapg rename prapgd_rapg wsd_rapg drop total_wssd *** zscore gen z_rapg = (rapg-wvar_rapg)/wsd_rapg * 2. Compute weighted mean of ifpop_pip generate double weighted_ifpop_pip_product = ifpop_pip * IP egen double total_weighted_ifpop_pip = sum(weighted_ifpop_pip_product), by(year) generate double weighted_mean_ifpop_pip = total_weighted_ifpop_pip / total_IP * 3. Compute weighted sum of squared deviations * (ifpop_pip - weighted_mean_ifpop_pip)^2 * IP generate double wssd = (ifpop_pip - weighted_mean_ifpop_pip)^2 * IP egen double total_wssd = sum(wssd), by(year) * 4. population variance: divide by total weight generate double popvar_ifpop_pip = total_wssd / total_IP * 5. Population weighted standard deviation generate double pifpop_pipd_ifpop_pip = sqrt(popvar_ifpop_pip) * 6. [Optional] Clean up intermediate variables drop wssd weighted_ifpop_pip_product total_weighted_ifpop_pip rename weighted_mean_ifpop_pip wvar_ifpop_pip rename pifpop_pipd_ifpop_pip wsd_ifpop_pip drop total_wssd gen z_ifpop_pip = (ifpop_pip-wvar_ifpop_pip)/wsd_ifpop_pip * 2. Compute weighted mean of k_pip generate double weighted_k_pip_product = k_pip * IP egen double total_weighted_k_pip = sum(weighted_k_pip_product), by(year) generate double weighted_mean_k_pip = total_weighted_k_pip / total_IP * 3. Compute weighted sum of squared deviations * (k_pip - weighted_mean_k_pip)^2 * IP generate double wssd = (k_pip - weighted_mean_k_pip)^2 * IP egen double total_wssd = sum(wssd), by(year) * 4. population variance: divide by total weight generate double popvar_k_pip = total_wssd / total_IP * 5. Population weighted standard deviation generate double pk_pipd_k_pip = sqrt(popvar_k_pip) * 6. [Optional] Clean up intermediate variables drop wssd weighted_k_pip_product total_weighted_k_pip rename weighted_mean_k_pip wvar_k_pip rename pk_pipd_k_pip wsd_k_pip drop total_wssd *** zscore gen z_k_pip = (k_pip-wvar_k_pip)/wsd_k_pip * 2. Compute weighted mean of hr_pip generate double weighted_hr_pip_product = hr_pip * IP egen double total_weighted_hr_pip = sum(weighted_hr_pip_product), by(year) generate double weighted_mean_hr_pip = total_weighted_hr_pip / total_IP * 3. Compute weighted sum of squared deviations * (hr_pip - weighted_mean_hr_pip)^2 * IP generate double wssd = (hr_pip - weighted_mean_hr_pip)^2 * IP egen double total_wssd = sum(wssd), by(year) * 4. population variance: divide by total weight generate double popvar_hr_pip = total_wssd / total_IP * 5. Population weighted standard deviation generate double phr_pipd_hr_pip = sqrt(popvar_hr_pip) * 6. [Optional] Clean up intermediate variables drop wssd weighted_hr_pip_product total_weighted_hr_pip rename weighted_mean_hr_pip wvar_hr_pip rename phr_pipd_hr_pip wsd_hr_pip drop total_wssd *** zscore gen z_hr_pip = (hr_pip-wvar_hr_pip)/wsd_hr_pip * 2. Compute weighted mean of bb_pip generate double weighted_bb_pip_product = bb_pip * IP egen double total_weighted_bb_pip = sum(weighted_bb_pip_product), by(year) generate double weighted_mean_bb_pip = total_weighted_bb_pip / total_IP * 3. Compute weighted sum of squared deviations * (bb_pip - weighted_mean_bb_pip)^2 * IP generate double wssd = (bb_pip - weighted_mean_bb_pip)^2 * IP egen double total_wssd = sum(wssd), by(year) * 4. population variance: divide by total weight generate double popvar_bb_pip = total_wssd / total_IP * 5. Population weighted standard deviation generate double pbb_pipd_bb_pip = sqrt(popvar_bb_pip) * 6. [Optional] Clean up intermediate variables drop wssd weighted_bb_pip_product total_weighted_bb_pip rename weighted_mean_bb_pip wvar_bb_pip rename pbb_pipd_bb_pip wsd_bb_pip drop total_wssd *** zscore gen z_bb_pip = (bb_pip-wvar_bb_pip)/wsd_bb_pip // drop outliers // Compute quartiles and IQR summarize z_obabip, detail scalar iqr = r(p75) - r(p25) scalar lower_bound = r(p25) - 1.5 * iqr scalar upper_bound = r(p75) + 1.5 * iqr // Replace outliers with missing values replace z_obabip = . if z_obabip < lower_bound | z_obabip > upper_bound // Compute quartiles and IQR summarize z_ofair_pip, detail scalar iqr = r(p75) - r(p25) scalar lower_bound = r(p25) - 1.5 * iqr scalar upper_bound = r(p75) + 1.5 * iqr // Replace outliers with missing values // replace z_ofair_pip = . if z_ofair_pip < lower_bound | z_ofair_pip > upper_bound // Compute quartiles and IQR summarize z_airb_pip, detail scalar iqr = r(p75) - r(p25) scalar lower_bound = r(p25) - 1.5 * iqr scalar upper_bound = r(p75) + 1.5 * iqr // Replace outliers with missing values replace z_airb_pip = . if z_airb_pip < lower_bound | z_airb_pip > upper_bound // Compute quartiles and IQR summarize z_fipr, detail scalar iqr = r(p75) - r(p25) scalar lower_bound = r(p25) - 1.5 * iqr scalar upper_bound = r(p75) + 1.5 * iqr // Replace outliers with missing values replace z_fipr = . if z_fipr < lower_bound | z_fipr > upper_bound zscore z_fipr replace z_fipr=z_z_fipr*-1 // Compute quartiles and IQR summarize z_rapg, detail scalar iqr = r(p75) - r(p25) scalar lower_bound = r(p25) - 1.5 * iqr scalar upper_bound = r(p75) + 1.5 * iqr // Replace outliers with missing values replace z_rapg = . if z_rapg < lower_bound | z_rapg > upper_bound zscore z_rapg replace z_rapg=z_z_rapg // Compute quartiles and IQR summarize z_ifpop_pip, detail scalar iqr = r(p75) - r(p25) scalar lower_bound = r(p25) - 1.5 * iqr scalar upper_bound = r(p75) + 1.5 * iqr // Replace outliers with missing values // replace z_ifpop_pip = . if z_ifpop_pip < lower_bound | z_ifpop_pip > upper_bound zscore z_ifpop_pip replace z_ifpop_pip=z_z_ifpop_pip // Compute quartiles and IQR summarize z_k_pip, detail scalar iqr = r(p75) - r(p25) scalar lower_bound = r(p25) - 1.5 * iqr scalar upper_bound = r(p75) + 1.5 * iqr // Replace outliers with missing values replace z_k_pip = . if z_k_pip < lower_bound | z_k_pip > upper_bound zscore z_k_pip replace z_k_pip=z_z_k_pip // Compute quartiles and IQR summarize z_bb_pip, detail scalar iqr = r(p75) - r(p25) scalar lower_bound = r(p25) - 1.5 * iqr scalar upper_bound = r(p75) + 1.5 * iqr // Replace outliers with missing values replace z_bb_pip = . if z_bb_pip < lower_bound | z_bb_pip > upper_bound zscore z_bb_pip replace z_bb_pip=z_z_bb_pip // Compute quartiles and IQR summarize z_hr_pip, detail scalar iqr = r(p75) - r(p25) scalar lower_bound = r(p25) - 1.5 * iqr scalar upper_bound = r(p75) + 1.5 * iqr // Replace outliers with missing values replace z_hr_pip = . if z_hr_pip < lower_bound | z_hr_pip > upper_bound zscore z_hr_pip replace z_hr_pip=z_z_hr_pip // Compute quartiles and IQR summarize z_gb_pct, detail scalar iqr = r(p75) - r(p25) scalar lower_bound = r(p25) - 1.5 * iqr scalar upper_bound = r(p75) + 1.5 * iqr // Replace outliers with missing values replace z_gb_pct = . if z_gb_pct < lower_bound | z_gb_pct > upper_bound rename z_gb_pct bobo //for some reason the program didn't like z_z_gb_pct go figure zscore bobo gen z_gb_pct=z_bobo // merge team fielding gen teamid = teamID merge m:m teamid yearID using "C:\Users\dmk38\Documents\x5\team_field_composite.dta" drop _merge /// reliability * Step 1: Sort the data by playerid and yearID sort playerID yearID * Step 2: Generate season -1 & season +1 variables gen ifpop_pip_minus1 = . gen ifpop_pip_plus1 = . gen ofair_pip_minus1 = . gen ofair_pip_plus1 = . gen obabip_minus1 = . gen obabip_plus1 = . gen k_pip_minus1 = . gen k_pip_plus1 = . gen bb_pip_minus1 = . gen bb_pip_plus1 = . gen hr_pip_minus1 = . gen hr_pip_plus1 = . gen field_composite_minus1 = . gen field_composite_plus1 = . gen fipr_minus1 = . gen fipr_plus1 = . gen gb_pct_minus1 = . gen gb_pct_plus1 = . * Step 3: Replace missing values for season -1 and season plus 1 bysort playerID (yearID): replace field_composite_minus1 = field_composite[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace field_composite_plus1 = field_composite[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace obabip_minus1 = obabip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace obabip_plus1 = obabip[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace fipr_minus1 = fipr[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace fipr_plus1 = fipr[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace k_pip_minus1 = k_pip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace k_pip_plus1 = k_pip[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace bb_pip_minus1 = bb_pip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace bb_pip_plus1 = bb_pip[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace hr_pip_minus1 = hr_pip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace hr_pip_plus1 = hr_pip[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace ifpop_pip_minus1 = ifpop_pip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace ifpop_pip_plus1 = ifpop_pip[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace gb_pct_minus1 = gb_pct[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace gb_pct_plus1 = gb_pct[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace ofair_pip_minus1 = ofair_pip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace ofair_pip_plus1 = ofair_pip[_n+1] if playerID == playerID[_n-1] * Step 1: Sort the data by playerid and yearID sort playerID yearID * Step 2: Generate season -1 & season +1 variables gen z_ifpop_pip_minus1 = . gen z_ifpop_pip_plus1 = . gen z_ofair_pip_minus1 = . gen z_ofair_pip_plus1 = . gen z_obabip_minus1 = . gen z_obabip_plus1 = . gen z_fipr_minus1 = . gen z_fipr_plus1 = . gen z_k_pip_minus1 = . gen z_k_pip_plus1 = . gen z_bb_pip_minus1 = . gen z_bb_pip_plus1 = . gen z_hr_pip_minus1 = . gen z_hr_pip_plus1 = . gen z_field_composite_minus1 = . gen z_field_composite_plus1 = . gen z_gb_pct_minus1 = . gen z_gb_pct_plus1 = . * Step 3: Replace missing values for season -1 and season plus 1 bysort playerID (yearID): replace z_field_composite_minus1 = z_field_composite[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_field_composite_plus1 = z_field_composite[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_obabip_minus1 = z_obabip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_obabip_plus1 = z_obabip[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_fipr_minus1 = z_fipr[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_fipr_plus1 = z_fipr[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_gb_pct_minus1 = z_gb_pct[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_gb_pct_plus1 = z_gb_pct[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_k_pip_minus1 = z_k_pip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_k_pip_plus1 = z_k_pip[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_bb_pip_minus1 = z_bb_pip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_bb_pip_plus1 = z_bb_pip[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_hr_pip_minus1 = z_hr_pip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_hr_pip_plus1 = z_hr_pip[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_ifpop_pip_minus1 = z_ifpop_pip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_ifpop_pip_plus1 = z_ifpop_pip[_n+1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_ofair_pip_minus1 = z_ofair_pip[_n-1] if playerID == playerID[_n-1] bysort playerID (yearID): replace z_ofair_pip_plus1 = z_ofair_pip[_n+1] if playerID == playerID[_n-1] // scale for groundball propensity alpha (ofair_pip_plus1 ofair_pip ofair_pip_minus1 ifpop_pip_plus1 ifpop_pip ifpop_pip_minus1 gb_pct gb_pct_minus1 gb_pct_plus1), s gen(gb_boy) * 2. Compute weighted mean of gb_boy generate double weighted_gb_boy_product = gb_boy * IP egen double total_weighted_gb_boy = sum(weighted_gb_boy_product), by(year) generate double weighted_mean_gb_boy = total_weighted_gb_boy / total_IP * 3. Compute weighted sum of squared deviations * (gb_boy - weighted_mean_gb_boy)^2 * IP generate double wssd = (gb_boy - weighted_mean_gb_boy)^2 * IP egen double total_wssd = sum(wssd), by(year) * 4. population variance: divide by total weight generate double popvar_gb_boy = total_wssd / total_IP * 5. Population weighted standard deviation generate double pgb_boyd_gb_boy = sqrt(popvar_gb_boy) * 6. [Optional] Clean up intermediate variables drop wssd weighted_gb_boy_product total_weighted_gb_boy rename weighted_mean_gb_boy wvar_gb_boy rename pgb_boyd_gb_boy wsd_gb_boy drop total_wssd *** zscore gen z_gb_boy = (gb_boy-wvar_gb_boy)/wsd_gb_boy // reliability for of-fly & if-popup propensities alpha ofair_pip_plus1 ofair_pip ofair_pip_minus1, i alpha ifpop_pip_plus1 ifpop_pip ifpop_pip_minus1, /// generate a nice output file drop if IP <154 & year <1961 drop if IP < 162 & year > 1960 drop if nameLast =="" export excel nameFirst nameLast yearID z_ifpop_pip z_ofair_pip z_gb_boy using "pitch_type.xls", /// firstrow(variables) replace /// regression analysis of grounball propensity hireg z_obabip (z_fipr) (z_field_composite) (z_gb_boy) hireg z_rapg (z_fipr) (z_field_composite) (z_gb_boy) // regression analyses of of_fly & if_popup propensities hireg z_rapg (z_fipr) (z_field_composite) (z_ifpop_pip) (z_ofair_pip) hireg z_obabip (z_fipr) (z_field_composite) (z_ifpop_pip) (z_ofair_pip) /// pracitical impact -- MC sims estsimp regress z_rapg z_fipr z_field_composite z_ifpop_pip z_ofair_pip setx mean simqi, fd(ev) changex(z_ifpop_pip p67 p33 z_ofair_pip p33 p67) setx mean simqi, fd(ev) changex(z_fipr p67 p33) /// measuring pitch_type contributions to elements of run avoidance * Create variables to store results gen R2_fip_fc=. gen R2i_ofair=. gen R2i_ifpop=. gen R2_t=. gen R2_obabip=. gen R_fc=. gen R2i_pt=. foreach y of numlist 1912/2019 2021 2022 2023 2024 { di "Processing year `y'" quietly regress rapg fipr field_composite if year == `y' replace R2_fip_fc = e(r2) if year == `y' quietly regress rapg fipr field_composite ifpop_pip if year == `y' replace R2i_ifpop = e(r2)-R2_fip_fc if year == `y' quietly regress rapg fipr field_composite ofair_pip ifpop_pip if year == `y' replace R2_t = e(r) if year == `y' replace R2i_ofair = e(r2)-(R2_fip_fc+R2i_ifpop) if year == `y' } foreach y of numlist 1920/2019 2021 2022 2023 2024 { di "Processing year `y'" quietly regress obabip field_composite replace R_fc=e(r2) quietly regress obabip field_composite ofair_pip ifpop_pip if year == `y' replace R2i_pt =e(r2)-R_fc if year ==`y' } * Collapse to get mean R2 by year collapse (mean) R2_fip_fc R2i_ofair R2i_ifpop R2_t R2_obabip R2i_pt, by(year) * Create cumulative variables for ribbon placement gen cum_fip_fc = R2_fip*100 gen cum_ifpop = (cum_fip_fc)+R2i_ifpop*100 gen cum_ofair= (cum_ifpop) + R2i_ofair*100 * Smooth the data for each cumulative variable foreach lname in fip_fc ofair ifpop { lpoly cum_`lname' year,bw(2) gen(smooth_`lname') at(year) } * Create a variable for the lower bound gen zero = 0 twoway /// rarea smooth_fip_fc zero year, color(gs12%60) alcolor(gs12%01) || /// Smoothed Base Rrf rarea smooth_ifpop smooth_fip_fc year, color(red%80) alcolor(blue%01) || /// Smoothed Increment K rarea smooth_ofair smooth_ifpop year, color(blue%80) alcolor(red%01) || /// Smoothed Increment BB , legend(order(1 "FIP plus Field" 2 "Increment IF pop" 3 "Increment OF air" )) /// title(" ") /// xlabel(1911 1920(10)2024, nogrid angle(45)format(%ty)) /// ylabel(0(10)100, nogrid angle(0)) /// ytitle("R² Value") /// xtitle("Year") gen R2i_pt_100= R2i_pt*100 twoway /// (lpoly R2i_pt_100 year if year, bw(2)lcolor(black) lpattern(dash)) /// , /// ylabel(0(10)40, nogrid) xlabel(1920(10)2024, angle(45) nogrid) /// title("") xtitle("") /// legend(off) /// graphs /// measuring pitch_type contributions to elements of run avoidance * Create variables to store results gen R2_fip_fc=. gen R2i_ofair=. gen R2i_ifpop=. gen R2_t=. gen R2_obabip=. gen R_fc=. gen R2i_pt=. foreach y of numlist 1912/2019 2021 2022 2023 2024 { di "Processing year `y'" quietly regress rapg fipr field_composite if year == `y' replace R2_fip_fc = e(r2) if year == `y' quietly regress rapg fipr field_composite ifpop_pip if year == `y' replace R2i_ifpop = e(r2)-R2_fip_fc if year == `y' quietly regress rapg fipr field_composite ofair_pip ifpop_pip if year == `y' replace R2_t = e(r) if year == `y' replace R2i_ofair = e(r2)-(R2_fip_fc+R2i_ifpop) if year == `y' } foreach y of numlist 1920/2019 2021 2022 2023 2024 { di "Processing year `y'" quietly regress obabip field_composite replace R_fc=e(r2) quietly regress obabip field_composite ofair_pip ifpop_pip if year == `y' replace R2i_pt =e(r2)-R_fc if year ==`y' } * Collapse to get mean R2 by year collapse (mean) R2_fip_fc R2i_ofair R2i_ifpop R2_t R2_obabip R2i_pt, by(year) * Create cumulative variables for ribbon placement gen cum_fip_fc = R2_fip*100 gen cum_ifpop = (cum_fip_fc)+R2i_ifpop*100 gen cum_ofair= (cum_ifpop) + R2i_ofair*100 * Smooth the data for each cumulative variable foreach lname in fip_fc ofair ifpop { lpoly cum_`lname' year,bw(2) gen(smooth_`lname') at(year) } * Create a variable for the lower bound gen zero = 0 twoway /// rarea smooth_fip_fc zero year, color(gs12%60) alcolor(gs12%01) || /// Smoothed Base Rrf rarea smooth_ifpop smooth_fip_fc year, color(red%80) alcolor(blue%01) || /// Smoothed Increment K rarea smooth_ofair smooth_ifpop year, color(blue%80) alcolor(red%01) || /// Smoothed Increment BB , legend(order(1 "FIP plus Field" 2 "Increment IF pop" 3 "Increment OF air" )) /// title(" ") /// xlabel(1911 1920(10)2024, nogrid angle(45)format(%ty)) /// ylabel(0(10)100, nogrid angle(0)) /// ytitle("R² Value") /// xtitle("Year") gen R2i_pt_100= R2i_pt*100 twoway /// (lpoly R2i_pt_100 year if year, bw(2)lcolor(black) lpattern(dash)) /// , /// ylabel(0(10)40, nogrid) xlabel(1920(10)2024, angle(45) nogrid) /// title("") xtitle("") /// legend(off)