📄 lrtest.ado
字号:
}
if "`remlnote'" != "" {
if "`conservative'" == "" {
di
}
di as txt "{p 0 6 2}Note: LR tests based on REML are valid "
di as txt "only when the fixed-effects specification is identical for "
di as txt "both models{p_end}"
}
if "`stats'" != "" {
est stats `restricted' `unrestricted'
}
if "`dir'" != "" {
est dir `restricted' `unrestricted' , width(78)
}
end
// ----------------------------------------------------------------------------
// subroutines
// ----------------------------------------------------------------------------
/* GetInfo
returns in r()
N number of obs
df df of model (rank e(V))
ll log-likelihood of model
ll_0 log-likelihood of null-model
With composite specifications, these values are summed over all model
in the namelist.
*/
program define GetInfo, rclass
args namelist Qforce
tempname V rank N df ll ll_0 hcurrent esample num_ce
scalar `N' = 0
scalar `df' = 0
scalar `ll' = 0
scalar `ll_0' = 0
_est hold `hcurrent' , restore nullok estsystem
foreach name of local namelist {
nobreak {
if "`name'" != "." {
est_unhold `name' `esample'
}
else _est unhold `hcurrent'
capture noisily {
if !`Qforce' {
if "`e(vcetype)'" == "Robust" {
di as err ///
"LR test likely invalid for models with robust vce"
exit 498
}
if "`e(clustvar)'" != "" {
di as err ///
"LR test likely invalid with cluster(`e(clustvar)')"
exit 498
}
if "`e(wtype)'" == "pweight" {
di as err ///
"LR test invalid in case of pweighted estimators"
exit 498
}
}
if "`e(cmd)'" == "vec" {
local n_names: word count `namelist'
if `n_names' > 1 {
di as err ///
"composite models not " ///
"after {cmd:vec}"
exit 498
}
scalar `rank' = e(k_rank)
if `rank' >= . {
di as err ///
"number of estimated " ///
"parameters not " ///
"available for model " ///
"`name'"
exit 498
}
scalar `num_ce' = e(k_ce)
}
else {
scalar `rank' = e(rank)
scalar `num_ce' = .
}
tempname k_f k_rc k_rs
if "`e(cmd)'" == "xtmixed" {
local n_names: word count `namelist'
if `n_names' > 1 {
di as err ///
"composite models not " ///
"allowed after {cmd:xtmixed}"
exit 498
}
scalar `k_f' = e(k_f)
scalar `k_rc' = e(k_rc)
scalar `k_rs' = e(k_rs)
scalar `rank' = e(k)
local mixed_method `e(method)'
}
else {
scalar `k_f' = .
scalar `k_rc' = .
scalar `k_rs' = .
}
if `rank' == . {
// 24mar2003 confirm matrix e(V) does not work !
capt mat `V' = syminv(e(V))
if _rc == 111 {
dis as txt "(`name' does not contain matrix e(V); rank = 0 assumed)"
scalar `rank' = 0
}
else if _rc != 0 {
// produce error message
mat `V' = syminv(e(V))
}
else {
scalar `rank' = rowsof(`V') - diag0cnt(`V')
if `rank' == 0 {
dis as err "(`name' has e(V) with rank = 0)"
// exit 498
}
}
}
capt confirm scalar e(ll)
if _rc {
di as err "`name' does not contain scalar e(ll)"
exit 498
}
if e(ll) == . {
di as err "`name': e(ll) is missing"
exit 498
}
scalar `N' = `N' + e(N)
scalar `df' = `df' + `rank'
scalar `ll' = `ll' + e(ll)
scalar `ll_0' = `ll_0' + e(ll_0)
local cmd `cmd' `e(cmd)'
local depvar `depvar' `e(depvar)'
}
local rc = _rc
if "`name'" != "." {
est_hold `name' `esample'
}
else _est hold `hcurrent', restore nullok estsystem
}
if `rc' {
exit `rc'
}
}
return local cmd `cmd'
return local depvar `depvar'
return scalar N = `N'
return scalar df = `df'
return scalar ll = `ll'
return scalar ll_0 = `ll_0'
return scalar num_ce = `num_ce'
return scalar k_f = `k_f'
return scalar k_rc = `k_rc'
return scalar k_rs = `k_rs'
return local mixed_method `mixed_method'
end
/* ParseSpecTest cmdline
parses the specifications of the restricted and unrestricted models,
returning the expanded lists in r(model1) and r(model2).
The rest of cmdline is returned in r(rest).
*/
program define ParseSpecTest, rclass
args cmdline
// get specification of model 1 (unrestricted model)
gettoken model1 cmdline : cmdline , parse(" ,") match(parens)
if inlist(`"`model1'"', "", ",") {
di as err "name(s) of estimation results expected"
exit 198
}
est_expand "`model1'"
local model1 `r(names)'
local nmodel1 : word count `model1'
// get specification of model 2 (unrestricted model)
gettoken model2 : cmdline , parse(" ,") match(parens)
if inlist(`"`model2'"', "", ",") {
// model2 not specified; default to . (last est results)
if "`e(cmd)'" != "" {
local model2 .
}
else {
di as err "last estimates not found"
di as err "second specification expected"
error 301
}
}
else {
gettoken model2 cmdline : cmdline , parse(" ,") match(parens)
}
est_expand "`model2'"
local model2 `r(names)'
local nmodel2 : word count `model2'
// check for "overlap" between models
local overlap : list model1 & model2
if "`overlap'" != "" {
di as err "models `overlap' specified more than once"
exit 198
}
return local rest `cmdline'
return local model1 `model1'
return local model2 `model2'
end
/* OldSyntax
returns in r(newsyntax) a command line in the new syntax.
The option -saving()- is implemented via -estimates store-.
*/
program define OldSyntax, rclass
args caller cmdline
local 0 `"`cmdline'"'
syntax [, Saving(string) Using(string) Model(string) Df(passthru) FORCE]
if `"`saving'"' != "" {
/*
store with a prefix LRTEST_
lrtest did not enforce that the names were valid identifiers
lrtest used to return rc=301 if N, e(ll), or df were missing
we mimick this behavior here.
*/
capture confirm integer number `e(N)'
if _rc {
di as err "`e(cmd)' does not store e(N)"
exit 301
}
capture confirm number `e(ll)'
if _rc {
di as err "`e(cmd)' does not store e(ll)"
exit 301
}
capture confirm matrix e(V)
if _rc {
di as err "`e(cmd)' does not store e(V)"
exit 301
}
est store LRTEST_`saving', title(saved by lrtest--old syntax)
if `"`using'`model'"' == "" {
exit
}
}
// using() : -unrestricted- ; defaults to 0 (stored as LRTEST_0)
// model() : -restricted- ; defaults to . (last estimation result)
if "`using'" == "" {
capt est_cfexist LRTEST_0
if _rc {
di as err "{p}" ///
"In the old syntax, the unrestricted model defaulted " ///
"to a model saved under the name 0. This model was not " ///
"found.{p_end}"
exit 198
}
local using LRTEST_0
}
else {
local using LRTEST_`using'
}
if "`model'" == "" {
if "`e(cmd)'" == "" {
di as err "{p}"
"In the old syntax, the restricted model defaulted to " ///
"the last estimation results. These were not found." ///
"{p_end}"
exit 198
}
local model .
}
else {
local model LRTEST_`model'
}
return local newsyntax `"`using' `model' , `force' `df'"'
if `caller' > 7 {
di "{p}" as txt
di "You ran lrtest using the old syntax. "
di "Click {help lrtest##|_new:here} to learn about the new syntax."
di "{p_end}" _n
}
end
exit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -