📄 predictnl.ado
字号:
*! version 1.4.5 01apr2005
program predictnl
version 8.2
global T_nlver : display string(_caller())
global T_nlver "version $T_nlver:"
if "`e(cmd)'" == "" {
error 301
}
if (!has_eprop(b) | !has_eprop(V)) {
error 321
}
is_svy
local is_svy `r(is_svy)'
if "`is_svy'"=="1" {
if "`e(complete)'" == "available" {
di as err /*
*/ `"must run svy command with "complete" option before using this command"'
exit 301
}
if inlist("`e(cmd)'", "svymean", "svytotal", "svyratio") {
di as err `"predictnl not possible after `e(cmd)'"'
exit 301
}
}
if inlist("`e(cmd)'","anova","boxcox") {
di as err "predictnl not available for use after `e(cmd)'"
exit 301
}
local intcomlist `""regress","logistic","logit","qreg","prais","rreg""'
gettoken type 0 : 0, parse(" =")
gettoken newvar 0 : 0, parse(" =")
if "`type'"=="" | "`newvar'"=="" {
error 198
}
if `"`newvar'"'=="=" {
local newvar `"`type'"'
local type : set type
}
else {
gettoken eqsign 0 : 0, parse(" =")
if `"`eqsign'"' != "=" {
error cond("`eqsign'"=="",198,103)
}
}
if !inlist("`type'","byte","int","long","float","double") {
di as err `"`type' is not a a valid numerical data type."'
exit 198
}
confirm new variable `newvar'
syntax anything(id="expression" name=predexp) [if] [in] /*
*/ [, Level(cilevel) /*
*/ se(passthru) VARiance(passthru) Wald(passthru) /*
*/ p(passthru) ci(passthru) g(string) FORCE /*
*/ ITERate(integer 100)]
marksample touse
CheckOpts, `se' `variance' `wald' `p' `ci'
local se `s(se)'
local var `s(var)'
local wald `s(wald)'
local p `s(p)'
local ci1 `s(ci1)'
local ci2 `s(ci2)'
local stub `g'
if `"`stub'"'!="" {
local wc : word count `stub'
if `wc' > 1 {
di as error "Only one stubname allowed in g()"
exit 198
}
}
local needvar "`se'`var'`wald'`p'`ci1'`ci2'"
/* Define variables that hold derivatives */
tempname b V oldest
matrix `b' = e(b)
local k = colsof(`b')
if `"`needvar'"'!="" | `"`stub'"'!="" {
if `"`stub'"'!="" {
forvalues i = 1/`k' {
local Gvars `Gvars' `stub'`i'
}
confirm new variable `Gvars'
}
else {
forvalues i = 1/`k' {
tempvar G`i'
local Gvars `Gvars' `G`i''
}
}
}
confirm new variable `newvar' `se' `var' /*
*/ `wald' `p' `ci1' `ci2' `Gvars'
/* Dry run: determine the number of tempvar's required */
ParseExp `"`predexp'"' 1 `touse'
local nvar `r(N)'
forvalues i = 1/`nvar' {
tempvar pred`i'
local predvars `predvars' `pred`i''
}
tempvar fbeta variance
local command "`e(cmd)'"
_estimates hold `oldest', copy
capture noisily nobreak {
if !inlist("`e(cmd)'",`intcomlist') {
PostI
}
/* Final pass through predexp */
ParseExp `"`predexp'"' 0 `touse' `predvars'
local predexp `"`r(exp)'"'
forvalues i = 1/`nvar' {
local predcoms `"`predcoms' `"`r(pred`i')'"'"'
}
if inlist("`command'","ologit","oprobit","tobit","cnreg") ///
& missing(e(version)) {
FixExp `"`predexp'"' `command'
local predexp `"`r(exp)'"'
}
ExpEval `fbeta' `"`predexp'"' `touse' /*
*/ "`predvars'" `predcoms'
if "`needvar'"!="" | `"`stub'"'!="" {
if inlist("`e(cmd)'",`intcomlist') {
PostI
}
CheckBeta `fbeta' `"`predexp'"' `touse' /*
*/ "`predvars'" `"`predcoms'"' `force'
FillG "`Gvars'" `"`predexp'"' /*
*/ `fbeta' `touse' "`predvars'" `"`predcoms'"' /*
*/ `iterate'
if "`needvar'"!="" {
mat `V' = e(V)
VarCalc `variance' = "`Gvars'" `V' `touse'
cap assert `variance'==0 if `touse'
if !_rc {
di as txt /*
*/ "Warning: prediction doesn't vary with respect to e(b)."
}
}
}
}
_estimates unhold `oldest'
if _rc {
exit _rc
}
gen `type' `newvar' = `fbeta' if `touse'
label var `newvar' "Prediction"
qui summ `newvar' if `touse', meanonly
if r(min) == r(max) {
di as txt /*
*/ "Warning: prediction constant over observations; " _c
di as txt /*
*/ "perhaps you meant to run {help nlcom##|_new:nlcom}."
}
if "`var'"!="" {
qui gen `type' `var' = `variance' if `touse'
label var `var' "Variance: `newvar'"
}
if "`se'"!="" {
qui gen `type' `se' = sqrt(`variance') if `touse'
label var `se' "Standard error: `newvar'"
}
if "`wald'`p'`ci1'" != "" {
tempvar ww
qui gen double `ww' = cond(`variance'==0, . , /*
*/ `fbeta'^2/`variance') if `touse'
}
if "`p'`ci1'" != "" { /* get the distribution of Wald */
tempname df_r
DenomDOF
scalar `df_r' = r(df_r)
if `df_r' == . {
local testtype chi2
}
else {
local testtype F
}
}
if "`wald'"!="" {
qui gen `type' `wald' = `ww' if `touse'
label var `wald' "Wald test statistic: `newvar'"
}
if "`p'"!="" {
di as txt /*
*/ "note: significance levels are with respect to the " _c
if "`testtype'" == "chi2" {
qui gen `type' `p' = chi2tail(1, `ww') if `touse'
di as txt "chi-squared({res:1}) distribution"
}
else { /* F distribution */
qui gen `type' `p' = Ftail(1, `df_r', `ww') if `touse'
di as txt /*
*/ "F({res:1}," as res scalar(`df_r') as txt ") distribution."
}
label var `p' "Level of significance: `newvar'"
}
if "`ci1'"!="" {
tempname zs
di as txt "note: Confidence intervals calculated using " _c
if "`testtype'" == "chi2" {
di as txt "Z critical values"
scalar `zs' = invnorm((100+`level')/200)
}
else { /* F distribution */
di as txt "t(" as res `df_r' as txt ") critical values."
scalar `zs' = invttail(scalar(`df_r'), /*
*/ (100-`level')/200)
}
qui gen `type' `ci1' = `fbeta' - /*
*/ `zs'*sqrt(`variance') if `touse'
qui gen `type' `ci2' = `fbeta' + /*
*/ `zs'*sqrt(`variance') if `touse'
label var `ci1' `"`=strsubdp("`level'")'% lower bound: `newvar'"'
label var `ci2' `"`=strsubdp("`level'")'% upper bound: `newvar'"'
}
mac drop T_nlver
end
program ExpEval
args X exp touse predvars
capture drop `predvars'
mac shift 4
while `"`1'"' != "" {
`1', if `touse'
mac shift
}
qui gen double `X' = `exp' if `touse'
end
program ParseExp, rclass
args exp count touse
mac shift 3
local i 0
if `count' {
local dont *
}
while `"`exp'"' != "" {
gettoken check exp: exp, /*
*/ parse(`" "`'~:;,<>\/?!@|#$%^&*()-+=[]{}"') quotes
if `"`check'"' != "predict" & `"`check'"' != "xb" {
`dont'local myexp `"`myexp'`check'"'
continue
}
gettoken paren : exp, parse(" (")
if `"`paren'"' != "(" { /* False alarm */
`dont'local myexp `"`myexp'`check'"'
continue
}
local i = `i' + 1
if !`count' {
gettoken args exp: exp, match(par)
if `"`check'"' == "xb" {
local args : subinstr local args " " "", all
local eqopt
if `"`args'"'!="" {
local eqopt eq(`args')
}
local term `"xb(`args')"'
local command /*
*/ `"qui Scorebeta `1', `eqopt'"'
}
else { /* check == "predict" */
local term `"predict(`args')"'
local command /*
*/ `"$T_nlver qui predict double `1', `args'"'
}
CheckValid `"`command'"' `"`term'"' `touse' `1'
return local pred`i' `"`command'"'
local myexp `"`myexp'`1'"'
mac shift
}
}
`dont'return local exp `"`myexp'"'
return scalar N = `i'
end
program VarCalc /* Var = G V */
args var equals G V touse
tempname jj V2
tempvar jji
mat `V2' = `V'
mat rownames `V2' = `G'
mat colnames `V2' = `G'
mat rownames `V2' = _:
mat colnames `V2' = _:
qui gen double `var' = 0 if `touse'
tokenize `G'
local i 1
while "``i''" != "" {
capture confirm variable ``i''
if !_rc {
mat `jj' = (`V2'[1...,`i'])'
capture drop `jji'
mat score double `jji' = `jj' if `touse', forcezero
qui replace `var' = `var' + `jji'*``i'' if `touse'
}
local i = `i' + 1
}
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -