📄 ml_model.ado
字号:
*! version 7.3.6 03may2005
program define ml_model, sclass
version 6, missing
sret clear
/*
... "caller 6 tempnames" <method> <userprob> <equations> <std syntax>
*/
gettoken tvars 0: 0
gettoken caller tvars: tvars
global ML_vers "version `caller':"
gettoken (global) ML_meth 0: 0
gettoken (global) ML_user 0: 0
capture IsName $ML_meth
if _rc {
di in red `""$ML_meth" invalid method"'
exit 198
}
local w1 : word 1 of $ML_user
capture IsName `w1'
if _rc {
di in red `""$ML_user" invalid evaluation-program name"'
exit 198
}
global ML_ouser `"$ML_user"'
local w1
if `"$ML_meth"'=="" | `"$ML_user"'=="" {
error 198
}
/*
... <equations> <std syntax>
*/
gettoken next 0: 0, parse(" [],()/")
IsLast `next'
global ML_n 0
while `s(cont)' {
global ML_n = $ML_n + 1
local n = `n' + 1
if `"`next'"'=="(" {
/* Now, there might be an option with parens */
local flag 1
gettoken next 0: 0, parse("()")
while `flag' > 0 & `"`next'"' != "" {
if `"`next'"' == "(" {
local flag = `flag' + 1
}
if `"`next'"' == ")" {
local flag = `flag' - 1
}
if `flag' {
local eq `"`eq'`next'"'
gettoken next 0: 0, parse("()")
}
}
if `flag' {
di in red "unbalanced parentheses"
exit 198
}
ParseIL `eq'
local eq
}
else if `"`next'"' == "/" {
gettoken next 0: 0, parse(" ,")
ParsePrm `next'
}
else {
eq ?? `next'
ParseIL $S_3: $S_1
}
gettoken next 0: 0, parse(" [],()/")
IsLast `next'
}
/*
... <std syntax>
*/
local 0 `"`next' `0'"'
syntax [fweight aweight iweight pweight] [if] [in] /*
*/ [, BRACKET noCLEAR CLuster(string) COLlinear CONTinue /*
*/ INIt(string) LF0(string) MAXimize MISSing OBS(integer 0) /*
*/ noPREserve Robust noSCVARS TItle(string) WALDtest(int -1) /*
*/ TECHnique(passthru) VCE(string) CONSTraints(string) /*
*/ TOUSEv(string) WEIGHTv(string) MATSv(string) noOUTput /*
*/ CRITTYPE(string) /*
*/ svy /* -svy- options
*/ STRata(varname) /* not allowed with svy option
*/ psu(varname) /*
*/ fpc(varname numeric) /*
*/ noSVYadjust /* allowed with svy option
*/ SUBpop(passthru) /*
*/ SRSsubpop /*
*/ svarconst /* force constraints for svar
*/ noCNSNOTEs /* -makecns- option
*/ dzeros(name) /* opt to set ML_d0_S
*/ NOSCORE /* not documented
*/ * /* other options
*/ ]
// -vce(ROBust)- is a synonym for -ROBust-
if `"`vce'"' == substr("robust",1,max(3,length("`vce'"))) {
local robust robust
local vce
}
else if `"`vce'"' != "" {
local vce `"vce(`vce')"'
}
local output /* ignore output/nooutput */
if "`dzeros'" != "" {
confirm matrix `dzeros'
global ML_00_S `dzeros'
}
// put -diparm()- options into globals ML_diparm#
ParseDiparmOpts `options'
local options `"`s(options)'"'
/* handle svy options */
if `"`svy'"' != "" {
if "`weight'" != "" {
di in red /*
*/ "svy option not allowed with weights"
exit 198
}
if "`psu'`strata'`fpc'" != "" {
di in red /*
*/ "svy option not allowed with psu(), strata(), or fpc()"
exit 198
}
/* pick up -svyset-ings */
if _caller() < 9 {
quietly version 8.2: svyset
local strata `r(strata)'
local psu `r(psu)'
local fpc `r(fpc)'
local weight `r(wtype)'
local wvar `r(`r(wtype)')'
local exp "`r(wexp)'"
if `"`r(_svy)'"' != "" {
local oldsvy yes
}
}
if "`oldsvy'" == "" {
quietly version 9: svyset
if `"`r(settings)'"' == ", clear" {
di in red ///
"svy option requires the data to be svyset"
exit 198
}
global ML_svy2 svy
local weight `r(wtype)'
local exp `"`r(wexp)'"'
local wvar : word 2 of `exp'
local strata `r(strata1)'
}
}
else {
if "`psu'`strata'`fpc'" != "" {
local svy svy
if `"`weight'"' != "" {
gettoken junk wvar : exp
}
}
}
if "`noscore'" != "" {
global ML_nosc `noscore'
if !inlist("$ML_meth", "d1", "d2") {
di as err ///
"option noscore is only allowed with method d1 and d2 likelihood evaluators"
exit 198
}
if `"`robust'"' != "" {
opts_exclusive "robust noscore"
}
if `"`svy'"' != "" {
opts_exclusive "svy noscore"
}
if `"`cluster'"' != "" {
opts_exclusive "cluster() noscore"
}
if `"`weight'"' == "pweight" {
di as err "pweights not allowed with option noscore"
exit 198
}
}
/* common settings for -svy- data */
if "`svy'" != "" {
local lf0
if "`cluster'" != "" {
opts_exclusive "cluster() svy" cluster
}
if inlist("$ML_meth","d0","rdu0") {
di as err ///
"option svy is not allowed with method $ML_meth"
exit 198
}
if "`vce'" != "" {
opts_exclusive "vce() svy" vce
}
global ML_svy svy
global ML_sadj `svyadju'
local zerow zeroweight
local robust robust
if "$ML_svy2" == "" {
global ML_str `strata'
}
global ML_clust `psu' /* instead of ML_psu */
global ML_fpc `fpc'
if `"`weight'"' != "" {
// weights are saved in two places for -svy- data
// becuase of -subpop()-, which requires one weight
// var for estimation that takes -subpop()- into
// account, and a second that does not for -_robust-
// (which takes the -subpop()- option itself)
global ML_swty `weight'
global ML_sw `wvar'
if `"`weight'"' != "iweight" & /*
*/ `"`weight'"' != "pweight" {
di in red /*
*/ "`weight's may not be used with svy options"
exit 198
}
local weight iweight
}
}
else {
if `"`subpop'"' != "" {
di in red /*
*/ "subpop() requires svy, strata(), psu(), or fpc() option"
exit 198
}
}
/* do not allow -ml maximize-'s -noclear- option */
if "`clear'"!="" {
di in red "noclear option not allowed"
exit 198
}
/* end do not allow -ml maximize-'s -noclear- option */
/* "$ML_mksc" != "" -> user needs score variables */
if "`scvars'"=="" {
global ML_mksc "yes"
}
if `waldtes'>0 & "`lf0'"!="" {
di in red "may not specify both lf0() and waldtest(k), k>0"
exit 198
}
/*
begin -continue- option:
-continue- just fills in -lf0()- and perhaps -init-
*/
if "`continu'" != "" {
if "`init'"=="" { /* no init() option */
tempname init /* ... then fill it in */
local ub0 `init'
}
else tempname ub0
mat `ub0' = get(_b)
// -svy- option does not allow the LR test
if "`svy'" == "" {
tempname v0 iv0 /* vce of the first model */
mat `v0' = e(V)
mat `iv0' = syminv(`v0')
global ML_rank0 = colsof(`v0') - diag0cnt(`iv0')
if "`lf0'"=="" & e(ll)< . {
local np = matrix(colsof(`ub0'))
local lf0 `np' `e(ll)'
local ub0
local np
}
}
}
/* end of -continue- option */
/* begin -cluster()-, part 1 processing */
if `"`cluster'"'!="" {
unabbrev `cluster', max(1)
global ML_clust `cluster'
}
/* end -cluster()-, part 1 processing */
/* obtain touse and weight variable names in $ML_samp and $ML_w */
if "`maximiz'"=="" {
SetTmpV ML_samp <- _MLtu `"`tousev'"'
SetTmpV ML_w <- _MLw `"`weightv'"'
}
else {
global ML_samp : word 1 of `tvars'
global ML_w : word 2 of `tvars'
}
// remember original weight variable, poststratification adjustments
// require two weight variables, one for estimation that takes
// -subpop()- into account, and a second that does not for -_robust-
// (which takes the -subpop()- option itself)
global ML_wo $ML_w
if `"`weight'"' != "" {
local w [`weight'`exp']
}
/* obtain names for ML_b, ML_f, ML_g, ML_V, ML_dfp_b, ML_dfp_g */
if "`matsv'"!="" { // UNDOCUMENTED option -matsv()-
local n : word count `matsv'
if `n'!=6 {
error 198
}
global ML_b : word 1 of `matsv'
global ML_f : word 2 of `matsv'
global ML_g : word 3 of `matsv'
global ML_V : word 4 of `matsv'
global ML_dfp_b : word 5 of `matsv'
global ML_dfp_g : word 6 of `matsv'
IsName $ML_b
IsName $ML_f
IsName $ML_g
IsName $ML_V
IsName $ML_dfp_b
IsName $ML_dfp_g
}
else {
if "`maximiz'"!="" {
global ML_b : word 3 of `tvars'
global ML_f : word 4 of `tvars'
global ML_g : word 5 of `tvars'
global ML_V : word 6 of `tvars'
global ML_dfp_b : word 7 of `tvars'
global ML_dfp_g : word 8 of `tvars'
global ML_subv : word 9 of `tvars'
}
else {
global ML_b ML_b
global ML_f ML_f
global ML_g ML_g
global ML_V ML_V
global ML_dfp_b ML_dfp_b
global ML_dfp_g ML_dfp_g
global ML_subv ML_subv
}
}
scalar $ML_f = .
/* end obtain names for ML_b, ML_f, ML_g, ML_V, ... */
/* mark estimation subsample */
mark $ML_samp `w' `if' `in' , `zerow'
if "`missing'"=="" {
MarkOut /* variables from equations */
}
markout $ML_samp $ML_clust $ML_str $ML_fpc, strok
/* fill in weight variable */
quietly {
if "`weight'"!="" {
global ML_wexp `"`exp'"'
gen double $ML_w `exp' if $ML_samp
if "`weight'"=="aweight" {
qui sum $ML_w if $ML_samp, meanonly
qui replace $ML_w = $ML_w/r(mean)
}
compress $ML_w
global ML_wtyp `weight'
}
else gen byte $ML_w = 1 if $ML_samp
if "$ML_svy" != "" {
if `"`weight'"' == "pweight" {
capture assert $ML_w >= 0
if _rc {
error 402
}
}
// e(N_pop) should not be affected by omitted strata
sum $ML_w, mean
global ML_pop `r(sum)'
}
}
/* Handle subpop. */
if `"`subpop'`srssubpop'"' != "" {
// _svy_subpop can decrease the sample size by omitting strata
_svy_subpop $ML_samp $ML_subv, ///
strata(`strata') `subpop' `srssubpop' wvar($ML_w)
global ML_srsp `r(srssubpop)' /* srssubpop option flag */
global ML_omit `r(N_strata_omit)'
global ML_subp `r(subpop)'
quietly replace $ML_w = 0 if $ML_subv==0
}
else global ML_subv
/* Compute total #obs. */
if "$ML_svy" != "" {
quietly count if $ML_samp
if r(N) == 0 {
error 2000
}
if `"`weight'"' != "" {
quietly count if $ML_samp & $ML_w != 0
if r(N) == 0 {
di in gr "all observation have zero weights"
exit 2000
}
}
}
/* count number of observations, deal with obs() option */
if `obs' {
if `obs'<0 {
di in red "obs(`obs') invalid"
exit 198
}
global ML_N `obs'
local obs
}
else {
if "`weight'"=="fweight" {
qui sum $ML_w if $ML_samp, meanonly
global ML_N = `r(sum)'
}
else {
qui count if $ML_samp
global ML_N = r(N)
}
}
/* end count number of observations, deal with obs() option */
/* set -robust- option */
global ML_vce `robust'
if "$ML_clust"!="" {
if "`vce'" != "" {
di as err ///
"options vce() and cluster() may not be combined"
exit 198
}
global ML_vce "robust"
}
if "$ML_wtyp"=="pweight" {
if "`vce'" != "" {
di as err ///
"option vce() and pweights may not be combined"
exit 198
}
global ML_vce "robust"
}
if `"`crittype'"' == "" {
if "$ML_vce" == "robust" {
global ML_crtyp "log pseudolikelihood"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -