Dear Statalisters,

I want to include a measure I saw in a paper called Krüger, S./Rösch, D. (2017): Downturn LGD modeling using quantile regression, Journal of Banking & Finance, 79, p. 42-56. This measure is like R² and evaluates predicted values but separated for each quantile (see picture)

In the end I want to plot R1 on the y-axis and the quantiles on the x-axis to illustrate which of my models (reg, fracreg, fmm, qr) performs the best over the entire distribution.
I thought about a loop that saves R1(1) to R1(99) in a matrix as shown below for the OLS regression. My results are not quite what I expected so I’m seeking for help. Since I’m fairly new to Stata I’m not sure if I translated the formula the right way or if there is a smarter way to do so.

Code:
*OLS
matrix R1=J(100,2,.)
gen OLSquantres = .
forvalues i=1/99{
gen ptau1 = .
replace ptau1 = (i'/100)*OLSres if OLSres>=0
replace ptau1 = (1-(i'/100))*abs(OLSres) if OLSres<0
qui sum ptau1
scalar ptau1_sum = r(sum)

_pctile $y1, p(i') replace OLSquantres =$y1 - r(r1)
gen ptau2 = .
replace ptau2 = (i'/100)*OLSquantres if OLSquantres>=0
replace ptau2 = (1-(i'/100))*abs(OLSquantres) if OLSquantres<0
qui sum ptau2
scalar ptau2_sum = r(sum)

drop ptau1
drop ptau2
matrix R1[i',2] = 1- (ptau1_sum/ptau2_sum)
matrix R1[i',1] = i'
}
Above OLSres is the residual of the OLS regression and \$y1 my dependent variable.

Kind regards

Steffen