📄 mfx.ado
字号:
local list
tokenize `fullist'
tempname T
local i = 1
while "``i''" != "" {
local eqlist : subinstr local eqlist "``i''" "", /*
*/ count(local isin) word all
if `isin' {
cap noi mat `T' = `B'[1,"``i'':"]
if _rc==111{
di as err "spaces are not allowed " /*
*/ "in equation names"
exit 111
}
local c = colsof(`T')
if `c' == 1 {
local cname : colnames `T'
cap tsrevar `cname'
if _rc == 0 {
local list "`list' ``i''"
}
else {
di in gr _n "warning: "/*
*/"equation ``i'' is " /*
*/"constant only;" /*
*/" marginal-effects not"/*
*/" calculated for" /*
*/" equation ``i''"
}
}
else local list "`list' ``i''"
}
local i = `i' + 1
}
local nospace : subinstr local eqlist " " "" , all
if "`nospace'" != "" {
di in gr _n "warning: equation(s) `eqlist' not found"
}
local eqlist "`list'"
if "`eqlist'"=="" {
local eqlist `fullist'
dis in gr _n "warning: equation list empty. "/*
*/"Default full variable list used."
}
}
local cnum = colsof(`B')
local cname : colnames `B'
*---- Parse varlist
if "`varlist'"!="" {
cap tsunab varlist: `varlist'
tokenize `varlist'
local i 1
while "``i''" !="" {
cap confirm var `i'
if _rc != 0 {
cap tsunab `i' : ``i''
}
local k 1
local found 0
local which = ""
while `k' <= `cnum' {
local cnamek : word `k' of `cname'
if "`cnamek'"=="``i''" {
local found=`found'+1
local which `which' `k'
}
local k=`k'+1
}
if `found'>0 {
forvalues m=1/`found' {
local wh1 : word `m' of `which'
mat `isv'[1,`wh1']=1
}
}
else {
dis in gr "warning: variable ``i'' not found"
}
local i=`i'+1
}
}
else mat def `isv'=J(1,`cnum',1)
*---- Find variables of interest
local i 1
local total 0 /* # of Xs of interest */
local xscol /* names of Xs of interest */
local dummy
local isin = 0
tokenize `fullist'
while `i' <= `cnum' {
local enamei : word `i' of `ename'
local cnamei : word `i' of `cname'
cap tsrevar `cnamei'
if _rc == 0 {
if "`eqlist'" == "" {
mat `ism'[1,`i'] = 1
local total = `total' + 1
local xscol "`xscol' `cnamei'"
if "`esamp'"=="" {
cap assert `cnamei' == 0 | /*
*/`cnamei' == 1 if e(sample)
}
else {
cap assert `cnamei' == 0 | /*
*/`cnamei'==1 | `cnamei'>=.
}
if _rc == 0 {
local dummy "`dummy' 1"
}
else local dummy "`dummy' 0"
}
else {
local junk : subinstr local eqlist "`enamei'" /*
*/ "`enamei'", count(local isin) word
local junk : subinstr local xscol "`cnamei'" /*
*/ "`cnamei'", count(local isdup) word
if (`isin' != 0) & (`isdup'== 0) {
local j = 1
while "``j''" != "`enamei'" {
local j = `j' + 1
}
mat `ism'[1,`i'] = `j'
local total = `total' + 1
local xscol "`xscol' `cnamei'"
local xseq "`xseq' `enamei'"
if "`esamp'"=="" {
cap assert `cnamei'== 0 | /*
*/ `cnamei'== 1 /*
*/ if e(sample)
}
else {
cap assert `cnamei' == 0 | /*
*/ `cnamei' == 1 | /*
*/ `cnamei' >= .
}
if _rc ==0 {
local dummy "`dummy' 1"
}
else local dummy "`dummy' 0"
}
if `isin' & `isdup' {
local flag 1
}
}
}
local i = `i' + 1
}
*---- Determine linear or nonlinear.
*---- Nonlinear if repeated variable found in fullist.
local i 1
local xfull
local totfull =0
local isin = 0
while `i' <= `cnum' {
local enamei : word `i' of `ename'
local cnamei : word `i' of `cname'
cap tsrevar `cnamei'
if _rc == 0 {
if "`fullist'" != "" {
local junk : subinstr local fullist /*
*/ "`enamei'" "`enamei'", /*
*/ count(local isin) word
local junk : subinstr local xfull /*
*/ "`cnamei'" "`cnamei'", /*
*/ count(local isdup) word
if (`isin' != 0) & (`isdup'== 0) {
local xfull `xfull' `cnamei'
local totfull =`totfull'+1
}
if `isin' & `isdup' {
local flag 1
}
}
}
local i = `i' + 1
}
forvalues i=1/`cnum' {
if `ism'[1,`i']==0 {
mat `isv'[1,`i']=0
}
}
local check 0
forvalues i=1/`cnum' {
if `ism'[1,`i']!=0 & `isv'[1,`i']!=0 {
local check = `check' + 1
}
}
if `check'==0 {
local total `check'
}
if `totfull'==0 {
local totfull `total'
}
if "`xfull'"=="" {
local xfull `xscol'
}
sreturn local xfull `xfull'
sreturn local xscol "`xscol'"
sreturn local total `total'
sreturn local totfull `totfull'
sreturn local dummy "`dummy'"
sreturn local flag `flag'
end
*----------------------------------------------------------
program define EstPost, eclass
args at atse
tempname B V
mat `B' = e(b)
mat `V' = e(V)
local cmd "`e(cmd)'"
IsTs `B' `V' `at' `atse'
local ts `s(ts)'
if "`cmd'" == "probit" | "`cmd'"=="dprobit" {
est post `B' `V', noclear
est local cmd "svyprobit"
est local predict "svylog_p"
}
if "`cmd'" == "logit" | "`cmd'"=="logistic" {
est post `B' `V', noclear
est local cmd "svylogit"
est local predict "svylog_p"
}
if "`cmd'" == "mlogit" {
est post `B' `V', noclear
est local cmd "svymlogit"
}
if ("`cmd'" == "ologit" | "`cmd'" == "oprobit") & missing(e(version)) {
local dep "`e(depvar)'"
local cname : colnames `B'
tokenize `cname'
local fname
while "`1'" != "" {
local junk : subinstr local 1 "_cut" "cut", /*
*/ count(local a)
if `a' == 1 {
local fname "`fname' `junk':_cons"
}
else local fname "`fname' `dep':`1'"
mac shift
}
mat colnames `B'= `fname'
mat colnames `V'= `fname'
mat rownames `V'= `fname'
est post `B' `V', noclear
if "`cmd'" == "oprobit" {
est local cmd "svyoprobit"
est local predict "ologit_p"
}
else est local cmd "svyologit"
}
if ("`cmd'" == "tobit" | "`cmd'" == "cnreg") & missing(e(version)) {
local cname : colnames `B'
tokenize `cname'
local fname
while "`1'" != "" {
if "`1'" != "_se" {
local fname "`fname' model:`1'"
}
else local fname "`fname' sigma:_cons"
mac shift
}
tempname sigma
scalar `sigma' = `B'[1,colsof(`B')]
mat colnames `B' = `fname'
mat colnames `V' = `fname'
mat rownames `V' = `fname'
est post `B' `V', noclear
est scalar sigma = `sigma'
est local cmd "intreg"
}
if `ts' {
cap est post `B' `V', noclear
mat `B' = e(b)
mat `V' = e(V)
local cname : colname `B'
tokenize `cname'
while "`1'" != "" {
if "`1'" != "_cons" {
cap drop `1'
qui gen `1' =.
}
mac shift
}
est post `B' `V', noclear
}
end
program define IsTs, sclass
args B V at atse
local cname : colname `B'
tokenize `cname'
local ts 0
while "`1'" != "" {
cap tsrevar `1'
if _rc == 0 {
if "`r(varlist)'" != "`1'" {
local cname : subinstr local cname "`1'" /*
*/ "var`r(varlist)'"
local ts 1
}
}
mac shift
}
if `ts' {
mat colname `B' = `cname'
mat colname `at' = `cname'
mat colname `atse' = `cname'
mat rowname `V' = `cname'
mat colname `V' = `cname'
}
sreturn local ts `ts'
end
*----------------------------------------------------------
program define MeanOffset, rclass
syntax [if] [in] [, noEsample noWght]
marksample touse
if "`esample'" == "" {
qui count if e(sample)
if r(N) != 0 {
qui replace `touse' = 0 if ~ e(sample)
}
}
if "`wght'" == "" {
if "`e(wexp)'" != "" {
local weight "[aweight`e(wexp)']"
}
}
foreach x in "" "1" "2" {
if ("`e(offset`x')'"!="" ) {
local offvar="`e(offset`x')'"
if "`wght'" == "" {
if "`e(wexp)'" != "" {
local weight "[aweight`e(wexp)']"
}
}
cap confirm var `offvar'
if _rc!=0 {
local expos = subinstr("`offvar'","ln(","",.)
local expos = subinstr("`expos'",")","",.)
cap confirm var `expos'
if _rc!=0 {
di as err "exposure variable "/*
*/"`expos' not found"
est unhold `origest'
exit 111
}
tempvar lnexp
qui gen `lnexp'=ln(`expos')
summ `lnexp' `weight' if `touse', meanonly
qui replace `expos'=exp(r(mean))
ret scalar avoff`x' =r(mean)
}
else {
summ `offvar' `weight' if `touse', meanonly
qui replace `offvar'=r(mean)
ret scalar avoff`x' =r(mean)
}
}
}
end
*----------------------------------------------------------
program define at, rclass
args X total cmd opt
local 0 "`opt'"
syntax [if] [in] [, noEsample noWght]
marksample touse
if "`esample'" == "" {
qui count if e(sample)
if r(N) != 0 {
qui replace `touse' = 0 if ~ e(sample)
}
}
if "`wght'" == "" {
if "`e(wexp)'" != "" {
local weight "[aweight`e(wexp)']"
}
}
tempname M B
mat `B' = e(b)
local cnum = colsof(`B')
local cname : colnames `B'
gettoken stat left: 0, parse(" ,")
mat `M' =`B'
local i 1
while `i' <= `cnum' {
mat `M'[1, `i'] = 1
local i = `i' + 1
}
local 0 "`cmd'"
gettoken thing1 rest : 0 , parse(" ,=")
if "`thing1'" == "mean" | "`thing1'" == "median" | /*
*/ "`thing1'" == "zero" {
Parsemean `X' `thing1' `touse' "`weight'"
if "`rest'"!="" {
gettoken thing1 rest2 : rest , parse(" ,=")
if "`thing1'" =="," {
Parseatlist `X' "`rest2'"
}
else {
Parseatlist `X' "`rest'"
}
}
Replace `X' `M'
return mat at `M'
return mat X `X'
exit
}
cap confirm number `thing1'
if _rc == 0 {
Parsenum `X' "`0'"
Replace `X' `M'
return mat at `M'
return mat X `X'
exit
}
cap qui mat list `thing1'
if _rc == 0 {
Parsematrix `X' "`0'"
Replace `X' `M'
return mat at `M'
return mat X `X'
exit
}
Parseatlist `X' "`0'"
local undone= "`r(undone)'"
local undone= trim("`undone'")
if "`undone'" !="" {
di as text _n "warning: no value assigned " /*
*/"in at() for variables `undone';"
di as text " means used for `undone'"
Parsemean `X' mean `touse' "`weight'"
Parseatlist `X' "`0'"
}
local unfnd= "`r(unfnd)'"
local unfnd= trim("`unfnd'")
if "`unfnd'" !="" {
di as text _n "warning: variables `unfnd' " /*
*/"in at() list not found in model" _n
Parsemean `X' mean `touse' "`weight'"
Parseatlist `X' "`0'"
}
Replace `X' `M'
return mat at `M'
return mat X `X'
end
program define Parsemean
args X stat touse weight
local vars : colnames `X'
tokenize `vars'
local i 1
while "``i''" !="" {
cap tsrevar ``i''
if _rc ==0 {
if "`stat'" == "zero" {
mat `X'[1, `i'] = 0
}
else {
if "`stat'" == "mean" {
sum ``i'' `weight' if `touse', meanonly
mat `X'[1,`i'] = r(mean)
}
else {
qui sum ``i'' `weight' if `touse',detail
mat `X'[1,`i'] = r(p50)
}
}
}
local i = `i' + 1
}
end
program define Replace
args X M
tempname B
mat `B' = e(b)
local vars : colnames `X'
tokenize `vars'
local cnum = colsof(`B')
local vnum = colsof(`X')
local cname : colnames `B'
local j 1
while `j' <= `cnum' {
local jcol : word `j' of `cname'
local k = 1
while "``k''"!= "`jcol'" & `k'<=`cnum' {
local k = `k' + 1
}
if "``k''"== "`jcol'" {
mat `M'[1,`j'] = `X'[1,`k']
}
local j = `j' + 1
}
end
program define Parseatlist, rclass
args X opt
local vars : colnames `X'
tokenize `vars'
local unfnd
local undone: colnames `X'
local vnum = colsof(`X')
gettoken thing1 vlist: opt, parse(" ,=")
while "`thing1'" != "" {
cap tsrevar `thing1'
if _rc {
dis in red "`thing1' in at() list not found"
exit 198
}
unab thing1 : `r(varlist)'
if index("`vars'","`thing1'")==0 {
local unfnd ="`unfnd'"+ " `thing1'"
}
gettoken equal vlist : vlist, parse(" =,")
if "=" != "`equal'" {
dis in red "`opt' found where `exp' expected in at()"
exit 198
}
gettoken thing2 vlist : vlist, parse(" =,")
confirm number `thing2'
local i = 1
while `i' <= `vnum' {
local name : word `i' of `vars'
cap tsrevar `name'
if !_rc & ("`r(varlist)'" == "`thing1'") {
mat `X'[1,`i'] = `thing2'
local undone : subinstr local undone /*
*/ "`thing1'" "", word all
}
local i = `i' + 1
}
gettoken comma rest: vlist, parse(" =,")
if "`comma'" == "," {
local vlist `rest'
}
gettoken thing1 vlist: vlist, parse(" =,")
}
ret local undone ="`undone'"
ret local unfnd ="`unfnd'"
end
program define Parsenum
args X opt
tempname B
mat `B' = e(b)
local eqname : coleq `B'
tokenize `eqname'
numlist "`opt'"
local nlist `r(numlist)'
tokenize "`nlist'"
local vnum = colsof(`X')
local i=`vnum'+1
local i1=`vnum'+2
if ("``i''"!="" & "``i''"!="1") | "``i1''"!="" {
di as error "numlist too long in at()"
exit 503
}
forvalues i=1/`vnum' {
if "``i''"=="" {
di as error "numlist too short in at()"
exit 503
}
else {
mat `X'[1,`i']=``i''
}
}
end
program define Parsematrix
args X A
tempname B
mat `B' = e(b)
local eqname : coleq `B'
tokenize `eqname'
local vnum = colsof(`X')
local Anum = colsof(`A')
if `Anum'==`vnum' | (`Anum'==`vnum' + 1 & `A'[1,`Anum']==1) {
forvalues i=1/`vnum' {
if `A'[1,`i']>=. {
di as error "matrix missing value in at()"
exit 503
}
else {
mat `X'[1,`i']=`A'[1,`i']
}
}
}
else {
di as error "incorrect matrix dimension in at()"
exit 503
}
end
*----------------------------------------------------------
program define GetY, sclass
args y X predopt
preserve
local cname : colnames `X'
tokenize "`cname'"
local j=1
while "``j''" != "" {
if "``j''" != "_cons" {
cap confirm var ``j''
local rc1 = _rc
cap confirm new var ``j''
local rc2 = _rc
if `rc1' == 0 | `rc2' == 0 {
tempvar junk
qui gen double `junk' = `X'[1,`j']
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -