*! v1.1 5 10 2020 Correction for a bug. when the leverage statistic ->1 there is a considerable rounding error for the estimation of *! LOO yhat. The solution here is to use OLD FASHION method for cases with LARGE Leverages. capture program drop cv_regress capture program drop reparser program define cv_regress, rclass syntax ,[cvwgt(varname) generr(str) genlev(str) genhat(str)] ** verify version if `c(stata_version)'<7 { display in red "You need Stata 7.0 or higher" } * Step 1. Verify the right command was excecuted in the previous step if e(cmd)!="regress" { display in red ("Last estimates not found or not a regress command") display in red ("This version of the program only allows for regress command") exit } if "`e(wtype)'"!="" { if e(wtype)=="pweight" { display in red ("Program not compatible with Robust estimation") display in red ("Use aweights, iweights or fweights") exit } } ** check for new variables if "`generr'`genlev'`genhat'"!="" { confirm new variable `generr' `genlev' `genhat', exact } ** determine subsample tempvar esample qui:gen `esample'=e(sample) ** determine dependent variable and weight local yy: word 2 of `e(cmdline)' local aux=e(cmdline) local wgtwgt:word 2 of `e(wexp)' ** estimate model predicted value tempvar yy_hat lvrg qui:predict double `yy_hat' if `esample'==1, qui:predict double `lvrg' if `esample'==1, hat ** Estimate leverage adjustment tempvar lv_adj qui:gen double `lv_adj'=1 if "`e(wtype)'"=="aweight" { qui:sum `wgtwgt' if `esample'==1, meanonly qui:replace `lvrg'=`lvrg'*`wgtwgt'/r(mean) } if "`e(wtype)'"=="fweight" | "`e(wtype)'"=="iweight" { qui:replace `lvrg'=`lvrg'*`wgtwgt' } ** Added step If lvrg is so close to 1 that makes the program fail. tempvar flag qui:gen byte `flag'=1 if abs(1-`lvrg')