📄 test.ado
字号:
local testtype F
tempname F df df_r p drop
scalar `F' = r(F)
scalar `df' = r(df)
scalar `df_r' = r(df_r)
scalar `p' = r(p)
scalar `drop' = r(drop)
// find out which constraints (if any) were dropped
local i 1
while "`r(dropped_`i')'" != "" {
local dropped_`i' `r(dropped_`i')'
local ++i
}
// now do multiple testing
tempname R r Rr V b VR tm Wald
mat `V' = e(V)
mat `b' = e(b)
mat `Rr' = get(Rr)
local nb = rowsof(`V')
local nr = rowsof(`Rr')
mat `R' = `Rr'[1...,1..`nb']
mat `VR' = `R'*`V'*`R''
mat `r' = `R'*`b'' - `Rr'[1...,`=`nb'+1']
mat `Wald' = `r''*syminv(`VR')*`r'
mat `tm' = J(`nr', 3, .)
mat colnames `tm' = `testtype' df p
forvalues it = 1 / `nr' {
mat `Wald' = `r'[`it',1]^2 / `VR'[`it',`it']
mat `tm'[`it',1] = `Wald'[1,1]
mat `tm'[`it',2] = 1
mat `tm'[`it',3] = Ftail(1,`df_r',`tm'[`it',1])
}
if "`mtmethod'" != "noadjust" {
_mtest adjust `tm', mtest(`mtmethod') pindex(3) append
mat `tm' = r(result)
local pindex 4
}
else local pindex 3
// Display results
// invoke _test to display conditions
// test is displayed later
_test, notest
// now display dropped constraints (if any)
local i 1
while "`dropped_`i''" != "" {
di as txt " Constraint `dropped_`i'' dropped"
local ++i
}
if "`mtmethod'" == "" {
di
di as txt /*
*/ " F(" %3.0f `df' "," %6.0f `df_r' ") =" /*
*/ as res %8.2f `F'
di as txt _col(13) "Prob > F =" as res %10.4f `p'
}
else {
local nexp = rowsof(`tm')
local nc = colsof(`tm')
local d "F(df,`=`df_r'')"
di
di as txt "{hline 7}{c TT}{hline 31}"
di as txt " {c |}{ralign 12:`d'} df p"
di as txt "{hline 7}{c +}{hline 31}"
forvalues it = 1 / `nexp' {
di as txt " (`it'){col 8}{c |}" as res ///
_col(12) %9.2f `tm'[`it',1] ///
_col(22) %6.0f `tm'[`it',2] ///
_col(33) %6.4f `tm'[`it',`pindex'] ///
as txt " #"
}
di as txt "{hline 7}{c +}{hline 31}"
di as txt " all {c |}" as res ///
_col(12) %9.2f ``testtype'' ///
_col(22) %6.0f `df' ///
_col(33) %6.4f `p'
di as txt "{hline 7}{c BT}{hline 31}"
_mtest footer 39 "`mtmethod'" "#"
}
// return results in r()
return scalar p = `p'
return scalar df = `df'
return scalar F = `F'
return scalar df_r = `df_r'
return scalar drop = `drop'
local i 1
while "`dropped_`i''" != "" {
return scalar dropped_`i' = `dropped_`i''
local ++i
}
if "`mtmethod'" != "" {
return matrix mtest `tm'
return local mtmethod `mtmethod'
}
end
// ===========================================================================
// subroutines
// ===========================================================================
/* ExpandExpns quoted-explist common
returns in
r(explist) the lists of quoted-equations,
r(nexp) the number of equations
*/
program ExpandExpns, rclass
args expns common constant
gettoken exp expns : expns
while `"`exp'"' != "" {
Expand `"`exp'"' "`common'" "`constant'"
local explist `"`explist' `r(explist)'"'
local nexp = `nexp' + `r(nexp)'
gettoken exp expns : expns
}
return local explist `"`explist'"'
return local nexp `nexp'
// di as err `"Expanded explist: `explist'"'
end
/* Expand multi-condition syntax (e.g., a=b=c)
syntax 1: coefficient list
returns unchanged input
syntax 2: exp1=exp2 [=exp3]
translated into
exp1=exp2 exp1=exp3 ...
beware: expi may be of form [eq]coef
syntax 3: [eq1=eq2=eq3..] ..
translated into
[eq1=eq2] .. [eq1=eq3] .. etc if common not defined
[eq1=eq2] .. [eq1=eq3] .. [eq2=eq3] .. if common defined
returns in
r(nexp) the number of conditions/equations
r(explist) list of quoted conditions/expressions
note: this command does not test that expressions are well-formed, or
that eq is indeed an equation name.
*/
program define Expand, rclass
args exp common constant
// if at most one equal sign, no expansion is necessary
local junk : subinstr local exp "=" "=", count(local nch) all
if `nch' <= 1 {
return local explist `"`"`exp'"'"'
return local nexp 1
exit
}
// determine syntax type
tokenize `"`exp'"', parse(" []:=")
if `"`1'"' == "[" & `"`3'"' == "=" {
/* syntax should be:
[eq=eq=..]
optionally followed by a : varlist */
local eqnames `"`2'"'
local neq 1
mac shift 2
while `"`1'"' == "=" {
local ++neq
local eqnames `"`eqnames' `2'"'
mac shift 2
}
if "`1'" != "]" {
di as err `"] expected, `1' found"'
exit 198
}
mac shift
if "`1'" != "" {
if `"`1'"' != ":" {
di as err `": expected, `colon' found"'
exit 198
}
mac shift
// unab vlist : `*'
// local vlist `":`vlist'"'
local vlist `":`*'"'
}
if `neq' < 2 {
/* NOTREACHED */
di as err "too few equations specified"
exit 198
}
if "`common'" != "" {
if `"`vlist'"' != "" {
di as err ///
"option common may not be combined with syntax [eq...]:varlist"
exit 198
}
// cleanup up eqnames names of form #<nnn>
CleanUp "`eqnames'"
local eqnames `r(eqnames)'
CommonVar "`eqnames'" "`constant'"
if "`r(varlist)'" == "" {
di as err "no coefficients in common"
exit 198
}
local vlist `":`r(varlist)'"'
}
// test all equations against equation 1
gettoken eq1 eqrest : eqnames
foreach eq of local eqrest {
local explist `"`explist' `"[`eq1'=`eq']`vlist'"'"'
}
local nexp = `neq'-1
}
else {
// syntax should be exp1=exp2=...=expk
// we do not check at this point that expi is a valid expression
tokenize `"`exp'"', parse("=")
local exp1 `"`1'"'
mac shift
local nexp 0
while "`1'" != "" {
if "`1'" != "=" {
di as err `"= expected, `1' found"'
exit 198
}
if "`2'" == "" {
di as err ///
"nothing found where expression expected"
exit 198
}
local ++nexp
local explist `"`explist' `"`exp1'=`2'"'"'
mac shift 2
}
}
return local explist `"`explist'"'
return local nexp `nexp'
end
/* CommonVar eqnames constant
returns the variables common in the equations in eqnames in e(b)
if "constant" is empty, _cons is removed
*/
program CommonVar, rclass
args eqnames constant
tempname b beq
mat `b' = e(b)
local neq 0
foreach eq of local eqnames {
// triggers error message if -eq- not found
mat `beq' = `b'[1,"`eq':"]
local names : colnames `beq'
local ++neq
if `neq' == 1 {
local varlist `names'
continue
}
// remove vars from varlist not found in names
local myvars `"`varlist'"'
foreach v of local myvars {
local tmp : subinstr local names "`v'" "", /*
*/ word all count(local nch)
if `nch' == 0 {
local varlist : subinstr local varlist /*
*/ "`v'" "", word all
}
}
if "`varlist'" == "" {
exit
}
}
// remove _cons
if "`constant'" == "" {
local varlist : subinstr local varlist "_cons" "", word all
}
return local varlist `varlist'
end
/*
Drop all duplicate tokens from list
*/
program DropDup
args newlist /// name of macro to store new list
colon /// ":"
list // list with possible duplicates
gettoken token list : list
while "`token'" != "" {
local fixlist `fixlist' `token'
local list : subinstr local list "`token'" "", word all
gettoken token list : list
}
c_local `newlist' `fixlist'
end
program CleanUp, rclass
args eqnames
assert "`eqnames'" != ""
local tmp : subinstr local eqnames "#" "", count(local nch)
if `nch' == 0 {
// nothing to cleanup
return local eqnames `eqnames'
exit
}
tempname b
mat `b' = e(b)
local coleq : coleq `b'
DropDup coleq : "`coleq'"
tokenize `coleq'
foreach eq of local eqnames {
if substr("`eq'",1,1) == "#" {
local eqn = substr("`eq'",2,.)
capt confirm number `eqn'
if _rc {
di as err "equation `eq' not found"
exit 303
}
if "``eqn''" == "" {
di as err "equation `eq' not found"
exit 303
}
local eq ``eqn''
}
local neweq `neweq' `eq'
}
return local eqnames `neweq'
end
program Table, eclass
tempname est R r Rr V b br VR
mat `V' = e(V)
mat `b' = e(b)
mat `Rr' = get(Rr)
loc nb = rowsof(`V')
mat `R' = `Rr'[1..., 1..`nb']
mat `r' = `R'*`b'' - `Rr'[1..., `=`nb'+1']
mat `VR' = syminv(`R' * `V' * `R'')
// mat `Wald' = `r'' * `VR' * `r'
mat `VR' = `VR' * `R' * `V'
// constrained estimator
mat `br' = `b' - `r'' * `VR'
// variance of constrained estimator
mat `VR' = `V' - `V' * `R'' * `VR'
local vct `e(vcetype)'
local cl `e(clustvar)'
_estimates hold `est', restore
ereturn post `br' `VR'
ereturn local vcetype `vct'
ereturn local clustvar `cl'
di _n(2) as txt "Constrained coefficients" _n
ereturn display
end
exit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -