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

📄 heckprob.ado

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

	version 6, missing

	if replay() {
		if "`e(cmd)'" != "heckprob" { error 301 }
		if _by() { error 190 }
		Display `0'
		exit
	}
	`BY' Estimate `0'
end

program define Estimate, eclass byable(recall)

				/* parse syntax */	

					/* Allow = after depvar */

	gettoken depvar 0 : 0 , parse(" =,[")
	tsunab depvar : `depvar'
	local tsnm = cond( match("`depvar'", "*.*"),		/*   
			*/ substr("`depvar'",			/*
			*/        (index("`depvar'",".")+1),.),	/*
			*/ "`depvar'")
	confirm variable `tsnm'
	local depvarn : subinstr local depvar "." "_"
	gettoken equals rest : 0 , parse(" =")
	if "`equals'" == "=" { local 0 `"`rest'"' }

	syntax [varlist(numeric default=none ts)] [pw iw fw] [if] [in], /*
		*/ SELect(string) [ 					/*
		*/ CLuster(varname) noCONstant FIRst FROM(string)	/*
		*/ FROM0(string) Level(cilevel) noLOg 			/*
		*/ OFFset(varname numeric ts) Robust noSKIP SCore(string) /*
		*/ CRITTYPE(passthru) * ]

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

	SelectEq seldep selind selnc seloff : `"`select'"'

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

	if "`weight'"  != "" { local wgt `"[`weight'`exp']"' }
	if "`cluster'" != "" { local clusopt "cluster(`cluster')" }
	if "`seloff'"  != "" { local soffopt "offset(`seloff')" }
	if "`offset'"  != "" { local offopt "offset(`offset')" }
	if "`first'"   == "" { local show1st "nocoef" }
	if "`log'"     != "" { local qui "quietly" }
	if "`weight'" == "pweight" | "`robust'`cluster'" != "" {
		local robust "robust"
		/* probit does not allow iweights and robust so use crittype
		 * instead of robust option */
		if `"`crittyp'"' == "" {
			local crtype crittype("log pseudolikelihood")
		}
	}
	else	local crtype `crittyp'


					/* Check syntax errors */

	if "`constant'" != "" & "`varlist'" == "" { 
		noi di in red "must specify independent variables or "	/*
			*/ "allow constant for primary equation"
		exit 198
	}

	ChkSkip skip : "`skip'" "`robust'" "`constant'" "`varlist'" "`cns'"


					/* Process scores */

	local ct : word count `score'		/* stub -- score(stub*) */
	if `ct' == 1 {
		if substr("`score'",-1,1) == "*" {
			local score = substr("`score'",1,length("`score'")-1)
			local score `score'1 `score'2 `score'3 
		}
	}

	local ct : word count `score'
	if `ct' > 0 & `ct' != 3 {
		di in red "score() requires you specify 3 new variable "  /*
			*/ "names in this case"
		exit 198
	}

	if "`score'" != "" { 
		confirm new variable `score'
		local score "score(`score')" 
	}


				/* Find estimation sample */

	if "`seldep'" == "" {
		tempname seldep
		gen byte `seldep' = `depvar' < .
		local selname "select"
	}
	else local selname : subinstr local seldep "." "_"

	marksample touse, novarlist
	markout `touse' `seldep' `selind' `seloff' `cluster', strok
	marksample touse2
	markout `touse2' `depvar' `varlist' `offset'
	qui replace `touse' = 0 if `seldep' & !`touse2'


				/* Remove collinearity */

	_rmcoll `selind' `wgt' if `touse', `selnc'
	local selind "`r(varlist)'"
	if "`selind'" == "" {
		di in red "no variables remaining in selection equation"
		exit 498
	}
	_rmcoll `varlist' `wgt' if `touse' & `seldep', `constant'
	local varlist "`r(varlist)'"

				/* Only way to check for perfect pred */

	capture probit `seldep' `selind' `wgt' if `touse', `selnc' `soffopt' 
	if _rc == 2000 {
	    di as txt "selection equation:" _c
	    probit `seldep' `selind' `wgt' if `touse', `selnc' `soffopt' 
	}
	else if _rc { 
		error _rc 
	}


				/* Check selection condition */

	qui sum `seldep' if `touse'
	if `r(N)' == `r(sum)' {
		di in red "Dependent variable never censored due to selection: "
		di in red "model simplifies to probit regression"
		exit 498
	}


				/* Get starting values, etc. */

	tempname llc b0 b0sel b00
	tempvar  nshaz 

					/* just for part of comparison LL */
					/* and to check for prefect pred */
	`qui' di in gr _n "Fitting probit model:"
	`qui' probit `depvar' `varlist' `wgt' if `touse' & `seldep', /*
		*/ `constant' `offopt' nocoef `crtype'
	scalar `llc' = e(ll)

	if "`robust'"=="" | "`from'"=="" | ("`skip'"!="" & "`from0'"=="") {
		`qui' di in gr _n "Fitting selection model:"
		`qui' probit `seldep' `selind' `wgt' if `touse',  /*
			*/ `selnc' `soffopt' `show1st' asis  `crtype'
		mat `b0sel' = e(b)
		if "`robust'" == "" { scalar `llc' = `llc' + e(ll) }

		qui predict double `nshaz', xb
		qui replace `nshaz' = normd(`nshaz') / normprob(`nshaz')
	}

	if "`robust'" == "" {
		`qui' di in gr _n "Comparison:    log likelihood = " /*
			*/ in ye %10.0g `llc' 
	}

	if "`constant'" == "" { 
		tempname one
		gen byte `one' = 1 
	}

	if "`skip'" != "" & "`from0'" == "" {
		`qui' di in gr _n "Fitting constant-only starting values:"
		`qui' probit `depvar' `one' `nshaz' `wgt' 	/*
			*/ if `touse' & `seldep', 		/*
			*/ noconstant `offopt' nocoef asis `crtype'
		MkB0 `b00' : `b0sel' `nshaz'
		local from0 "`b00', copy"
	}

	if "`from'" == "" {
		`qui' di in gr _n "Fitting starting values:"
		`qui' probit `depvar' `varlist' `one' `nshaz' `wgt'	/*
			*/ if `touse' & `seldep', 			/*
			*/ noconstant `offopt' nocoef asis `crtype'
		MkB0 `b0' : `b0sel' `nshaz'
		local from "`b0', copy"
	}

	qui regress `seldep' `wgt' if !`seldep' & `touse'
	local N_cens = e(N)


				/* ML estimation */


	if "`skip'" != "" {
		`qui' di in gr _n "Fitting constant-only model:"

		capture noi ml model lf heckp_lf 			/*
		*/ (`depvarn': `depvar' = , `offopt')			/*
		*/ (`selname': `seldep' = `selind', `selnc' `soffopt')	/*
		*/ /athrho						/*
		*/ `wgt' if `touse' , waldtest(0)			/*
		*/ collinear missing max nooutput nopreserve		/*
		*/ init(`from0') search(off) `log' `mlopts' `crittyp'

		if _rc == 1400 & "`from'" == "`b0', copy" {
			di as txt "note:  default initial values "	/*
			*/ "infeasible; starting from B=0"

			ml model lf heckp_lf				/*
			*/ (`depvarn': `depvar' = , `offopt')		/*
			*/ (`selname': `seldep' = `selind', `selnc' `soffopt')/*
			*/ /athrho					/*
			*/ `wgt' if `touse' , waldtest(0)		/*
			*/ collinear missing max nooutput nopreserve	/*
			*/ init(/athrho=0) search(off) `log' `mlopts'	/*
			*/ `crittyp'
		}
		else if _rc { 
			error _rc 
		}

		local continu "continue"
	}
	local diparm diparm(athrho, tanh label("rho"))

	`qui' di in gr _n "Fitting full model:"
	capture noi ml model lf heckp_lf 				 /*
		*/ (`depvarn': `depvar' = `varlist', `offopt' `constant') /*
		*/ (`selname': `seldep' = `selind', `selnc' `soffopt')	 /*
		*/ /athrho						 /*
		*/ if `touse' `wgt',					 /*
		*/ collinear missing max nooutput nopreserve		 /*
		*/ title(Probit model with sample selection)		 /*
		*/ `score' `robust' `clusopt'				 /*
		*/ init(`from') search(off) `continu' `log' `mlopts'	 /*
		*/ `crittyp' `diparm'

	if _rc == 1400 & "`from'" == "`b0', copy" {
		di as txt "note:  default initial values "		 /*
		*/ "infeasible; starting from B=0"

		ml model lf heckp_lf 					 /*
		*/ (`depvarn': `depvar' = `varlist', `offopt' `constant') /*
		*/ (`selname': `seldep' = `selind', `selnc' `soffopt')	 /*
		*/ /athrho						 /*
		*/ if `touse' `wgt',					 /*
		*/ collinear missing max nooutput nopreserve		 /*
		*/ title(Probit model with sample selection)		 /*
		*/ `score' `robust' `clusopt'				 /*
		*/ init(/athrho=0) search(off) `continu' `log' `mlopts'	 /*
		*/ `diparm'
	} 
	else if _rc { 
		error _rc 
	}


				/* Saved results */

	if "`robust'" == "" {		/* test of independent equations */
		est scalar ll_c = `llc'
		est scalar chi2_c = abs(-2*(e(ll)-e(ll_c)))
		est local chi2_ct "LR"
	}
	else {
		qui test [athrho]_cons = 0 
		est scalar chi2_c = r(chi2)
		est local chi2_ct "Wald"
	}
	est scalar p_c = chiprob(1, e(chi2_c))
	qui _diparm athrho, tanh
	est scalar rho = r(est)
	tokenize `e(depvar)'
	if substr("`2'", 1, 2) == "__" { est local depvar `1' }
	est scalar k_aux = 1
	est scalar N_cens = `N_cens'
	est local predict "heckpr_p"
	est local cmd "heckprob"

	Display , level(`level') 

end



/* process the selection equation
	[depvar =] indvars [, noconstant offset ]
*/

program define SelectEq
	args seldep selind selnc seloff colon sel_eqn

	gettoken dep rest : sel_eqn, parse(" =")
	gettoken equal rest : rest, parse(" =")

	if "`equal'" == "=" { 
		tsunab dep : `dep'
		c_local `seldep' `dep' 
	}
	else	local rest `"`sel_eqn'"'
	
	local 0 `"`rest'"'
	syntax [varlist(numeric default=none ts)] 	/*
		*/ [, noCONstant OFFset(varname numeric) ]

	if "`varlist'" == "" {
		di in red "no variables specified for selection equation"
		exit 198
	}

	c_local `selind' `varlist'
	c_local `selnc' `constant'
	c_local `seloff' `offset'
end


/* handle -noskip- option */

program define ChkSkip
	args newskip colon skip robust const indvars cns

	c_local `newskip' `skip'

	if "`skip'" != "" {
		if "`robust'" != "" {
			di as txt "model LR test inappropriate with " /*
				*/ "robust covariance estimates,"
			local skip
		}
		if "`const'" != "" {
			di as txt "model LR test inappropriate with " /*
				*/ "noconstant option,"
			local skip
		}
		if "`indvars'" == "" {
			di as txt "model LR test inappropriate with " /*
				*/ "constant-only model,"
			local skip
		}
		if "`robust'" != "" {
			di as txt "model LR test inappropriate with " /*
				*/ "constraints,"
			local skip
		}
		if "`skip'" == "" {
			di as txt "    option skip ignored and " /*
				*/ "performing Wald test instead"
		}
	}

	c_local `newskip' `skip'

end


/* make a Beta_0 matrix of initial values */

program define MkB0
	args b0 colon b0sel hazvar

	tempname athrho

	mat `b0' = e(b)
	local k = colsof(`b0')

	scalar `athrho' = _b[`hazvar']
	scalar `athrho' = max(min(`athrho',.85), -.85)
	scalar `athrho' = 0.5 * log((1+`athrho') / (1-`athrho'))

	mat `b0' = `b0'[1,1..`k'-1] , `b0sel' , `athrho'
end


program define Display
	syntax [, Level(cilevel) ]

	_crcshdr
	ml display , noheader level(`level') nofootnote

	if "`e(vcetype)'" != "Robust" { 
		local testtyp LR
	}
	else    local testtyp Wald
	di in gr  "`testtyp' test of indep. eqns. (rho = 0):" /*
		*/ _col(38) "chi2(" in ye "1" in gr ") = "   /*
		*/ in ye %8.2f e(chi2_c) 		     /*
		*/ _col(59) in gr "Prob > chi2 = " in ye %6.4f e(p_c)
	di in smcl in gr "{hline 78}"
	_prefix_footnote
	exit e(rc)
end

⌨️ 快捷键说明

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