如何在Stata中自动计算PRESS和$R^2_{predicted}$

How to calculate PRESS and $R^2_{predicted}$ in Stata automatically

所以我有两个模型,我想计算这些统计数据。在 Stata 中有计算它们的软件包吗?

PRESS statistic (wiki)

而且,如果我没记错的话。 $$ R^2_{预测} = 1 - \frac{RESET}{ESS} $$。

clear all

program define press, rclass
    syntax varlist(fv) [if] [in]        ///
    [fweight aweight pweight iweight] , ///
    [nodots]

    gettoken y x : varlist
    marksample touse
    preserve
    quietly keep if `touse'
    if "`weight'" != "" {
        local wgt "[`weight'`exp']"
    }

    tempvar pred temp prs
    quietly gen double `pred' = .
    if "`dots'" == "" _dots 0, title(Jackknife replications) reps(`=_N')
    forvalues i = 1/`=_N' {
        capture {
            reg `y' `x' `wgt' if _n != `i'
            predict double `temp'
            replace `pred' = `temp' in `i'
            drop `temp'
        }
        if "`dots'" == "" _dots `i' `=_rc > 0'
    }
    quietly gen double `prs' = (`y' - `pred')^2
    sum `prs', meanonly

    if "`dots'" == "" di _n _n
    di as txt "The predicted residual sum of squares is " as result r(sum)
    return scalar press = r(sum)
    restore
end

sysuse auto
press price mpg i.foreign

对于多元线性回归,PRESS 也可以计算为 r(i)^2/(1-h(i))^2 的总和,其中 r(i) 是残差,h(i) 是帽子值(hat matrix, which is X*(X'*X)^-1*X', where X is the design matrix.

的对角线条目

在 Stata 中,在 regress 之后调用以下命令 press,不带参数:

prog def press, rclass
    tempvar r h p
    predict `r', r
    predict `h', h
    gen `p'=(`r'/(1-`h'))^2
    sum `p', mean
    di in gr "PRESS = " in ye r(sum)
    ret sca press=r(sum)
end