xthtaylor.ado

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

ADO
787
字号
*! version 1.2.5  17mar2005
program  xthtaylor, eclass byable(onecall) sort
	if _by() {
		local BY `"by `_byvars'`_byrc0':"'
	}
	`BY' _vce_parserun xthtaylor, panel : `0'
	if "`s(exit)'" != "" {
		exit
	}
			/* Model: y_it = b*x_it + g*z_i + mu_i + v_it
			   z_i: time invariant in panel
			   mu_i: random effects
			   v_it: random error
			   X = [X1, X2];  Z = [Z1, Z2]
			   X1, Z1 exogeneous, and X2, Z2 endogeneous
			*/
	version 8
	if replay() {
		if `"`e(cmd)'"'==`"xthtaylor"' {
			if _by() {
				error 190
			}
			Replay `0'
			exit `e(rc)'
		}
		error 301
	}
	`BY' Estimate `0'
end

program Estimate, eclass byable(recall)
	syntax varlist(ts) [if] [in] [iw fw],		/*
		*/ Endog(varlist numeric ts) 		/*
		*/ [ CONStant2(varlist numeric ts) 	/*
		*/ Varying(varlist numeric ts) 		/*
		*/ AMacurdy 				/*
		*/ noCONStant 				/*
		*/ I(varname) 				/*
		*/ T(varname) 				/*
		*/ Level(cilevel) 			/*
		*/ Small ]

	if "`constant2'" != "" & "`varying'" != "" {
		di as err "may not specify both -constant()- and -varying()-"
		exit 198
	}

	local typelist varlist endog 
	local inlist : list endog in varlist
	if !`inlist' {
		di as err "some variables specified in -endog()- not " /*
			*/ "included in main variable list"
		exit 198
	} 

	if "`constant2'" != "" {
		local typelist `typelist' constant2
		local inlist : list constant2 in varlist
		if !`inlist' {
			di as err "some variables specified in " /*
				*/ "-constant()- not " /*
				*/ "included in main variable list"
			exit 198
		} 
	}
	if "`varying'" != "" {
		local typelist `typelist' varying
		local inlist : list varying in varlist
		if !`inlist' {
			di as err "some variables specified in " /*
				*/ "-varying()- not " /*
				*/ "included in main variable list"
			exit 198
		} 
	}
	
	local fullist		/* full variable list */
	foreach type of local typelist {
		local `type' : list uniq `type'
		tsunab `type' : ``type''
		local fullist : list fullist | `type'
	}

	gettoken yvar xzlist : fullist

	xt_iis `i'
	local ivar "`s(ivar)'"
	cap xt_tis `t'		/* to handle time-series operators
				   & -amacurdy- */
	local tvar "`s(timevar)'"
	
	if _rc & "`amacurdy'" != "" {
		di as err "t() required"
		exit 198
	}

	tempvar wt
	if "`weight'" != "" {
		qui gen double `wt' `exp'
		local wtopt "[`weight'=`wt']"
	}
	else {
		qui gen byte `wt' = 1
	}


	tempvar touse
	mark `touse' `if' `in' `wtopt'
	markout `touse' `xzlist' 

	if "`amacurdy'" != "" {
		tempvar first_t diff
		sort `touse' `ivar' `tvar'
		qui by `touse' `ivar': gen byte `first_t' = (_n==1 & `touse')
		qui bys `touse' `first_t' : gen byte `diff' = `tvar' - /*
			*/ `tvar'[_n-1] if _n>1 & `first_t' 
		summ `diff' if `first_t', meanonly
		if r(max)!=0 | r(min)!=0 {
			di as err "All panels must start at the " /*
				*/ "same time period"
			exit 198
		}

		qui tsset `ivar' `tvar'
		tsreport if `touse', report panel
		if r(N_gaps) != 0 {
			di as err "Gaps are not allowed with -amacurdy-"
			exit 198
		}

		if "`weight'" != "" {
			di as err "Weights are not allowed with -amacurdy-"
			exit 198
		}
	}

				/* parse variables */
	CheckVar `ivar' `"`tvar'"' `touse' `xzlist'
	local constant2_c `s(invariant)'
	local varying_c `s(varying)'


	if "`constant2'" != "" {
		local diff : list constant2 - constant2_c 
		if "`diff'" != "" {
			di as err "`diff' not time invariant in panels." 
			exit 198
		}
		local diff : list constant2_c - constant2 
		if "`diff'" != "" {
			di as err "`diff' not included in -constant()-." 
			exit 198
		}
	}
	if "`varying'" != "" {
		local diff : list  varying - varying_c
		if "`diff'" != "" {
			di as err "`diff' not time varying in panels." 
			exit 198
		}
		local diff : list  varying_c - varying
		if "`diff'" != "" {
			di as err "`diff' not included in -varying()-." 
			exit 198
		}
	}

	local exog : list xzlist - endog
	local xvar1 : list exog - constant2_c
	local xvar2 : list endog - constant2_c 
	local zvar1 : list exog - varying_c
	local zvar2 : list endog - varying_c

				/* check colliearity */
	local nvar : word count `xzlist'
	_rmcoll `xzlist' `wtopt' if `touse', `constant'
	local xzlist `"`r(varlist)'"'

	local vartype : set type
	set type double
	local names
	foreach type in xvar1 xvar2 zvar1 zvar2  {
		local `type' : list `type' & xzlist
		local names `names' ``type''
		local `type'names ``type''
		tsrevar ``type''
		local `type' `r(varlist)'
		markout `touse' ``type''
	}
	local yname `yvar'
	tsrevar `yvar'
	local yvar `r(varlist)'
	markout `touse' `yvar'
	set type `vartype'

	local ok = 1
	if "`xvar1'" == "" {
		di as err "There are no time-varying exogeneous " /*
			*/ "variables in the model."
		local ok = 0
	} 
	if "`xvar2'" == "" {
		di as err "There are no time-varying endogeneous " /*
			*/ "variables in the model."
		local ok = 0
	} 
	if "`zvar1'" == "" {
		di as err "There are no time-invariant exogeneous " /*
			*/ "variables in the model."
		local ok = 0
	} 
	if "`zvar2'" == "" {
		di as err "There are no time-invariant endogeneous " /*
			*/ "variables in the model."
		local ok = 0
	} 
	if !`ok' {
		di as err "If you have those variables specified, " /*
			*/ "they may have been " /*
			*/ "removed due to collinearity."
		exit 198
	}

	sort `touse' `ivar'		/* _crcchkw needs to sorted by this 
					   order, but previous commands needs 
					   the order to be -by ivar tvar-
					   to handle time-series operators
					*/
				/* check fweights */
	if "`weight'" == "fweight" {
 		_crcchkw `ivar' `wt' `touse'
	}

				/* check conditions */
	local k1 : word count `xvar1'
	local k2 : word count `xvar2'
	local g2 : word count `zvar2'
	local k = `k1' + `k2'

	if `k1' < `g2' & "`amacurdy'" == ""  {
		di as err "The model is underidentified (k1<g2)"
		exit 198
	}

	quietly {
				/* count T_i, summarize */
		tempvar Ti
		by `touse' `ivar': gen double `Ti' = sum(`touse')
		by `touse' `ivar': replace `Ti' = . if _n!=_N
		summ `Ti' `wtopt'  if `Ti'!=0 & `Ti'<., meanonly
		local Ng = r(N)			/* # of panels */
		local N = r(sum_w)		/* weighted counts */

		summ `Ti' if `Ti'>0, meanonly
		local Tmax = r(max)
		local Tmin = r(min)
		local Tavg = r(mean)

		if `Tmax'*`k1' < `g2' & "`amacurdy'" != "" {
			di as err "The model is " /*
				*/ "underidentified (T*k1<g2)"
			exit 198
		}	

		tempname Tbar
		if `Tmax' == `Tmin' {
			local isT_cons 1	/* balanced */
			by `touse' `ivar': replace `Ti' = cond(`touse', /*
				*/ `Ti'[_N], .)
			scalar `Tbar' = `Tmax'
		}
		else {
			if "`amacurdy'" != "" {
				di as err "-amacurdy- only works with " /*
					*/ "balanced data"
				exit 198
			}

			local isT_cons 0	/* unbalanced */
			tempvar T_inv
			by `touse' `ivar': gen double `T_inv'=1/`Ti'
			summ `T_inv', meanonly
			scalar `Tbar' = 1/r(mean)
			by `touse' `ivar': replace `Ti' = cond(`touse', /*
				*/ `Ti'[_N], .)
		}

				/* within transformation */
		local list "yvar xvar1 xvar2"
		foreach var of local list {
			local k = 1
			foreach i of local `var' {
					/* 
					   `var'`k'_m = Xbar, the mean of X
					   `var'`k'_dm = X - Xbar 
					   note: using `k' instead of original
						 variable name to avoid the
						 case that a name is too long
					*/
				tempvar `var'`k'_m `var'`k'_dm

				by `touse' `ivar': gen double ``var'`k'_m' = /*
					*/ sum(`i') 
				by `touse' `ivar': replace ``var'`k'_m' = /*
					*/ ``var'`k'_m'[_N]/`Ti'
				by `touse' `ivar': gen double ``var'`k'_dm' = /*
					*/ `i'-``var'`k'_m' if `touse'
				local `var'_m ``var'_m' ``var'`k'_m'
				local `var'_dm ``var'_dm' ``var'`k'_dm' 
				local k = `k'+1
			}
		}

					/* obtain within-estimator */
		reg `yvar_dm' `xvar1_dm' `xvar2_dm' `wtopt' /*
			*/ if `touse', nocons
		local n = e(N)
				
		tempname b_w V_w
		mat `b_w' = e(b)

				/* estimate sigma_v^2 */
		tempname sigmaV2
		scalar `sigmaV2' = e(rss)/(`n'-`N')

				/* generate d_i=ybar_i-xbar_i*b_within 
				   trick -mat score- to use xbar_i 

				   note: if some transformed covariates 
				   are dropped from OLS regression, the 
				   corresponding coefficient will be set as
				   zero, so won't affect -mat score-. 
				*/
		tempvar xbm_w xb_w res res2 zg di
		local newnames `xvar1_m' `xvar2_m'
		mat colnames `b_w' = `newnames'
		mat score double `xbm_w' = `b_w' if `touse'
		gen double `di' = `yvar_m' - `xbm_w'

		local newnames `xvar1' `xvar2'
		mat colnames `b_w' = `newnames'
		mat score double `xb_w' = `b_w' if `touse'

				/* estimate sigma_1^2 */
		reg `di' `zvar1' `zvar2' (`xvar1' `zvar1') `wtopt' /*
			*/ if `touse'
		predict double `zg', xb

		gen double `res' = `yvar' - `xb_w' - `zg' if `touse'
		by `touse' `ivar': gen double `res2' = sum(`res') /*
			*/ if `touse'
		by `touse' `ivar': replace `res2' = (`res2'[_N]/`Ti'[_N])^2
		summ `res2' `wtopt', meanonly 
		tempname sigma12 sigmaU2
		scalar `sigma12' = r(sum)/`N'

				/* Generate theta=sigma_u/sigma_1 */
		scalar `sigmaU2' = cond( ((`sigma12'-`sigmaV2')/`Tbar') > 0, /*
			*/ (`sigma12'-`sigmaV2')/`Tbar', 0 )
		if `isT_cons' {
			tempname theta 
			if `sigmaU2' == 0 { 
				scalar `theta' = 0 
			}
			else { 
				scalar `theta' = 1 - /*
					*/ sqrt(`sigmaV2'/`sigma12') 
			}
		}
		else {
			tempvar theta
			if `sigmaU2' == 0 { 
				gen byte `theta' = 0 
			}
			else { 
				gen double `theta' = 1 - /*
					*/ sqrt(`sigmaV2'/(`Ti'*`sigmaU2' /*
					*/ +`sigmaV2')) if `touse' 
			}
		}

				/* GLS transformation */
		tempvar yvar_g
		gen double `yvar_g' = `yvar' - `theta'*`yvar_m' if `touse'
		local list "xvar1 xvar2 zvar1 zvar2"
		foreach var of local list {
			local k = 1
			foreach i of local `var' {
				tempvar `var'`k'_g
				if "`var'"!="zvar1" & "`var'"!="zvar2" {
					gen double ``var'`k'_g' = `i' /*
						*/ - `theta'*``var'`k'_m' /*
						*/ if `touse'
				}

⌨️ 快捷键说明

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