clear clear matrix set matsize 800 set mem 500m cd [your drive partition] use lanham_pitching.dta collapse (sum) HR BB SO IBB WP HBP R IPouts , by(playerID yearID teamID lgID) gen IP=IPouts/3 merge m:m playerID using lahman_names drop _merge merge m:m bbrefID yearID using bbr 2024 pitcher patch,update drop _merge drop if yearID==. *** determine performance values as rates gen hr_pip=HR/IP gen k_pip=SO/IP gen bb_pip=BB/IP gen hbp_pip=HBP/IP gen rapg=R/IP*9 *** create fipr scores for pitchers gen fipr=. foreach y of numlist 1900/2024 { di "year `y'" regress rapg k_pip bb_pip hr_pip hbp_pip [iweight=IP] if year==`y' predict yhat replace fipr = yhat if year==`y' drop yhat } collapse fipr k_pip bb_pip hr_pip hbp_pip rapg (sum) IP , by(bbrefID playerID yearID) *** merge conventional FIP merge 1:1 bbrefID yearID using fg fip data, update drop _merge *** season-by-season regression gen R2_fip=. gen R2_fipr=. foreach y of numlist 1900/2024 { di "Processing year `y'" quietly regress rapg FIP [iweight=IP] if year == `y' replace R2_fip = e(r2) if year == `y' quietly regress rapg fipr [iweight=IP] if year == `y' replace R2_fipr = e(r2) if year == `y' } gen R2_fipr_scaled=R2_fipr*100 gen R2_fip_scaled=R2_fip*100 twoway /// (lpoly R2_fip_scaled year , bw(5) fcolor(blue) lpattern(dash)) /// (lpoly R2_fipr_scaled year , bw(5)lcolor(red) lpattern(dash)) /// , /// ylabel(20(10)70, nogrid) xlabel(1912 1920(10)2024, angle(45) nogrid) /// title("") xtitle("") /// legend(off) *** store fipr weights *** * Create a temporary file to store the coefficients tempfile coef_results postfile handle int year double(b_k_pip b_bb_pip b_hr_pip b_hbp_pip) using `coef_results' * Loop over each year from 1900 to 2024 foreach y of numlist 1900/2024 { di "Processing year `y'..." quietly { regress rapg k_pip bb_pip hr_pip hbp_pip [iweight=IP] if year == `y' * Store coefficients for each variable local b_k = _b[k_pip] local b_bb = _b[bb_pip] local b_hr = _b[hr_pip] local b_hbp = _b[hbp_pip] } post handle (`y') (`b_k') (`b_bb') (`b_hr') (`b_hbp') } postclose handle use `coef_results', clear list *** plot them * Graph for b_k_pip twoway (lpoly b_k_pip year, lpattern(dash) bw(2) lcolor(black)), /// xlabel(1900(10)2020, nogrid format(%02.0f) angle(45)) /// ylabel(, nogrid) /// ytitle("") xtitle("Season") /// graphregion(color(white)) /// plotregion(style(none)) /// xscale(r(1912 2020)) /// legend(off) graph save graph1.gph, replace * Graph for b_bb_pip twoway (lpoly b_bb_pip year, lpattern(dash) bw(2) lcolor(red)), /// xlabel(1900(10)2020, nogrid format(%02.0f) angle(45)) /// ylabel(, nogrid) /// ytitle("") xtitle("Season") /// graphregion(color(white)) /// plotregion(style(none)) /// xscale(r(1912 2020)) /// legend(off) graph save graph2.gph, replace * Graph for b_hbp_pip twoway (lpoly b_hbp_pip year, lpattern(dash) bw(2) lcolor(green)), /// xlabel(1900(10)2020, nogrid format(%02.0f) angle(45)) /// ylabel(, nogrid) /// ytitle("") xtitle("Season") /// graphregion(color(white)) /// plotregion(style(none)) /// xscale(r(1912 2020)) /// legend(off) graph save graph3.gph, replace * Graph for b_hr_pip twoway (lpoly b_hr_pip year, lpattern(dash) bw(2) lcolor(blue)), /// xlabel(1900(10)2020, nogrid format(%02.0f) angle(45)) /// ylabel(, nogrid) /// ytitle("") xtitle("Season") /// graphregion(color(white)) /// plotregion(style(none)) /// xscale(r(1912 2020)) /// legend(off) graph save graph4.gph, replace graph combine graph1.gph graph2.gph graph3.gph graph4.gph, cols(2) *** save weights ** export excel using "[your drive]\fipr_weights.xls", firstrow(variables) replace