📄 xtmixed.ado
字号:
qui test [#1]
if `r(drop)' {
ereturn scalar chi2 = .
ereturn scalar p = .
}
else {
ereturn scalar chi2 = r(chi2)
ereturn scalar p = r(p)
}
ereturn scalar df_m = r(df)
}
ereturn local chi2type Wald
ereturn scalar rc = cond(`e(converged)',0,430)
Display, level(`level') `lrtest' ///
`variance' `group' `header' `estmetric' ///
`grouponly' `fetable' `retable'
end
program ParseCmd
gettoken i 0 : 0
// First we strip off if and in - they have already been processed
syntax [anything] [if] [in] [, * ]
local 0 `anything', `options'
local myopts noConstant COVariance(name)
gettoken lev rest : 0 , parse(":")
if `"`lev'"' == `":"' {
local lev _all
local 0 `"`rest'"'
}
else {
gettoken colon rest : rest , parse(":")
if `"`colon'"' == `":"' {
if (`"`lev'"' == `"."') {
local lev _all
}
else if "`lev'" != "_all" {
unab lev : `lev'
confirm variable `lev'
}
local 0 `"`rest'"'
}
else {
if (`i' > 0) local lev _all
else local lev
}
}
capture syntax [varlist(ts numeric default=none)] ///
[ , `myopts' ]
if _rc { // r.varname...
if `i' > 0 {
ChkNonNumeric `"`myopts'"' `0'
syntax [anything] [ , `myopts' ]
if 0`:list sizeof anything' != 1 {
di as error `"`anything' invalid level specification"'
exit 198
}
local anything0 `"`anything'"'
local anything : subinstr local anything "*" " " , all
if 0`:list sizeof anything' == 1 { // r.varname
tokenize `"`anything'"' , parse(".")
if `"`=upper(`"`1'"')'"' != `"R"' | `"`4'"' != `""' {
di as error `"`anything0' invalid random factor"'
exit 198
}
confirm variable `3'
local varlist `3'
}
else { // r.v1*r.v2...
gettoken fvar anything : anything
while "`fvar'" != "" {
tokenize `"`fvar'"' , parse(".")
if `"`=upper(`"`1'"')'"' != `"R"' | `"`4'"' != `""' {
di as error `"`anything0' invalid random factor"'
exit 198
}
confirm variable `3'
local varlist "`varlist' `3'"
gettoken fvar anything : anything
local anything `anything' // sic
}
}
tsunab varlist : `varlist'
}
else {
syntax [varlist(ts numeric default=none)] ///
[ , `myopts' ]
}
local isfctr 1
}
else local isfctr 0
ParseCov cov : `isfctr' , `covariance'
if !0`isfctr' & "`cov'" != "identity" & ///
`:list sizeof varlist' + ("`constant'"=="") == 1 {
if `"`covariance'"' != `""' {
SingleLevelCovNote `lev' `varlist'
}
local cov identity
}
if `i' == 0 { // parsing fixed level
if `"`lev'"' != "" {
di as err "{p 0 4 2}level specification not allowed "
di as err "for fixed equation{p_end}"
exit 198
}
gettoken depname varlist : varlist
c_local depname `depname'
local cov identity
if "`covariance'" != "" {
di as error ///
"option covariance() not allowed in fixed varlist"
exit 198
}
}
c_local levnm_`i' `lev'
c_local cov_`i' `cov'
c_local isfctr_`i' `isfctr'
c_local constant_`i' = cond(`isfctr', "noconstant", "`constant'")
c_local varnames_`i' `varlist'
end
program ParseCov
gettoken lmac 0 : 0
gettoken colon 0 : 0
gettoken isfctr 0 : 0
syntax [ , INDependent EXchangeable IDentity UNstructured * ]
if `"`options'"' != "" {
di as error `"`options' invalid covariance structure"'
exit 198
}
local res `independent' `exchangeable' `identity' `unstructured'
if 0`isfctr' & "`independent'`unstructured'" != "" {
di as error ///
"`independent'`unstructured' covariance structure not allowed for factor variables"
exit 198
}
if ("`res'" == "") {
if (0`isfctr') local res "identity"
else local res "independent"
}
c_local `lmac' `res'
end
program RMcolLevelFake
gettoken beg 0 : 0
gettoken end 0 : 0
gettoken depvar 0 : 0
gettoken constant 0 : 0
gettoken isfctrs 0 : 0
gettoken touse 0 : 0
forvalues s = `beg'/`end' {
gettoken vlist 0 : 0
c_local varnames_`s' `vlist'
}
end
program RMcolLevel
gettoken beg 0 : 0
gettoken end 0 : 0
gettoken depvar 0 : 0
gettoken constant 0 : 0
gettoken isfctrs 0 : 0
gettoken touse 0 : 0
local pos 0
local k : word count `constant'
forval i = 1/`k' {
if `:word `i' of `constant'' == 1 {
local pos `i'
}
}
if `pos' {
forval i = 1/`k' {
local cons `cons' `=(`pos'==`i')'
}
}
else {
local cons `constant'
}
local is2 `isfctrs'
forvalues s = `beg'/`end' { // each sublevel
gettoken vlist 0 : 0
gettoken isfctr isfctrs : isfctrs
gettoken con cons : cons
local nocons
if !`con' { // no constant in sublevel
local nocons noconstant
}
if `s' == 0 {
if "`vlist'" != "" {
_rmdcoll `depvar' `vlist' if `touse' , `nocons'
c_local varnames_0 `r(varlist)' // c_local
}
}
else {
if `isfctr' {
c_local varnames_`s' `vlist' // c_local
local factors `factors' `vlist'
}
else {
_rmcoll `vlist' if `touse' , `nocons'
local vl`s' `r(varlist)'
local continuous `continuous' `r(varlist)'
c_local varnames_`s' `r(varlist)'
// just in case only one sublevel
}
}
}
foreach fulllist in continuous factors {
local dups : list dups `fulllist'
if "`dups'" != "" {
di as error "`:list uniq dups' repeated across equations within the same group"
exit 198
}
}
if (`beg' >= `end') exit
/* deleted: only check for collinearity within each sub level
_rmcoll `continuous' if `touse' , `nocons'
local continuous `r(varlist)'
*/
forvalues s = `beg'/`end' {
gettoken isfctr is2 : is2
if !`isfctr' {
if `s' > 0 {
c_local varnames_`s' `:list vl`s' & continuous'
local continuous : list continuous - vl`s'
}
}
}
end
program FixupConstant
args beg end s_constant
forvalues s = `beg'/`=`end'-1' {
if `s' == `s_constant' {
c_local constant_`s'
}
else {
c_local constant_`s' noconstant
}
}
end
program SetSublevels
args beg end sublevels
if (`end' < 1) exit
forvalues s = `=max(`beg',1)'/`end' {
matrix `sublevels'[1,`s'] = `end' - `beg' + 1
}
end
program ChkNonNumeric
gettoken myopts 0 : 0
local 0 : subinstr local 0 "R." "" , all count(local ct)
if (`ct' > 0) exit // Exit
local 0 : subinstr local 0 "r." "" , all count(local ct)
if (`ct' > 0) exit // Exit
syntax [varlist(ts numeric default=none)] [if] [in] [ , `myopts' ]
end
program ConstantNote
args lev_k
di as text ///
"{p 0 6}Note: level `lev_k' already has a constant, {cmd:noconstant} assumed for all other entries for the level{p_end}"
end
program RepeatedLevelError
di as error ///
"{p 0 4}`0' level variable, specified at more than one level{p_end}"
end
program SingleLevelCovNote
gettoken lev varlist : 0
di as text "{p 0 6} Note: single-variable random-effects specification; covariance structure set to {inp:identity}{p_end}"
end
program PostParms, eclass
syntax [anything] [, beta(string) vbeta(string) re(string) ///
vre(string) touse(string) ]
tempname bc vc
local r2 = colsof(`re')
if "`beta'" != "" {
local r1 = colsof(`beta')
}
else {
local names: colfullnames `re'
mat colnames `vre' = `names'
mat rownames `vre' = `names'
eret post `re' `vre', depname(`depvar') esample(`touse')
eret scalar k_f = 0
eret scalar k_r = `r2'
eret scalar k = `r2'
exit
}
gettoken depvar xvars : anything
if strpos("`depvar'",".") {
gettoken ts rest : depvar, parse(".")
gettoken dot depvar : rest, parse(".")
}
foreach v of local xvars {
local col `col' `depvar':`v'
}
mat colnames `beta' = `col'
mat `bc' = `beta', `re'
mat `vc' = (`vbeta' , J(`r1',`r2',0) \ J(`r2',`r1',0), `vre')
local names: colfullnames `bc'
mat colnames `vc' = `names'
mat rownames `vc' = `names'
eret post `bc' `vc', depname(`depvar') esample(`touse')
eret scalar k_f = `r1'
eret scalar k_r = `r2'
eret scalar k = `r1' + `r2'
end
program Display
syntax [, level(cilevel) VARiance noLRtest ///
noGRoup noHEADer noFETable noRETable ESTMetric grouponly]
local k = ("`fetable'"!="") + ("`retable'"!="") + ("`variance'"!="")
if `k' {
if "`estmetric'" != "" {
di as err "{p 0 4 4}option estmetric not allowed with options nofetable, noretable, and variance{p_end}"
exit 198
}
}
if "`header'" == "" {
DiHeader, `group' `grouponly'
if "`grouponly'" != "" {
exit
}
}
if "`estmetric'" != "" {
di
eret di, level(`level')
exit
}
if "`fetable'" == "" & e(k_f) > 0 {
DiEstTable, level(`level')
}
if "`retable'" == "" {
DiVarComp, level(`level') `variance' `lrtest'
local note `r(note)'
}
if !`e(converged)' {
if "`note'" == "" {
di
}
if "`e(emonly)'" != "" {
di as txt "{p 0 6 4}Note: EM algorithm failed to converge{p_end}"
}
else {
di as txt "{p 0 9 4}Warning: " as err ///
"convergence not achieved; estimates are based on iterated EM{p_end}"
}
}
end
program DiHeader
syntax [, noGRoup grouponly]
if "`grouponly'" != "" {
if "`e(ivars)'" == "" {
di as err "{p 0 4 2}model is linear regression; "
di as err "no group information avaiable{p_end}"
exit 459
}
DiGroupTable, table
exit
}
di
local crtype = upper(substr(`"`e(crittype)'"',1,1)) + /*
*/ substr(`"`e(crittype)'"',2,.)
di as txt "`e(title)'" _c
di _col(49) as txt "Number of obs" _col(68) "=" _col(70) as res ///
%9.0g e(N)
if "`group'" == "" {
DiGroupTable
di
}
di _col(49) as txt "`e(chi2type)' chi2(" as res e(df_m) as txt ")" ///
_col(68) "=" _col(70) as res %9.2f e(chi2)
di as txt "`crtype'" " = " as res %10.0g e(ll) /*
*/ _col(49) as txt "Prob > chi2" _col(68) "=" _col(73) /*
*/ as res %6.4f chiprob(e(df_m), e(chi2))
end
program DiGroupTable
syntax [, table]
local ivars `e(ivars)'
local levels : list uniq ivars
tempname Ng min avg max
mat `Ng' = e(N_g)
mat `min' = e(g_min)
mat `avg' = e(g_avg)
mat `max' = e(g_max)
local w : word count `levels'
if `w' == 0 {
exit
}
if `w' == 1 & "`table'" == "" {
di as txt "Group variable: " as res abbrev("`levels'",14) ///
_col(49) as txt "Number of groups" _col(68) "=" ///
_col(70) as res %9.0g `Ng'[1,1] _n
di as txt _col(49) "Obs per group: min" _col(68) "=" ///
_col(70) as res %9.0g `min'[1,1]
di as txt _col(64) "avg" _col(68) "=" ///
_col(70) as res %9.1f `avg'[1,1]
di as txt _col(64) "max" _col(68) "=" ///
_col(70) as res %9.0g `max'[1,1] _n
}
// 1 2 3 4 5 6
//123456789012345678901234567890123456789012345678901234567890
// No. of Observations per Group
// Group Variable | Groups Minimum Average Maximum
// level1 | ######## ######### ######### #########
else {
di
di as txt "{hline 16}{c TT}{hline 42}
di as txt _col(17) "{c |}" _col(21) "No. of" ///
_col(34) "Observations per Group"
di as txt _col(2) "Group Variable" _col(17) "{c |}" ///
_col(21) "Groups" _col(31) "Minimum" ///
_col(42) "Average" _col(53) "Maximum"
di as txt "{hline 16}{c +}{hline 42}"
local i 1
foreach k of local levels {
local lev = abbrev("`k'",12)
local p = 16 - length("`lev'")
di as res _col(`p') "`lev'" ///
as txt _col(17) "{c |}" ///
as res _col(19) %8.0g `Ng'[1,`i'] ///
_col(29) %9.0g `min'[1,`i'] ///
_col(40) %9.1f `avg'[1,`i'] ///
_col(51) %9.0g `max'[1,`i']
local ++i
}
di as txt "{hline 16}{c BT}{hline 42}"
}
end
program DiEstTable
syntax [, level(cilevel)]
di
ereturn di, first level(`level')
end
program DiLRTest, rclass
// We have already established that e(chi2_c) exists
if ((e(chi2_c) > 0.005) & (e(chi2_c)<1e5)) | (e(chi2_c)==0) {
local fmt "%8.2f"
}
else local fmt "%8.2e"
di as txt "LR test vs. linear regression:" _c
if `e(df_c)' == 1 { // chibar2(01)
di as txt _col(32) "{help j_chibar##|_new:chibar2(01) =}" ///
_col(46) as res `fmt' e(chi2_c) ///
_col(55) as txt "Prob >= chibar2 = " ///
_col(73) as res %6.4f e(p_c)
}
else {
local k = length("`e(df_c)'")
di as txt _col(`=39-`k'') "chi2(" as res e(df_c) ///
as txt ") =" _col(48) as res `fmt' e(chi2_c) ///
_col(59) as txt "Prob > chi2 =" ///
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -