xtgee.ado

来自「是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到」· ADO 代码 · 共 1,911 行 · 第 1/4 页

ADO
1,911
字号
*! version 4.4.5  03mar2005
program define xtgee, eclass byable(onecall)
	if _by() {
		local BY `"by `_byvars'`_byrc0':"'
	}
	`BY' _vce_parserun xtgee, panel : `0'
	if "`s(exit)'" != "" {
		exit
	}

	version 6.0, missing
	if replay() {
		if _by() { error 190 }
		di
                geerpt `0'
                exit
        }
	if _by() {
		local by "by `_byvars'`_byrc0':"
	}
	capture noisily `by' TSEstimate `0'
	mac drop S_X_*
	exit _rc
end

program define TSEstimate, eclass byable(recall)

	syntax varlist(ts) [if] [in] [iw fw pw] [, HASCONS /*
		*/	noCONstant noDISplay I(varname) T(varname) /*
		*/	EForm *]

	marksample touse	/* needed here so by: works */

	tokenize `varlist'
	local dep `1'
	mac shift
	local ind `*'
	
	/* Record names, then tsrevar */
	local depname `dep'
	local depstr : subinstr local depname "." "_"
	local indname `ind'
	_find_tsops `dep' `ind'
	if `r(tsops)' {
		qui tsset
		local myi `r(panelvar)'
		local myt `r(timevar)'
		if ("`i'" != "" & "`i'" != "`myi'") | /*
		*/ ("`t'" != "" & "`t'" != "`myt'") {
			di as error /*
*/ "time and panel variables previously tsset -- may not be changed by" /*
*/ _newline "options t() and i() -- use tsset to change them"
			exit 198
		}
		local i "i(`myi')"
		local t "t(`myt')"			
		tsrevar `dep'
		local dep `r(varlist)'
		tsrevar `ind'
		local ind `r(varlist)'
	}
	else {
		if "`i'" != "" {
			local i "i(`i')"
		}
		if "`t'" != "" {
			local t "t(`t')"
		}
	}
	
	/* Remove collinear vars and issue error message */	
	local inclcon ""
	if "`hascons'`constant'" != "" {
		local inclcon "noconstant"
	}
	local oldind `ind'
	qui _rmcoll `ind' if `touse' [`weight'`exp'] , `inclcon'
	local ind `r(varlist)'
	local dropped : list oldind - ind
	foreach x of local dropped {
		local j : list posof "`x'" in oldind
		local x2 : word `j' of `indname'
		noi di as txt "note: `x2' dropped due to collinearity"
	}
	
	/* Fit the model on the tsrevar'ed data */
	Estimate `dep' `ind' if `touse' [`weight'`exp'], `hascons' /*
		*/	`constant' `options' depname(`depname') `i' `t'
	
	/* Doctor up stripes on b and V	*/
	tempname b V
	mat `b' = e(b)
	mat `V' = e(V)
	local stripe : colfullnames `b'
	local stripe : subinstr	local stripe "`dep'" "`depstr'", all
	foreach x of local ind {
		local j : list posof "`x'" in oldind
		local x2 : word `j' of `indname'
		local stripe : subinstr local stripe "`x'" "`x2'"
	}
	mat colnames `b' = `stripe'
	mat colnames `V' = `stripe'
	mat rownames `V' = `stripe'
	est local depvar "`depname'"
	est repost b=`b' V=`V' , rename

	global S_E_depv = "`=trim("`depname'")'"
	global S_E_vl = "`=trim("`depname'")' `indname'"
	
	if "`display'" == "" {
		geerpt , `eform' 
	}
	
	if "`e(rc)'" == "" {
		error `e(rc)'
	}


end

program define Estimate, eclass byable(recall) sort
	syntax varlist [if] [in] [iw fw pw] [, /*
		*/ Link(string) Family(string) Corr(string) /*
		*/ I(varname) ITERate(int 100) TOLerance(real 1e-6) NMP RGF/*
		*/ Exposure(varname numeric) OFFset(varname numeric) Robust /*
		*/ HASCONS noCONstant TRace RC0 FROM(string) /*
		*/ EForm FIXed(string) T(varname) Scale(string) /*
		*/ noLOg noDISplay LEVel(cilevel) SCore(string) FORCE /*
		*/ /* undocumented -> */ DEPNAME(string) ]

	local iter "`iterate'"
	local iterate

	local tol "`tolerance'"
	local toleran

	GetName `"`link'"' `"`family'"' `"`corr'"'
	local link = "$S_1"				/* OK to use S_ here */
	local family  = "$S_2"				/* OK to use S_ here */
	local corr = "$S_3"				/* OK to use S_ here */
	local oarg = "$S_4"				/* OK to use S_ here */
	local band "$S_5"	/* default -1 */	/* OK to use S_ here */
	local fixed "$S_6"				/* OK to use S_ here */

	if "`nmp'"=="" {
		local nmp 0
	}
	else {
		local nmp 1
	}

	if "`rgf'" != "" {
		if "`weight'" != "pweight" & "`robust'" =="" {
			noi di in red "rgf can only be specified with robust"
			error 198
		}
		if "$S_2" != "gauss" {
			noi di in red "rgf can only be specified with "/*
				*/ "family(gaussian) "
			error 198
		}
	}
	if "`constan'" != "" {
		local cons "nocons"
		local nocons "nocons"
	}

	if "`oarg'"!="" {
		cap confirm number `oarg'
		if _rc {
			unabbrev `oarg', max(1)
			local oarg "`s(varlist)'"
			local xtravar "`s(varlist)'"
		}
	}

	checkfam "`family'" "`link'"

	xt_iis `i'
	local rivar "`s(ivar)'"

	GetLag `corr' `t'
	local tvar "`s(timevar)'"

	global S_X_scp = "`scale'"

	if "`log'" == "" {
		local prel "noi di "
		global S_LOG 1
	}
	else {
		local prel "*"
	}

	if "`score'" != "" {
		local n : word count `score'
		if `n'!=1 {
			di as err "score() requires specification of one " /*
			*/ "new variable"
			exit 198
		}
		confirm new var `score'
	}


	ChkSyn `tol' `iter' "`hascons'" "`cons'"

	tempvar mui recid bb binom

	if "`exposur'"!="" {
		if "`offset'"!="" {
			di in red "may not specify both exposure() and offset()"
			exit 198
		}
		local offstr "ln(`exposur')"
		tempvar offset
		qui gen double `offset' = ln(`exposur')
	}

	if "`offset'" != "" {
		local addoff "replace `mui' = `mui' + `offset'"
		local glmarg "off(`offset')"
		if "`offstr'"=="" {
			local offstr "`offset'"
		}
	}

	marksample touse
	markout `touse' `offset' `rivar' `tvar' `xtravar'
	local xtravar

	qui count if `touse'
	if r(N) <= 1 {
		di in red "insufficient observations"
		exit 2001
	}

	if "`score'"!= "" {
		tempvar merge
		qui gen `merge' = _n
	}
	preserve

	qui {
		keep if `touse'
		sort `rivar' `tvar'

		if "`tvar'"!="" {
			tempvar tord
			tabulate `tvar', cgen(`tord')
			if "`corr'"=="unstr" | "`corr'"=="fixed" {
				local abst "`tord'"
				local topt "t(`tord')"
			}
			else {
				noi KeepMode `rivar' "`tvar'" "`force'"
				noi KeepMin "`corr'" `rivar' `band'
				local band `r(band)'
				if "`corr'"=="nonst" {
					local abst "`tord'"
					local topt "t(`tord')"
				}
			}
			noi Checkt `rivar' `tord' `tvar'
		}
					/* Observation sample fixed */
		noi BigEnuf `rivar' `abst'
		local minni `r(min)'
		global S_X_maxn `r(xmax)'
		local avgni `r(mean)'
		local maxni $S_X_maxn
		local rmaxni `r(max)'
		local nclust `r(N)'
		local keep   `r(N)'

		if "`corr'" == "fixed" {
			local g = rowsof(`fixed')
			if `maxni' != `g' {
				noi di in red /*
			*/ "correlation matrix must be `maxni'x`maxni'"
				exit 198
			}
		}
		if `nclust' == 1 & "`robust'" != "" {
			noi di in red /*
				*/ "cannot specify robust with only one panel"
			exit 198
		}

		tempname sumw
		if "`weight'" != "" {

			if "`weight'" == "pweight" {
				local robust "robust"
			}

/* comment A */
			* Generate weights
			tempvar ww
			gen double `ww' `exp'

			* Scale weights if not iweight
/* comment B */
			summ `ww'
			if "`weight'" == "aweight" & 0 {
				replace `ww' = `ww'/r(mean)
				scalar `sumw' = _N
			}
			else {
				scalar `sumw' = r(sum)
			}

			* Get avg panel size and number of groups
			noi ChkWt `rivar' `ww'
			if "`weight'" != "aweight" {
				local avgni  `r(mean)'
				local nclust `r(sum)'
			}

			local wtex  "[`weight'=`ww']"
		}
		else {
			tempvar ww
			gen byte `ww' = 1
			scalar `sumw' = _N
		}
		local warg "wvar(`ww')"

		if "`oarg'"=="" {
			gen byte `binom' = 1
		}
		else	gen `binom' = `oarg'

		keep `varlist' `rivar' `tvar' `offset' `merge' /*
			*/ `ww' `tord' `binom'

		if "`tvar'"=="" {
			local ivar "`rivar'"
		}
		else {
			tempvar ivar
			xtgee_makeivar `rivar' `tvar' `tord' `maxni' -> `ivar'
			sort `ivar' `tvar'
		}

		tempvar obs
		gen float `obs' = _n
		local nobs = _N

		tokenize `varlist'
		global S_X_depv "`1'"
		mac shift
		global S_X_idep "`*'"

		if $S_X_con == 0 {
			local hascons 1
			if "`eform'" == "" {
				noi _rmcoll $S_X_idep `wtex', nocons
			}
			else    noi _rmcoll $S_X_idep `wtex'
			global S_X_idep "`r(varlist)'"
			_rmcoll $S_X_idep
			global S_X_HAS "`r(varlist)'"
		}
		else {
			noi _rmcoll $S_X_idep `wtex'
			global S_X_idep "`r(varlist)'"
			global S_X_HAS "`r(varlist)'"
		}
		noi di

		local p : word count $S_X_idep
		local p = `p' + $S_X_con
		if `p' <= 0 {
			error 102
		}
		if `p' >= _N {
			di in red "insufficient observations"
			exit 2001
		}

		if `sumw' < `p' {
			noi di in red /*
			*/ "sum of weights must be larger than p"
			exit 198
		}

		global S_X_link "`link'"
		global S_X_mvar "`family'"
		global S_X_corr "`corr'"
		global S_X_ivar "`ivar'"

		sort `ivar' `tvar' `obs'

		* the following may not be necesasry
		if `band' < 0 {
			local band = `minni'-1
		}

		if "`corr'" == "fixed" {
			local R "`fixed'"
		}
		else {
			tempname R
			mat `R' = J(`maxni',`maxni',0)
		}

		tempname b0 b1 phi alpha S1 S2 zi

		if "`family'" == "binom" {
			local xarg "`binom'"
		}

		if "`from'" == "" {
			GetIval "`hascons'" "`nocons'" "`wtex'" /*
				*/ "`xarg'" "`oarg'" -> `b1' "`glmarg'"
		}
		else {
			local names $S_X_idep
			if $S_X_con {
				local names `names' _cons
			}
			noi _mkvec `b1', from(`from') colnames(`names') /*
				*/ error("from()")
		}
		local sna : colnames(`b1')

		local XXn : word count `sna'
		if `XXn' != `p' {
			noi di in red "Unable to identify sample"
			exit 198
		}

		local iterate 1
		local diff 1000

                if "`trace'" != "" {
                        local aanames ""
                        local i 0
                        while `i' <= `band' {
                                local aanames `aanames' `i'
                                local i = `i'+1
                        }
                }

		tempvar ei t cc lagw

		sort `ivar' `tvar' `obs'
		while `iterate' < `iter' & `diff' > `tol' {

			mat `b0' = `b1'
			scalar `phi' = 0.0

			* switch(link) page 4

			cap drop `mui'
			mat score double `mui' = `b1'
			`addoff' /* replace `mui' = `mui' + `offset' */
			xtgee_plink "`link'" `mui' `binom'

			* switch (var_mean_rel) page 4

			cap drop `ei'
			gen double `ei' = $S_X_depv - `mui'
			xtgee_elink "`family'" `mui' `ei' `binom'

			cap drop `cc'

			gen double `cc' = `ww'*`ei'*`ei'
			cap drop `t'

/* Use Liang Zeger 1986 moment estimators instead of Karim estimators */
			summ `cc'
			if r(N) != `nobs' {
				noi di in red "estimates diverging " /*
					*/ "(missing predictions)"
				exit 430
			}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?