📄 estimates.ado
字号:
*! version 1.1.5 13may2005
program estimates
version 8
capture noisily nobreak {
global T_EST_VERSION = string(_caller())
break Work `0'
}
local rc = _rc
global T_EST_VERSION
exit `rc'
end
program Work
gettoken key 0 : 0, parse(" ,")
local lkey = length(`"`key'"')
// valid subcommands
if `"`key'"' == "" {
if "`e(cmd)'" != "" {
Replay "."
}
}
else if `"`key'"' == substr("query", 1, `lkey') {
Query `0'
}
else if `"`key'"' == substr("store", 1, max(3,`lkey')) {
Store `0'
}
else if `"`key'"' == substr("restore", 1, max(3,`lkey')) {
Restore `0'
}
else if `"`key'"' == substr("change", 1, max(2,`lkey')) {
Change `0'
}
else if `"`key'"' == substr("for", 1, `lkey') {
For `0'
}
else if `"`key'"' == substr("dir", 1, `lkey') {
Dir `0'
}
else if `"`key'"' == substr("stats", 1, max(2,`lkey')) {
Stats `0'
}
else if `"`key'"' == substr("table", 1, `lkey') {
// external
est_table`0'
}
else if `"`key'"' == substr("replay", 1, `lkey') {
Replay `0'
}
else if `"`key'"' == "drop" {
Drop `0'
}
else if `"`key'"' == "clear" {
Clear `0'
}
// subcommands of estimation version <= 7
else if `"`key'"' == substr("local", 1, max(3,`lkey')) {
SubcmdMoved "local" "ereturn"
}
else if `"`key'"' == substr("scalar", 1, max(3,`lkey')) {
SubcmdMoved "scalar" "ereturn"
}
else if `"`key'"' == substr("matrix", 1, max(3,`lkey')) {
SubcmdMoved "matrix" "ereturn"
}
else if `"`key'"' == "post" {
SubcmdMoved "post" "ereturn"
}
else if `"`key'"' == "repost" {
SubcmdMoved "repost" "ereturn"
}
else if `"`key'"' == substr("display", 1, max(2,`lkey')) {
SubcmdMoved "display" "ereturn"
}
else if `"`key'"' == substr("hold", 1, `lkey') {
SubcmdMoved "hold" " _estimates"
}
else if `"`key'"' == substr("unhold", 1, `lkey') {
SubcmdMoved "unhold" "_estimates"
}
else if `"`key'"' == substr("list", 1, max(2,`lkey')) {
SubcmdMoved "list" "ereturn"
}
// error
else {
dis as err `"estimates: unknown subcommand "`key'""'
exit 198
}
end
// ============================================================================
// subcommands ("methods")
// ============================================================================
/*
subcmd query
*/
program define Query
syntax
loc name `e(_estimates_name)'
if `"`name'"' != "" {
local cl "{stata estimates replay `name':`name'}"
di as txt "(the active estimation result is `cl')"
if `"`e(_estimates_title)'"' != "" {
di "{p 0 12 2}"
di as txt `"description: {it:`e(_estimates_title)'}"'
di "{p_end}"
}
}
else if `"`e(cmd)'"' != "" {
di as txt "(the active estimation results are not yet stored)"
}
else {
di as txt "(no estimation results)"
}
end
/*
subcmd change name, title() scorevars()
*/
program define Change, eclass
syntax [anything] [, TItle(passthru) SCorevars(passthru)]
est_expand `"`anything'"' , max(1) default(.)
local name `r(names)'
if `"`title'`scorevars'"' == "" {
// nothing to do
exit
}
if !inlist(`"`name'"', ".", `"`e(_estimates_name)'"') {
// change fields in non-active estimation result
tempname hcurrent esample
_est hold `hcurrent', restore nullok estsystem
nobreak {
est_unhold `name' `esample'
capture noisily {
CheckName `name'
ChangeFields, `title' `scorevars'
}
local rc = _rc
est_hold `name' `esample'
}
if `rc' {
exit `rc'
}
}
else if "`name'" == `"`e(_estimates_name)'"' {
// current results already saved; resave!
ChangeFields, `title' `scorevars'
capt _est drop `name'
capt _est hold `name' , copy estimates varname(_est_`name')
}
else {
// now name==.
// not yet saved, just set fields
ChangeFields, `title' `scorevars'
}
end
/*
subcmd restore name [, noheader drop ]
*/
program define Restore
syntax [anything] [, noHeader DROP ]
est_expand `"`anything'"' , min(1) max(1)
local name `r(names)'
if inlist("`name'", ".", `"`e(_estimates_name)'"') {
// requested results already active
if "`header'" == "" & "`name'" != "." {
local cl "{stata estimates replay `name':`name'}"
di as txt "(results `cl' are already active)"
}
}
else {
// activate -name-
if "`drop'" != "" {
_est unhold `name'
}
else {
nobreak {
tempvar esample
est_unhold `name' `esample'
est_hold `name' `esample' "copy"
}
}
if "`header'" == "" {
local cl "{stata estimates replay `name':`name'}"
di as txt "(results `cl' are active now)"
}
}
end
/*
subcmd store name [, title(str) nocopy ]
*/
program define Store, eclass
if "`e(cmd)'" == "" {
di as err "last estimation results not found, nothing to store"
exit 301
}
syntax anything(id=name name=name) [, Title(str) noCOpy ]
confirm name `name'
if `:word count `name'' > 1 {
di as err "single name expected"
exit 198
}
confirm name _est_`name'
// save e(_estimates_*) values to reset if storing fails
local loct `"`e(_estimates_title)'"'
local locn `"`e(_estimates_name)'"'
// _estimates drop does *not* overwrite, -estimates- does..
capture _est drop `name'
capture drop _est_`name'
ereturn local _estimates_title `"`title'"'
ereturn local _estimates_name `name'
if "`copy'" == "" {
local copy_option copy
}
capt _est hold `name', estimates `copy_option' varname(_est_`name')
local rc = _rc
if `rc'==0 {
exit
}
// if we come here, storing has failed
// undo sets locals and display error message
ereturn local _estimates_title `"`loct'"'
ereturn local _estimates_name `"`name'"'
if `rc' == 1000 {
TooManyModels
exit 1000
}
error `rc'
end
/*
subcmd for name-list [, nodrop noheader]: any_cmd
*/
program define For
// parse off the name-list
gettoken tok 0 : 0 , parse(" ,[:")
while !inlist(`"`tok'"', "", ",", ":", "[", "in", "if", "using") {
local names `"`names' `tok'"'
gettoken tok 0 : 0 , parse(" ,[:")
}
if `"`names'"' == "" {
di as err "name-list expected"
exit 198
}
est_expand `"`names'"'
local names `r(names)'
if "`names'" == "" {
exit
}
// parse option specification for -for- subcommand
// store in pecmd the "postestimation" cmdline to be executed
if `"`tok'"' == "," {
gettoken tok 0 : 0 , parse(" :")
while !inlist(`"`tok'"', "", ":") {
local foropts `"`foropts' `tok'"'
gettoken tok 0 : 0 , parse(" :")
}
local pecmd `"`0'"'
local 0 `", `foropts'"'
syntax [, noStop noHeader]
}
else {
local pecmd `"`0'"'
}
local pecmd version $T_EST_VERSION : `pecmd'
if "`tok'" != ":" {
di as err "colon expected"
exit 198
}
local Qheader = ("`header'" == "")
// parsing ready
// get "active only" case out of the way
if inlist("`names'", ".", "`e(_estimates_name)'") {
capt noi `pecmd'
exit _rc
}
// save current estimation results, if any
tempname hcurrent esample
_est hold `hcurrent', restore nullok estsystem
foreach name of local names {
nobreak {
if "`name'" != "." {
est_unhold `name' `esample'
local errlab `name
}
else {
_est unhold `hcurrent' '
local errlab the active results
}
capture break noisily {
if `Qheader' {
Header
}
version $T_EST_VERSION : `pecmd'
}
local rc = _rc
if "`name'" != "."{
est_hold `name' `esample'
}
else {
_est hold `hcurrent', restore nullok estsystem
}
}
if `rc' == 1 {
di as err "--Break--"
}
else if `rc'!=0 & !`Qheader' {
di as err "error executing the post estimation command on `errlab'"
}
if "`stop'" == "" {
exit `rc'
}
} /* foreach */
end
/*
subcmd replay [name-list] [, noheader]
*/
program define Replay
syntax [anything(id=name)] [, noHeader *]
local estopt `options'
local Qheader = ("`header'" == "")
est_expand `"`anything'"' , default(.)
local names `r(names)'
if `"`names'"' == "" {
exit
}
// if inlist("`names'", ".", "`e(_estimates_name)'") {
// if `Qheader' {
// Header `names'
// }
// Replay_u `estopt'
// exit
// }
tempname hcurrent esample
_est hold `hcurrent', restore nullok estsystem
foreach name of local names {
nobreak {
if "`name'" != "." {
est_unhold `name' `esample'
}
else {
_est unhold `hcurrent'
}
capture break nois {
if `Qheader' {
Header
}
Replay_u `estopt'
}
local rc = _rc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -