📄 hettest.ado
字号:
*! version 3.2.4 01may2002
program define hettest, rclass
version 8
syntax [varlist(default=none numeric)] [, Rhs Mtest Mtest2(passthru)]
_isfit cons anovaok
if "`e(wtype)'" == "pweight" {
di as err "hettest not appropriate with pweighted data"
//di as err "use testom instead"
exit 101
}
if "`e(vcetype)'" == "Robust" {
di as err "hettest not appropriate after robust cluster()"
//di as err "use testom instead"
exit 498
}
// check that mtest option has valid value
// store the unabbreviated value in local method
if `"`mtest'`mtest2''"' != "" {
_mtest syntax, `mtest' `mtest2'
local mtmethod `r(method)'
local mtest
local mtest2
}
// add rhs-variables (except _cons) to varlist
if "`rhs'" != "" {
tempname b
mat `b' = e(b)
local v : colnames(`b')
local v : subinstr local v "_cons" "", word
local varlist `varlist' `v'
}
DropDup varlist : "`varlist'"
local nvar : word count `varlist'
// check that depvar is not in varlist
local tmp : subinstr local varlist "`e(depvar)'" "", ///
word all count(local nch)
if `nch' == 1 {
if `nvar' == 1 {
local varlist `e(depvar)'
}
else {
di in err `"`e(depvar)' may not be specified"'
exit 499
}
}
// generate residuals and predicted values
// ---------------------------------------
tempvar ui touse
tempname oldest
if "`e(wtype)'" != "" {
local wgt "[`e(wtype)' `e(wexp)']"
}
gen byte `touse' = e(sample)
qui _predict double `ui' if `touse', resid
qui replace `ui' = `ui'^2 / (e(rss)/e(N)) // ui = squared res
if "`varlist'" == "" { // use fitted values
tempvar z
qui _predict double `z' if `touse'
local varlist `z'
local tvarlist "fitted values of `e(depvar)'"
}
else {
local tvarlist `varlist'
}
local nvar: word count `varlist'
if `nvar' == 1 {
local mtmethod
}
// compute tests
// -------------
_est hold `oldest', restore
// multivariate test, stored in 3 scalars
quiet reg `ui' `varlist' `wgt' if `touse'
return scalar chi2 = e(mss)/2
return scalar df = e(df_m)
return scalar p = chi2tail(e(df_m),e(mss)/2)
if "`mtmethod'" != "" {
// multiple tests are stored in nx3 matrix mtest
// column 1: test statistic
// column 2: df = 1
// column 3: chi2-based p-value
tempname mtest
mat `mtest' = J(`nvar',3,0)
matrix colnames `mtest' = chi2 df p
matrix rownames `mtest' = `varlist'
local i 0
foreach v of local varlist {
local ++i
qui reg `ui' `v' `wgt' if `touse'
mat `mtest'[`i',1] = e(mss)/2
mat `mtest'[`i',2] = e(df_m)
mat `mtest'[`i',3] = chi2tail(e(df_m),e(mss)/2)
}
// obtain adjusted p-values using _mtest
if "`mtmethod'" != "noadjust" {
_mtest adjust `mtest', mtest(`mtmethod') pindex(3) append
mat `mtest' = r(result)
local indexp 4
}
else local indexp 3
return matrix mtest `mtest'
return local mtmethod `mtmethod'
}
_est unhold `oldest'
// display tests
// -------------
di _n as txt `"Breusch-Pagan / Cook-Weisberg test for heteroskedasticity `tt'"'
di as txt " Ho: Constant variance"
if "`mtmethod'" != "" { // multiple tests
di
di as txt "{hline 13}{c TT}{hline 25}"
di as txt " Variable {c |} chi2 df p "
di as txt "{hline 13}{c +}{hline 25}"
local i 0
foreach v of local varlist {
local ++i
local vn = abbrev("`v'",12)
di as txt "{ralign 12:`vn'}" ///
_col(14) "{c |} " as res ///
_col(16) %9.2f el(return(mtest),`i',1) ///
_col(24) %5.0f el(return(mtest),`i',2) ///
_col(33) %6.4f el(return(mtest),`i',`indexp') " #"
}
di as txt "{hline 13}{c +}{hline 25}"
di as txt "simultaneous" ///
_col(14) "{c |} " as res ///
_col(16) %9.2f return(chi2) ///
_col(24) %5.0f return(df) ///
_col(33) %6.4f return(p)
di as txt "{hline 13}{c BT}{hline 25}"
_mtest footer 39 "`mtmethod'" "#"
}
else { // only simult test
di as txt "{p 9 18}Variables: `tvarlist'"
di
di as txt _col(10) "chi2(" as res return(df) ///
as txt ")" _col(23) "=" as res %9.2f return(chi2)
di as txt _col(10) "Prob > chi2" _col(23) "=" ///
as res %9.4f return(p)
}
// double save results
// -------------------
global S_3 `return(df)' // degrees of freedom chi2 under H0
global S_5
global S_6 `return(chi2)' // test statistic
end
/* DropDup newlist : list
drops all duplicate tokens from list -- copied from hausman.ado
*/
program define 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
exit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -