📄 test.ado
字号:
*! version 2.0.1 07oct2004
program test
// no version here
if "`e(cmd)'" == "anova" | "`e(cmd)'" == "manova" {
// no version here
AnovaTestChooser `0'
exit
}
else if _caller() < 8 | replay() {
// no version here
// get to internal handler for speed
_test `0'
exit
}
else {
version 8
WaldTest `0'
exit
}
end
program WaldTest, rclass
// prepare for svy
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
}
}
// parse input
// parse off specifications till comma
// multiple specs are necessarily parenthesized.
//
// -anything- cannot be used due to possible syntax of test
// test exp=exp
gettoken tok : 0, parse(" (,")
local single single
local zero `"`0'"'
if `"`tok'"' == "(" { /* eqns within parens */
local single
while `"`tok'"' == "(" {
gettoken tok 0 : 0, parse(" (,") match(paren)
local eqns `"`eqns' `"`tok'"'"'
gettoken tok : 0, parse(" (,")
}
if `"`tok'"' != "," & `"`tok'"' != "" {
local single single
}
}
if "`single'"=="single" { /* eqn without parens */
local eqns
local 0 `"`zero'"'
while `"`tok'"' != "" & `"`tok'"' != "," {
gettoken tok 0 : 0, parse(" ,")
local eqns `"`eqns' `tok'"'
gettoken tok : 0, parse(" ,")
}
local eqns `"`"`eqns'"'"'
}
syntax [, Accumulate noSVYadjust common CONStant MINimum ///
Mtest Mtest2(passthru) noTest COEF matvlc(passthru) ]
if `"`mtest'`mtest2'"' != "" {
if inlist("`e(cmd)'", "svymean", "svytotal", "svyratio") {
di as err `"mtest option not allowed after `e(cmd)'"'
exit 301
}
_mtest syntax, `mtest' `mtest2'
local mtmethod `r(method)'
}
if "`mtmethod'" != "" {
if "`test'" != "" {
di as err /*
*/ "options mtest and notest may not be combined"
exit 198
}
if "`accumulate'" != "" {
di as err /*
*/ "options mtest and accumulate may not be combined"
exit 198
}
}
if !`is_svy' & "`svyadjust'" != "" {
di as err "option nosvyadjust is only allowed with svy data"
exit 198
}
local svy_adjust = `is_svy' & "`svyadjust'" == ""
if `svy_adjust' & missing(e(df_r)) ///
& (missing(e(N_psu)) | missing(e(N_strata))) {
di as err "impossible to adjust p-values"
exit 198
}
if `is_svy' & `"`test'"' == "" {
if "`svyadjust'" == "" {
di _n as txt "Adjusted Wald test"
}
else di _n as txt "Unadjusted Wald test"
}
// bring constraints into expected format
// check for triple =
local tmp : subinstr local eqns "===" "", all count(local nch)
if `nch' > 0 {
di as err "=== invalid operator"
exit 198
}
// translate == into =
local eqns : subinstr local eqns "==" "=", all
// expand multiple-equality constraint (a=b=c) syntax into
// single equality constraint syntax (a=b) (a=c)
ExpandExpns `"`eqns'"' "`common'" "`constant'"
local explist `"`r(explist)'"'
local nexp `r(nexp)'
// simultaneous test
local acc `accumulate'
foreach exp of local explist {
quietly _test `exp' , `acc' `common' `constant' notest
local acc acc
}
if "`test'" == "notest" {
_test , notest
exit
}
quietly _test , `matvlc'
// save rclass stuff
if r(df_r) < . {
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)
}
else {
local testtype chi2
tempname chi2 df p drop
scalar `chi2' = r(chi2)
scalar `df' = r(df)
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
}
if `svy_adjust' {
if !missing(e(df_r)) {
// replication methods post missing values for one or
// both of e(N_psu), e(N_strata)
local svy_d = e(df_r)
}
else local svy_d = e(N_psu)-e(N_strata)
if missing(`svy_d') {
di as err "internal error in adjustment for svy"
exit 10001
}
if "`testtype'" == "F" {
scalar `F' = ((`svy_d'-`df'+1)/`svy_d') * `F'
}
else {
tempname F df_r
scalar `F' = ((`svy_d'-`df'+1)/(`svy_d'*`df')) * `chi2'
}
scalar `df_r' = `svy_d' - `df' + 1
scalar `p' = Ftail(`df', `df_r', `F')
local testype F
}
// multiple testing -- 1 df
if "`mtmethod'" != "" {
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
if `svy_adjust' {
mat `tm'[`it',3] = Ftail(1,`svy_d',`tm'[`it',1])
}
else if "`testtype'" == "chi2" {
mat `tm'[`it',3] = chi2tail(1,`tm'[`it',1])
}
else {
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'" == "" {
/* better looking output
di
if "`testtype'" == "chi2" {
di as txt "{ralign 20:Wald chi2({res:`=`df''})} = " ///
as res %8.2f `chi2'
}
else {
di as txt "{ralign 20:Wald F({res:`=`df''}, {res:`=`df_r''})} = " ///
as res %8.2f `F'
}
di "{txt}{ralign 20:Prob > `testtype'} = {res}" %8.4f `p'
*/
di
if "`testtype'" == "chi2" {
di as txt _col(12) "chi2(" %3.0f `df' ") =" /*
*/ as res %8.2f `chi2'
di as txt _col(10) "Prob > chi2 = " as res %8.4f `p'
}
else {
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')
if "`testype'" == "F" {
local d "F(df,`=`df_r'')"
}
else local d chi2
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'" "#"
}
if "`coef'" != "" {
Table
}
// return results in r()
return scalar p = `p'
return scalar df = `df'
if "`testtype'" == "F" {
return scalar F = `F'
return scalar df_r = `df_r'
}
else return scalar chi2 = `chi2'
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
program AnovaTestChooser
if replay() { // meaning no arguments before the comma
capture syntax , Symbolic
if _rc == 0 { // test , symbolic
// no version here
_test `0'
exit
}
capture syntax , SHOWORDer
if _rc == 0 { // test , showorder
// born() checks executable is recent enough
version 8.1 , born(01aug2003)
anovadef , showorder
exit
}
capture syntax , TEST(passthru) [ * ]
if _rc == 0 { // test , test() ...
version 8
AnovaTest `0'
exit
}
// replay on a test after [m]anova
// no version here
_test `0'
exit
}
else {
// no version here
// get to internal handler for speed
_test `0'
exit
}
end
program AnovaTest, rclass
// test , test(matname) [ mtest[(method)] matvlc(matname) ]
syntax , TEST(name) [ Mtest Mtest2(passthru) matvlc(passthru) ]
confirm matrix `test'
tempname tmat
// append column of zeros to matrix in prep for sending to mat_put_rr
mat `tmat' = `test' , J(rowsof(`test'),1,0)
mat_put_rr `tmat'
if `"`mtest'`mtest2'"' == "" {
// no multiple comparisons to worry about
// just use replay feature of test
_test , `matvlc'
ret add
exit
}
// need to handle multiple comparisons testing
_mtest syntax, `mtest' `mtest2'
local mtmethod `r(method)'
// do the simultaneous test
quietly _test , `matvlc'
// save rclass items for simultaneous test
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -