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

📄 ztp.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
字号:
*! version 3.2.17  21jun2005
program ztp, byable(onecall) prop(irr ml_score svyb svyj svyr)
	version 8
        if _by() {
        	local BY `"by `_byvars'`_byrc0':"'
        }
        `BY' _vce_parserun ztp : `0'
        if "`s(exit)'" != "" {
                exit
        }

        local version : di "version " string(_caller()) ":"
        if replay() {
                if ("`e(cmd)'" != "ztp") error 301
                if (_by()) error 190
                Display `0'
                error `e(rc)'
                exit
        }

        `version' `BY' Estimate `0'
end

program Estimate, eclass byable(recall)
	version 8

/* Parse. */
      
	syntax varlist(numeric ts) [fw pw iw] [if] [in] [, IRr /*
	*/ FROM(string) Level(cilevel) OFFset(varname numeric ts) /*
	*/ Exposure(varname numeric ts) noCONstant Robust CLuster(varname) /*
	*/ SCore(string) noLOg noDISPLAY CRITTYPE(passthru) * ]

	if _by() {
		_byoptnotallowed score `"`score'"'
	}

	mlopts mlopts, `options'
	local cns `s(constraints)'
	local mlopts `mlopts' `crittype'

/* Check syntax. */

	if `"`score'"'!="" {
		confirm new variable `score'
		local nword : word count `score'
		if `nword' > 1 {
			di as error "score() must contain the name of only " /*
			*/ "one new variable"
			exit 198
		}
		tempvar scvar
		local scopt score(`scvar')
	}
	if "`offset'"!="" & "`exposure'"!="" {
		di as error "only one of offset() or exposure() can be " /* 
		*/"specified"
		exit 198
	}
	if "`constant'"!="" {
		local nvar : word count `varlist'
		if `nvar' == 1 {
			di as error "independent variables required with " /*
			*/ "noconstant option"
			exit 100
		}
	}

/* Mark sample except for offset/exposure. */

	marksample touse

	if `"`cluster'"'!="" {
		markout `touse' `cluster', strok
		local clopt cluster(`cluster')
	}

/* Process offset/exposure. */

	if "`exposure'"!="" {
		capture assert `exposure' > 0 if `touse'
		if _rc {
			di as error "exposure() must be greater than zero"
			exit 459
		}
		tempvar offset
		qui gen double `offset' = ln(`exposure')
		local offvar "ln(`exposure')"
	}

	if "`offset'"!="" {
		markout `touse' `offset'
		local offopt "offset(`offset')"
		if "`offvar'"=="" {
			local offvar "`offset'"
		}
	}

/* Count obs and check for no-positive values of `y'. */

	gettoken y xvars : varlist
	tsunab y : `y'
        local yname : subinstr local y "." "_"

	if "`weight'"!="" {
		if "`weight'"=="fweight" {
			local wt `"[fw`exp']"'
		}
		else local wt `"[aw`exp']"'
	}

	summarize `y' `wt' if `touse', meanonly

	if (r(N) == 0)  error 2000 
	if (r(N) == 1)  error 2001 

	if r(min) <= 0 {
		di as error "`y' must be greater than zero"
		exit 459
	}

	tempname mean nobs
	scalar `mean' = r(mean)
	scalar `nobs' = r(N) /* #obs for checking #missings in calculations */

/* Check whether `y' is integer-valued. */

	if "`display'"=="" {
		capture assert `y' == int(`y') if `touse'
		if _rc {
			di in gr "note: you are responsible for " /*
			*/ "interpretation of non-count dep. variable"
		}
	}


/* Get lf0 = constant-only model log likelihood. */

	if "`constant'`cns'"=="" & "`cluster'"=="" & "`weight'"!="pweight" {
				/* pseudo-R2 is OK for robust */
		tempname c
		if "`offset'"!="" {
			SolveC `y' `offset' [`weight'`exp'] if `touse', /*
			*/ n(`nobs') mean(`mean')
			scalar `c' = r(_cons)
			if `c'>=. {
				di in gr /*
		                */ "note: exposure = exp(`offvar') overflows;"/*				*/ _n /*
	                	*/ "      could not estimate constant-only " /*
                                */ "model" _c
				if "`exposure'"=="" {
					di in gr _n /*
	       				*/ "      use exposure() option for "/*
                                        */ "exposure = `offvar'"
				}
				else 	di in gr 
				local cfail 1
			}
			local c "`c'+`offset'"
		}
		else	scalar `c' = ln(`mean')

		if "`cfail'"=="" {
			LikePois `y' `c' [`weight'`exp'] if `touse', n(`nobs')
			if r(lnf)<. {
				local ll0 "lf0(1 `r(lnf)')"
			}
			else {
				di in gr "note: could not compute " /*
				*/ "constant-only model log likelihood"
			}
		}
	}

