⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xttobit.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
字号:
*! version 1.5.10 24jun2005
program xttobit, eclass byable(onecall)
        version 6, missing
	if replay() {
                if "`e(cmd)'" != "xttobit" { error 301 }
		if _by() { error 190 }
                Display `0'
                exit
        }
	syntax [varlist(ts)] [if] [in] [iweight] [, ///
		INTPoints(int 12) Quad(int 12) *]

	/* version 6 so local macros restricted to 7 characters */
	if `intpoin' != 12 {
		if `quad' != 12 {
			di as err "intpoints() and quad() may not be specified together"
			exit(198)
		}
		local options `options' quad(`intpoin')
	}
	else {
		local options `options' quad(`quad')
	}

	local XT_VV : di "version " string(_caller()) ", missing: "
	if _by() {
        	local myby by `_byvars'`_byrc0':
	}
	`XT_VV' `myby' _vce_parserun xttobit, panel : `0'
	if "`s(exit)'" != "" {
		exit
	}

	if (_caller()<9) {
		`XT_VV' `myby' xttobit_8 `varlist' `if' `in'  ///
			[`weight'`exp'],`options'
	} 
	else `myby' Estimate `varlist' `if' `in' [`weight'`exp'],	///
	`options' 
end

program Estimate, eclass byable(recall) sort
	#delimit ;
	syntax [varlist(ts)] [if] [in] [iweight] 
			[, I(varname) RE FE PA noSKIP noCONstant tobit
			OFFset(varname num) FROM(string) Quad(int 12) 
			Level(passthru) LL(string) UL(string) noLOg 
			INTMethod(string) *] ;
	#delimit cr

	if "`intmeth'" != "" {
		local len = length("`intmeth'")
		if "`intmeth'" != substr("ghermite",1,max(2,`len')) & 	///
			"`intmeth'" != substr("aghermite",1,max(3,`len')) {
			di as err "intmethod() must be either ghermite or aghermite"
			exit(198)
		}
		if "`intmeth'" == substr("ghermite",1, max(2,`len')) {
			local stdquad stdquad
		} 
	}
	
	mlopts mlopt, `options'

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

	if "`offset'" != "" {
		tempvar ovar
		gen double `ovar' = `offset'
		local oarg "offset(`ovar')"
	}
	if "`stdquad'"=="" {
		tempvar shat hh
		generate double `shat' = 1
		generate double `hh' = 0
		global XTI_shat `shat' /*set global with shat variable for adapquad */
		global XTI_hh `hh' /*set global with hh variable for adaquad */
		global XTI_noadap 
	}
	else {
		global XTI_noadap noadap
	}
	tempvar qavar qwvar
	qui gen double `qavar' = .
	qui gen double `qwvar' = .
	global XTI_quad `quad'
	global XTI_qavar `qavar'
	global XTI_qwvar `qwvar'

	if "`fe'" != "" {	
		di in red "Fixed-effects model not available"
		exit 198
	}
	if "`pa'" != "" {	
		di in red "Population-averaged model not available"
		exit 198
	}

        if `quad' < 4 | `quad' > 195 {
                di in red "number of quadrature points must be between 4 and 195"

                exit 198
        }

	quietly {
                /* NOTE:  VERY VERY VERY carefully mark the sample
                        Remember that there are two dependent variables
                        where one of them could be missing to denote
                        censoring.
                */

		tempvar touse
		mark `touse' `if' `in' [`weight'`exp']
		markout `touse' `ovar'
		markout `touse' `ivar', strok

		tokenize `varlist'
		local depn "`1'"
		local depnam `depn'
		local depstr : subinstr local depnam "." "_"
		
		mac shift 
		local ind "`*'"
		local indnam `ind'
		_find_tsops `varlist'
		if `r(tsops)' {
			qui tsset
			tsrevar `depn'
			local depn `r(varlist)'
			tsrevar `ind'
			local ind `r(varlist)'
		}			
		tempvar dep1 dep2
		gen double `dep1' = `depn'
		gen double `dep2' = `depn'
		if "`ll'" != "" {
			replace `dep1' = .    if `depn' <= `ll'
			replace `dep2' = `ll' if `depn' <= `ll'
			local llopt `"`ll'"'
		}
		else {
			local ll = .
		}
		if "`ul'" != "" {
			replace `dep2' = .    if `depn' >= `ul'
			replace `dep1' = `ul' if `depn' >= `ul'
			local ulopt `"`ul'"'
		}
		else {
			local ul = .
		}
		count if `dep1' > `dep2' & `dep1'<. & `dep2'<. 
		if r(N) > 0 {
			di in red "ll() must be less than ul()"
			exit 198
		}

		replace `touse' = 0 if `dep1'>=. & `dep2'>=. 
		markout `touse' `ind'

		/* Sample is now set */


		count if `touse'
                if `quad' > r(N) {
                        noi di in red "number of quadrature points " /*
                                */ "must be less than or equal to " /*
                                */ "number of obs"
                        exit 198
                }

		if "`constan'`from'" != "" {
			local skip ""
		}
		local oldind `ind'
		qui _rmcoll `ind' if `touse', `constan' 
		local ind "`r(varlist)'"
		local dropped : list oldind - ind
		foreach x of local dropped {
			local j : list posof "`x'" in oldind
			local y : word `j' of `indnam' 
			noi di as txt "note: `y' dropped due to collinearity"
		}
		local p : word count `ind' 
		tempname zero
		mat `zero' = J(1,`p',0)

                if "`log'" == "" { local lll "noisily" }
                else             { local lll "quietly" }


                local k 1
                while `k' <= `p' {
                        local wrd : word `k' of `ind'
                        local names "`names' `depn':`wrd'"
                        local k = `k'+1
                }

		local suse "sigma_u:_cons sigma_e:_cons"
		if "`constan'" == "" {
			local names  `names' `depn':_cons `suse'
		}
		else {
			local names  `names' `suse'
		}

                if "`from'" != "" {
                        local arg `from'
                        tempname from
                        noi _mkvec `from', from(`arg') colnames(`names') /*
                                */ error("from()")
                }

		tempname beta beta0
		if "`from'" == "" & "`tobit'" != "" {
			`lll' di in gr _n "Fitting comparison model:"
			`lll' intreg `dep1' `dep2' `ind' /*
				*/ if `touse', `oarg' nodisplay `constan'
			local llprob = e(ll)  
			mat `beta' = e(b)
			local j = colsof(`beta')
			local se = `beta'[1,`j']
			local jm1 = `j'-1
		}
		if "`from'" != "" {
			mat `beta' = `from'
                        local refine "norefine"
			noi di
		} 
		else {
                        `lll' di in gr _n "Obtaining starting values for " /*
                                */ "full model:" _n
                        `lll' xtreg `depn' `ind' if `touse', /*
                                */ i(`ivar') mle nodisp skip `constan'
                        mat `beta' = e(b)
		}


                tempvar T
                sort `touse' `ivar'
                if "`weight'" != "" {
                        tempvar wv
                        gen double `wv' `exp'
                        _crcchkw `ivar' `wv' `touse'
                        drop `wv'
                }
                by `touse' `ivar': gen int `T' = _N if `touse'
                summarize `T' if `touse' & `ivar'~=`ivar'[_n-1], meanonly
                local ng = r(N)
                local g1 = r(min)
                local g2 = r(mean)
                local g3 = r(max)

                if "`skip'" != "" {
                        tempvar ys
                        gen double `ys' = (`dep1'+`dep2')/2
                        replace `ys' = `dep1' if `dep1'<. & `ys'>=.
                        replace `ys' = `dep2' if `dep2'<. & `ys'>=.
                        `lll' di in gr _n "Obtaining starting values for " /*
                                */ "constant-only model:" _n
                        `lll' xtreg `ys' if `touse', i(`ivar') mle nodisp skip
                        mat `beta0' = e(b)
                }


		tempvar w
		if "`weight'" == "" {
			gen double `w' = 1 if `touse'
		}
		else {
			gen double `w' `exp' if `touse'
		}

		tempvar cvar
		global XTI_cvar `cvar'
		gen byte `cvar' = 1 if `dep1'==`dep2'
		replace `cvar' = 2 if `dep1'>=. & `dep2'<.
		replace `cvar' = 3 if `dep1'<. & `dep2'>=.
		replace `cvar' = 4 if `dep1'<. & `dep2'<. & `dep1'<`dep2'
		replace `cvar' = 0 if `touse' == 0
		count if `dep1' > `dep2' & `cvar' == 4 
		if r(N) {
			di in red "`dep1' must be less or equal to `dep2'"
			exit 198
		}
                count if `touse' & `cvar' == 1
                local N_c1 = r(N)
                count if `touse' & `cvar' == 2
                local N_c2 = r(N)
                count if `touse' & `cvar' == 3
                local N_c3 = r(N)
		
		sort `touse' `ivar' `cvar' `dep1' `dep2' `ind'
		tempvar p
		gen long `p' = _n*`touse'
		summ `p' if `touse', meanonly
		local j0 = r(min)
		local j1 = r(max)
		local ca "cvar(`cvar')"
	}
	_GetQuad, avar(`qavar') wvar(`qwvar') quad(`quad')
	if "`skip'" != "" {
		`lll' di in green _n "Fitting constant-only model:"
		if "`stdquad'"=="" {
			_GetAdap `dep1' `dep2' in `j0'/`j1',		///
			shat(`shat') hh(`hh') 				///
			ivar(`ivar') b(`beta0') quad(`quad') `options' 	///
			intreg `ca'
		}
		`lll' ml model d2 xtintreg_d2 				   ///
		(`dep1' `dep2' = , `constan' `oarg') /sigu /sige 	   ///
		[iw=`w'] in `j0'/`j1', init(`beta0', copy)        	   ///
		maximize `nolog' `options' missing search(off) nopreserve
		local options `options' continue
	}
	`lll' di in green _n "Fitting full model:" 
	if "`stdquad'"=="" {
		_GetAdap `dep1' `dep2' `ind' in `j0'/`j1', 		///
		shat(`shat') hh(`hh') 					///
		intreg ivar(`ivar') b(`beta') `constan' `oarg' `ca'	
	}
	`lll' ml model d2 xtintreg_d2 					 ///
		(`dep1' `dep2' = `ind', `constan' `oarg') /sigu /sige    ///
		[iw=`w'] in `j0'/`j1', init(`beta', copy) `log' maximize ///
		`options' missing search(off) nopreserve
	est local cmd
	est local r2_p
	est local offset
	est local intmethod "aghermite"
        if "`stdquad'" !="" {
		est local intmethod "ghermite"
	}
	est local distrib "Gaussian"
	est local title   "Random-effects tobit regression"
        est local wtype   "`weight'"
        est local wexp    "`exp'"
	est local depvar  "`depnam'"
	est local ivar    "`ivar'"
	est scalar n_quad = `quad'
        est scalar N_g    = `ng'
        est scalar g_min  = `g1'
        est scalar g_avg  = `g2'
        est scalar g_max  = `g3'
        est scalar N_c1   = `N_c1'
        est scalar N_c2   = `N_c2'
        est scalar N_c3   = `N_c3'

        tempname b v

        mat `b' = e(b)
        mat `v' = e(V)
        local nc1 = colsof(`b')
        local nc = `nc1'-1
        local su = `b'[1,`nc']
        if `su' < 0 {
                mat `b'[1,`nc'] = -`su'
                local i 1
                while `i' < `nc' {
                        mat `v'[`i',`nc'] = -`v'[`i',`nc']
                        mat `v'[`nc',`i'] = `v'[`i',`nc']
                        local i = `i'+1
                }
                mat `v'[`nc',`nc1'] = -`v'[`nc',`nc1']
                mat `v'[`nc1',`nc'] = `v'[`nc',`nc1']
        }
        local nc = `nc1'
        local se = `b'[1,`nc']
        if `se' < 0 {
                mat `b'[1,`nc'] = -`se'
                local i 1
                while `i' < `nc' {
                        mat `v'[`i',`nc'] = -`v'[`i',`nc']
                        mat `v'[`nc',`i'] = `v'[`i',`nc']
                        local i = `i'+1
                }
        }
        
        local names : subinstr local names "`depn'" "`depstr'", all
        foreach x of local ind {
        	local j : list posof "`x'" in oldind
        	local x2 : word `j' of `indnam'
        	local names : subinstr local names "`x'" "`x2'"
        }
        mat colnames `b' = `names'
        mat colnames `v' = `names'
        mat rownames `v' = `names'
        est post `b' `v', noclear dep(`depnam')
        if "`llprob'" != "" {
                est scalar ll_c   = `llprob'
		est scalar chi2_c = cond([sigma_u]_b[_cons]<1e-5, 0, /*
                        */ abs(-2*(e(ll_c)-e(ll))))
                est local chi2_ct "LR"
        }
        else {
                qui test [sigma_u]_cons = 0
                est scalar chi2_c  = r(chi2)
                est local chi2_ct  "Wald"
        }
        est scalar sigma_u = [sigma_u]_b[_cons]
        est scalar sigma_e = [sigma_e]_b[_cons]
        est scalar rho = e(sigma_u)^2/(e(sigma_u)^2+e(sigma_e)^2)
	est local offset1  "`offset'"
	if `"`llopt'"' != "" {
		est local llopt `"`llopt'"'
	}
	if `"`ulopt'"' != "" {
		est local ulopt `"`ulopt'"'
	}
	est local predict "xttobit_p"
	est local cmd	  "xttobit"
        DispTbl, `level'
	DispLR
end
	
program Display
	DispTbl `0'
	DispLR
end


program DispTbl
	syntax [, Level(cilevel)]

	tempname b
	mat `b' = e(b)
	local su = [sigma_u]_b[_cons]
	local se = [sigma_e]_b[_cons]
	local bb = `su'^2/(`su'^2+`se'^2)
        _crcphdr
        est di, first plus level(`level')
        _diparm sigma_u, level(`level')
        _diparm sigma_e, level(`level')
	di in gr in smcl "{hline 13}{c +}{hline 64}"
        _diparm sigma_u sigma_e, label(rho) func(@1^2/(@1^2+@2^2)) /*
                */ der( 2*@1*(@2/(@1^2+@2^2))^2 -2*@2*(@1/(@1^2+@2^2))^2 ) /*
                */ ci(probit)
	di in gr in smcl "{hline 13}{c BT}{hline 64}"
end


program DispLR
        if "`e(ll_c)'" == "" {
                *di in green "Wald test of sigma_u=0:             " _c
                *di in green "chi2(" in ye "1" in gr ") = " /*
                        */ in ye %8.2f e(chi2_c) _c
                *di in green "    Prob > chi2 = " in ye %6.4f /*
                        */ chiprob(1,e(chi2_c))
        }
        else {
		tempname pval
                scalar `pval' =  chiprob(1, e(chi2_c))*0.5
                if e(chi2_c)==0 { scalar `pval'= 1 }
		if ((e(chi2_c) > 0.005) & (e(chi2_c)<1e4)) | (e(chi2_c)==0) {
                        local fmt "%8.2f"
                }
                else    local fmt "%8.2e"
                di in green "Likelihood-ratio test of sigma_u=0: " _c
                di in green in smcl "{help j_chibar##|_new:chibar2(01)=}" /*
                        */ in ye `fmt' e(chi2_c) _c
                di in green " Prob>=chibar2 = " in ye %5.3f /*
                        */ `pval'
        }

        di
	censobs_table e(N_c1) e(N_c2) e(N_c3)
        if e(N_cd) < . {
                if e(N_cd) > 1 { local s "s" }
                di in gr _n /*
                */ "(note: `e(N_cd)' completely determined panel`s')"
        }
end
exit

⌨️ 快捷键说明

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