*
* @PLSGrid(options) gridVECTOR
* Procedure for generating a (logarithmic) grid for lambdas for
* penalized least squares
*
* Revision Schedule
*  05/2025 Written by Tom Doan, Estima
*
procedure PLSGrid gridVECTOR
type vector *gridVECTOR
*
option real R2Guess .7
option integer OrdersOfMagnitude 2
option integer PerOrder 8
option real YY
option real XX 1.0
option choice penalize 1 L1 L2
*
local integer PerSide i
local real    Guess GuessBB GuessSSR
*
if .not.%defined(gridVECTOR) {
   disp "###@PLSGrid(options) <<gridVECTOR>> parameter is missing"
   return
}
if .not.%defined(YY) {
   disp "###@PLSGrid(<<YY=value of Y'Y>>) is required"
   return
}
compute PerSide=OrdersOfMagnitude*PerOrder
*
* Figure out guess for sum of squared residuals
*
compute GuessSSR=(1-R2Guess)*yy
*
* Figure out approximation for ||beta||^2
*
compute GuessBB=R2Guess*yy/xx
*
* Solve for the lambda where the SSR and guess for penalty are equal
*
compute Guess=%if(penalize==1,GuessSSR/sqrt(GuessBB),GuessSSR/GuessBB)
*
* Generate a log-linear grid with <<Guess>> in the center
*
dim gridVECTOR(PerSide*2+1)
ewise gridVECTOR(i)=Guess*10^((1.0/PerOrder)*(i-PerSide-1))
end