/* Remove collinearity. */

	_rmcoll `xvars' [`weight'`exp'] if `touse', `constant'
	local xvars `r(varlist)'

/* Get initial values. */

	if "`from'"=="" {
		Ipois `y' `xvars' [`weight'`exp'] if `touse', /*
		*/ n(`nobs') mean(`mean') `constant' `offopt'

		if "`r(b0)'"!="" {
			tempname from
			mat `from' = r(b0)
		}
	}
	if "`from'"!="" {
		local initopt `"init(`from') search(off)"'
	}
	else local initopt "search(on) maxfeas(50)"

	ml model d2 trpoiss_d2 (`yname': `y' = `xvars', `constant' `offopt') /*
	*/ [`weight'`exp'] if `touse', collinear missing max nooutput /*
	*/ nopreserve `mlopts' title(Zero-truncated Poisson regression) /*
        */ `scopt' `robust' /*
	*/ `clopt' `log' `initopt' `ll0'

	if "`score'" != "" {
		label var `scvar' "Score index from poisson"
		rename `scvar' `score'
		eret local scorevars `score'
	}

	eret scalar r2_p = 1 - e(ll)/e(ll_0)
	eret local offset  "`offvar'"
	eret local offset1 /* erase; set by -ml- */
	eret local predict "ztp_p"
	eret local cmd     "ztp"


	if "`display'"=="" {
		Display, `irr' level(`level')
	}
	error `e(rc)'

end

program Display
	syntax [, Level(cilevel) IRr]
	if "`irr'"!="" {
		local eopt "eform(IRR)"
	}

	version 9: ml di, level(`level') `eopt' nofootnote
	_prefix_footnote
end

program LikePois, rclass
	gettoken y  0 : 0
	gettoken xb 0 : 0
	syntax [fw aw pw iw] [if] , Nobs(string)
	if "`weight'"!="" {
		if "`weight'"=="fweight" {
			local wt `"[fw`exp']"'
		}
		else	local wt `"[aw`exp']"'
	}
	tempvar lnf
	qui gen double `lnf' = `y'*(`xb')-exp(`xb')-lngamma(`y'+1) /*
           */ -ln(1-exp(-exp(`xb'))) `if'
	summarize `lnf' `wt' `if', meanonly
	if (r(N) != `nobs')  exit
 
	ret scalar lnf = r(sum)
end

program SolveC, rclass /* note: similar code is found in nbreg.ado */
	gettoken y  0 : 0
	gettoken xb 0 : 0
	syntax [fw aw pw iw] [if] , Nobs(string) Mean(string)
	if "`weight'"=="pweight" | "`weight'"=="iweight" {
		local weight "aweight"
	}
	capture confirm variable `xb'
	if _rc {
		tempvar xbnew
		qui gen double `xbnew' = (`xb') `if'
		local xb `xbnew'
	}
	summarize `xb' [`weight'`exp'] `if', meanonly
	if (r(N) != `nobs') exit 
	if (r(max) - r(min) > 2*709 ) { /* unavoidable exp() over/underflow */
		exit /* r(_cons) is missing */
	}
	if r(max) > 709 | r(min) < -709  {
		tempname shift
		if r(max) > 709 { scalar `shift' =  709 - r(max) }
		else		  scalar `shift' = -709 - r(min)
		local shift "+`shift'"
	}
	tempvar expoff
	qui gen double `expoff' = exp(`xb'`shift') `if'
	summarize `expoff' [`weight'`exp'], meanonly
	if (r(N) != `nobs')  exit  /* should not happen */
	return scalar _cons = ln(`mean')-ln(r(mean))`shift'
end

program Ipois, rclass
	syntax varlist(ts) [fw aw pw iw/] [if] , Nobs(string) Mean(string) /*
	*/ [ noCONstant OFFset(string) ]
	gettoken y xvars : varlist

	tempvar xb z
	tempname b1 b2 lnf1

	if "`weight'"!="" {
		local awt `"[aw=`exp']"'
		local wt  `"(`exp')*"'
		local exp `"=`exp'"'
	}

	quietly {

	/* Initial values: b1 = b/mean, where b are coefficients
	   from reg `y' `xvars' and mean is mean of `y'.
	*/
		if "`offset'"!="" {
			tempvar ynew
			gen double `ynew' = `y' - `offset'*`mean' `if'
			local poff "+`offset'"
		}
		else	local ynew `y'

		reg `ynew' `xvars' `awt' `if', `constant'
		mat `b1' = (1/`mean')*e(b)
		mat score double `xb' = `b1' `if'

		LikePois `y' `xb'`poff' [`weight'`exp'] `if', n(`nobs')
		scalar `lnf1' = r(lnf)
	}

	/* Solve for _cons (change) for poisson likelihood given b1. */

	if "`constant'"=="" {
		SolveC `y' `xb'`poff' [`weight'`exp'] `if', /*
		*/ n(`nobs') mean(`mean')
		tempname c
		scalar `c' = r(_cons)
		if `c'<. {
			local c "+`c'"
		}
		else	local c /* erase macro */

		LikePois `y' `xb'`poff'`c' [`weight'`exp'] `if', n(`nobs')
		tempname lnf1c
		scalar `lnf1c' = r(lnf)
	}
	else	local lnf1c .

	/* Take iteratively reweighted least-squares step to get b2. */

	capture {
		gen double `z' = `y'*exp(-(`xb'`poff'`c'))-1 /*
		*/ +`xb'`c' `if'

		reg `z' `xvars' [aw=`wt'exp(`xb'`poff'`c')] `if', /*
		*/ `constant'
		mat `b2' = e(b)
		drop `xb'
		_predict double `xb' `if'

		LikePois `y' `xb'`poff' [`weight'`exp'] `if', n(`nobs')
		tempname lnf2
		scalar `lnf2' = r(lnf)
	}
	if _rc  local lnf2 . 
	if (`lnf1'>=. & `lnf1c'>=. & `lnf2'>=.)  exit 

	if `lnf2'<.&(`lnf2'>`lnf1'|`lnf1'>=.)&(`lnf2'>`lnf1c'|`lnf1c'>=.) {
		ret matrix b0 `b2' /* `lnf2' best */
		exit
	}
	if `lnf1'<.&(`lnf1'>`lnf1c'|`lnf1c'>=.)&(`lnf1'>`lnf2'|`lnf2'>=.) {
		ret matrix b0 `b1' /* `lnf1' best */
		exit
	}

	local dim = colsof(`b1')
	mat `b1'[1,`dim'] = `b1'[1,`dim']`c'
	ret matrix b0 `b1' /* `lnf1c' best */
end

⌨️ 快捷键说明

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