📄 regres_p.ado
字号:
*! version 1.0.12 16mar2005
program define regres_p
version 6, missing
syntax [anything] [if] [in] [, SCores * ]
if `"`scores'"' != "" {
GenScores `0'
exit
}
/* Step 1:
place command-unique options in local myopts
Note that standard options are
LR:
Index XB Cooksd Hat
REsiduals RSTAndard RSTUdent
STDF STDP STDR noOFFset
SE:
Index XB STDP noOFFset
*/
local xopts COVratio DFBeta(varname) DFIts E(string) ///
Hat Leverage Pr(string) Welsch YStar(string)
local svyopts Residuals
if "`e(prefix)'" == "svy" {
local xopts
}
else if "`e(prefix)'" == "" {
local svyopts
}
local myopts `xopts' `svyopts'
/* If robust, trap invalid options and give nice error msg. */
if "`e(vcetype)'" == "Robust" {
local zero `0'
local 0 ", `options'"
syntax [, Cooksd Leverage RSTAndard RSTUdent STDF STDR /*
*/ COVratio DFBeta(varname) DFIts Welsch * ]
local badopt "`cooksd'`leverage'`rstandard'`rstudent'`stdf'"
local badopt "`badopt'`stdr'`covratio'`dfbeta'`dfits'`welsch'"
if "`badopt'" != "" {
if "`dfbeta'" == "" {
di as error /*
*/ "`badopt' not allowed after robust estimation"
}
else {
di as error /*
*/ "dfbeta() not allowed after robust estimation"
}
exit 198
}
local 0 `zero'
}
/* Step 2:
call _propts, exit if done,
else collect what was returned.
*/
_pred_se "`myopts'" `0'
if `s(done)' { exit }
local vtyp `s(typ)'
local varn `s(varn)'
local 0 `"`s(rest)'"'
/* Step 3:
Parse your syntax.
*/
syntax [if] [in] [, `myopts' noOFFset]
/* Step 4:
Concatenate switch options together
*/
local type "`covrati'`dfits'`leverag'`welsch'`hat'`residua'"
local args `"`dfbeta'`pr'`e'`ystar'"'
/* Step 5:
quickly process default case if you can
Do not forget -nooffset- option.
*/
if "`type'"=="" & `"`args'"'=="" {
di in gr "(option xb assumed; fitted values)"
_predict `vtyp' `varn' `if' `in', `offset'
label var `varn' "Fitted values"
exit
}
/* Step 6:
mark sample (this is not e(sample)).
*/
marksample touse
/* Step 7:
handle options that take argument one at a time.
Comment if restricted to e(sample).
Be careful in coding that number of missing values
created is shown.
Do all intermediate calculations in double.
*/
if "`residua'" != "" {
if `"`residua'"' != `"`type'`args'"' {
error 198
}
GenScores `vtyp' `varn' if `touse'
exit
}
if "`dfbeta'"!="" { /* restricted to e(sample) */
if "`type'"!="" { error 198 }
if "`offset'"!="" {
di in red "nooffset() with dfbeta not allowed"
exit 198
}
DFbeta "`vtyp'" "`varn'" "`touse'" "`dfbeta'"
exit
}
if `"`args'"'!="" {
if "`type'"!="" { error 198 }
regre_p2 "`vtyp'" "`varn'" "`touse'" "`offset'" /*
*/ `"`pr'"' `"`e'"' `"`ystar'"' "e(rmse)"
exit
}
/* Step 8:
handle switch options that can be used in-sample or
out-of-sample one at a time.
Be careful in coding that number of missing values
created is shown.
Do all intermediate calculations in double.
*/
if "`type'"=="leverage" || "`type'"=="hat" {
_predict `vtyp' `varn' if `touse', hat `offset'
exit
}
/* Step 9:
handle switch options that can be used in-sample only.
Same comments as for step 8.
*/
qui replace `touse'=0 if !e(sample)
if "`type'"=="dfits" { /* restricted to e(sample) */
tempvar hh t
qui _predict double `hh' if `touse', hat `offset'
qui _predict double `t' if `touse', rstudent `offset'
gen `vtyp' `varn' = `t'*sqrt(`hh'/(1-`hh')) if `touse'
label var `varn' "Dfits"
exit
}
if "`type'"=="welsch" { /* restricted to e(sample) */
tempvar hh t
qui _predict double `hh' if `touse', hat `offset'
qui _predict double `t' if `touse', rstudent `offset'
gen `vtyp' `varn'=(`t'*sqrt(`hh'/(1-`hh')))* /*
*/ sqrt((e(N)-1)/(1-`hh')) if `touse'
label var `varn' "Welsch distance"
exit
}
if "`type'"=="covratio" { /* restricted to e(sample) */
tempvar hh t
qui _predict double `hh' if `touse', hat `offset'
qui _predict double `t' if `touse', resid `offset'
qui replace `t'=`t'/(e(rmse)*sqrt(1-`hh')) if `touse'
gen `vtyp' `varn' = (( /*
*/(e(N)-e(df_m)-`t'*`t'-1)/(e(N)-e(df_m)-2) /*
*/)^(e(df_m)+1)) / (1-`hh') if `touse'
label var `varn' "Covratio"
exit
}
/* Step 10.
Issue r(198), syntax error.
The user specified more than one option
*/
error 198
end
program define DFbeta /* "`typ'" "`varn'" "`touse'" "`dfbeta'" */
args type newvar touse var
if "`e(wtype)'" != "" {
di in red "not possible after weighted regression"
exit 398
}
capture local beta=_b[`var']
if _rc {
di in red "`var' not in model"
exit 398
}
/* obtain names */
tempname b
mat `b' = get(_b)
local rhs : colnames `b'
mat drop `b'
local n : word count `rhs'
tokenize `"`rhs'"'
if "``n''"=="_cons" {
local `n'
}
local rhs "`*'"
/* end obtain names */
local i 1
while "``i''"!="" & "`found'"=="" {
if "``i''"=="`var'" {
local found "yes"
local `i' " "
}
local i=`i'+1
}
local rhsx "`*'"
tempvar HAT RSTU lest RES SRES RESULT
qui _predict double `HAT' if `touse' & e(sample), hat
qui _predict double `RSTU' if `touse' & e(sample), rstud
estimate hold `lest'
capture {
reg `var' `rhsx' if `RSTU'<.
_predict double `RES' if `RSTU'<., res
}
local rc=_rc
estimate unhold `lest'
if `rc' { error `rc' }
quietly gen double `SRES'=sum(`RES'^2)
gen `type' `newvar'=`RSTU'*`RES'/sqrt((1-`HAT')*`SRES'[_N])
label var `newvar' "Dfbeta `var'"
end
program GenScores, rclass
version 9, missing
syntax [anything] [if] [in] [, * ]
marksample touse
_score_spec `anything', `options'
local varn `s(varlist)'
local vtyp `s(typlist)'
tempvar xb
quietly _predict double `xb' if `touse', xb
gen `vtyp' `varn' = `e(depvar)' - `xb'
label var `varn' "Residuals"
return local scorevars `varn'
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -