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

📄 truncreg.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
📖 第 1 页 / 共 2 页
字号:
			*/ as res "      -inf" /*
			*/ as txt _col(57) `"Number of obs ="' /*
			*/ as res %7.0f e(N)
			di as txt %`crlen's "upper" " = " /*
			*/ as res "`ulopt'" `model'
        	}
		if `flag' == 0 {
			di as txt "Limit:" /*
			*/ as txt %`crlow's "lower" " = " /*
			*/ as res "`llopt'" /*
			*/ as txt _col(57) `"Number of obs ="' /*
			*/ as res %7.0f e(N)
			di as txt %`crlen's "upper" " = " /*
			*/ as res "`ulopt'" `model'
        	}
		if `flag' == 2 {
			di as txt "Limit:" /*
			*/ as txt %`crlow's "lower" " = " /*
			*/ as res "      -inf" /*
			*/ as txt _col(57) `"Number of obs ="' /*
			*/ as res %7.0f e(N)
			di as txt %`crlen's "upper" " = " /*
			*/ as res "      +inf" `model'
        	}
		di as txt `"`crtype' = "' as res %10.0g e(ll) `pvalue'
		di
		ml display, level(`level') noh
	}
	else {
		if `"`at'"' =="" {	
			Margin, level(`level') at(e(means)) 
		}
		else {
			Margin, level(`level') at(`at') atuser
                }
	}
	if "`zwarn'" != "" {
		di as txt "note: no marginal effects to display -- " _c
		di as txt "coefficients displayed instead"
	}
end


program define Margin, eclass
	syntax [,Level(cilevel) AT(string) atuser]

	local flag cond("`e(ulopt)'"=="", 1, -1) 
	if `flag'== -1 {
		local flag cond("`e(llopt)'"=="", -1,0)	
	}
	else local flag cond("`e(llopt)'"=="", 2, 1)
	local llopt `e(llopt)'
	local ulopt `e(ulopt)'
	capture di _b[_cons]
	local hascons = _rc==0
	
	if "`e(offset1)'" != "" {
		tempname A M
		if `"`e(wexp)'"' != "" {
			local ww `"[`e(wtype)'`e(wexp)']"'
		}
		qui mat ac `A' = `e(offset1)' `ww' if e(sample), noc means(`M') 
		local meanoff = `M'[1,1]
		local off `"+ `meanoff'"'
	}

	tempname bm vm b V a c xmat xb 
	tempname alpha alpha2 lambda Z z s u 
	tempname f f2 f3 f4 f5 f6 J beta b0col scol dum

	mat `xmat' = `at'
	mat `b' = get(_b) 
	mat `V' = e(V) 
	scalar `a' =colsof(`b')
	scalar `s' =`b'[1,`a'] 
	scalar `c' = colsof(`xmat')
	matrix `beta' = `b'[1,1..`c']'
	mat `xb'= `xmat'*`beta' `off'
	if `hascons' {
		mat `xb' = `xb' + _b[_cons] 
	}
       	scalar `alpha' = (`llopt' - `xb'[1,1])/`s'
       	scalar `alpha2' = (`ulopt' - `xb'[1,1])/`s'

        if `flag' == 1 {
		scalar `lambda' = normd(`alpha')/(normprob(-`alpha'))
		scalar `f' = 1-`lambda'^2 + `alpha'*`lambda'
		scalar `f2' = `lambda'*(`lambda'-`alpha')
		scalar `f2' = ((-2*`lambda' + `alpha')*`f2' ///
				     + `lambda')/`s'
		scalar `f3' = `f2'*`alpha'
        }
	else if `flag' == -1 {
		scalar `lambda' = -normd(`alpha2')/(normprob(`alpha2'))
		scalar `f' = 1-`lambda'^2 + `alpha2'*`lambda'
		scalar `f2' = `lambda'*(`lambda'-`alpha2')
		scalar `f2' = ((-2*`lambda' + `alpha2')*`f2' ///
				     + `lambda')/`s'
		scalar `f3' = `f2'*`alpha2'
	}
	else if `flag' == 0 {
		scalar `lambda' = (normd(`alpha2') - normd(`alpha')) /*
		       */ /(normprob(`alpha2') - normprob(`alpha'))
		scalar `f6' = (`alpha2' - `alpha')*normd(`alpha') /   /*
                       */   (normprob(`alpha2') - normprob(`alpha'))
		scalar `f' = 1 - `lambda'^2 - `alpha2'*`lambda' - `f6'
		scalar `f4' = (`alpha2'*normd(`alpha2') - ///
			       `alpha'*normd(`alpha')) / ///
			      (normprob(`alpha2') - normprob(`alpha')) 
		scalar `f5' = (`alpha2'*`alpha2'*normd(`alpha2') - ///
			       `alpha'*`alpha'*normd(`alpha')) / ///
			      (normprob(`alpha2') - normprob(`alpha')) 
		scalar `f2' = ((2*`lambda' + `alpha2')* ///
			      (`f4'+`lambda'^2) - `lambda' + ///
			      `f6'*(`alpha' + `lambda')) / `s'
		scalar `f3' = ((`alpha2'+2*`lambda')* ///
			       (`f5'+`lambda'*`f4') - `lambda'*`alpha2' + ///
				`f6'*(`alpha'^2 - 1 + `f4')) / `s' 
	}
	else {                          /* `flag'==2 */
		scalar `f' = 1
		scalar `f2' = 0
		scalar `f3' = 0
	}

	matrix `J' = `f'*I(`c') - `f2'*`beta'*`xmat'
	if `hascons' {
		matrix `b0col' = -`f2'*`beta'
		matrix `J' = (`J', `b0col')
	}
	matrix `scol' = -`f3'*`beta'
	matrix `J' = (`J', `scol')
	mat `bm' = `f'*`b'[1,1..`c'] 
	mat `vm' = `J'*`V'*`J''
	mat `dum' = e(dummy)
					   /* display results        */
	scalar `Z' = invnorm(1-(1-`level'/100)/2)

	if !missing(e(df_r)) {
		local model _col(57) as txt "F(" ///
			as res %3.0f e(df_m) as txt "," ///
			as res %6.0f e(df_r) as txt ")" _col(71) ///
			"=" as res %7.2f e(F)
		local pvalue _col(57) as txt "Prob > F" _col(71) ///
			"=" as res %7.4f Ftail(e(df_m),e(df_r),e(F))
	}
	else {
		if "`e(chi2type)'" == "" {
			local chitype Wald
		}
		else	local chitype `e(chi2type)'
		local model _col(57) as txt `"`chitype' chi2("' ///
			as res e(df_m) as txt ")" _col(71) ///
			"=" as res %7.2f e(chi2)
		local pvalue _col(57) as txt "Prob > chi2" _col(71) ///
			"=" as res %7.4f chiprob(e(df_m),e(chi2))
	}

	local dv = abbrev("`e(depvar)'",8) 
	di _n as txt `"Marginal effects of truncated regression"'
	di as txt "Prediction: " _c
	if `flag' == 1 {
		local ulopt .
		di as res "E(`dv' | `dv' > `llopt')
        	di as txt "Limit:   lower = " as res %10.0g `llopt' /*
		*/ as txt _col(57) `"Number of obs ="' /*
		*/ as res %7.0f e(N)
		di as txt "         upper = " as res "      +inf" `model'
       	}
	if `flag' == -1 {
		local llopt . 
		di as res "E(`dv' | `dv' < `ulopt')
		di as txt "Limit:   lower = " as res "      -inf" /*
		*/ as txt _col(57) `"Number of obs ="' /*
		*/ as res %7.0f e(N)
		di as txt "         upper = " as res %10.0g `ulopt' `model'
       	}
	if `flag' == 0 {
		di as res "E(`dv' | `llopt' < `dv' < `ulopt')
		di as txt "Limit:   lower = " as res %10.0g `llopt' /*
		*/ as txt _col(57) `"Number of obs ="' /*
		*/ as res %7.0f e(N)
		di as txt "         upper = " as res %10.0g `ulopt' `model'
       	}
	if `flag' == 2 {
		di as res "E(`dv')" 
                di as txt "Limit:   lower = " as res "      -inf" /*
                */ as txt _col(57) `"Number of obs ="' /*
                */ as res %7.0f e(N)
                di as txt "         upper = " as res "      +inf" `model'
        }

	di as txt `"Log likelihood = "' as res %10.0g e(ll) `pvalue'
	di
	if `"`e(clustvar)'"' != "" {
		di as txt %78s /*
	      */ `"(standard errors adjusted for clustering on `e(clustvar)')"'
	}
	local cil `=string(`level')'
	local cil `=length("`cil'")'
	if `cil' == 2 {
		local space1 "    "
		local space2 "   "
	}
	if `cil' == 4 {
		local space1 "   "
		local space2 "  "
	}
	if `cil' == 5 {
		local space1 "  "
		local space2 "  "
	}
	
	di in smcl as txt "{hline 9}{c TT}{hline 68}"
	di in smcl as txt /*
		*/ %8s abbrev("`e(depvar)'",8) " {c |}" /*
		*/ _col(17) `"dF/dx"' _col(25) /*
		*/ `"Std. Err."' _col(40) `"z"' _col(45) `"P>|z|"' /*
		*/ _col(55) `"X_at"'/*
		*/ _col(62) `"[`space1'`=strsubdp("`level'")'% C.I.`space2']"' _n /*
		*/ "{hline 9}{c +}{hline 68}"

        local varlist: colnames(`bm')
	tokenize `varlist'
	local i 1
	local hasdummy 0 
	while `i' <= `c' {
		local C = `bm'[1, `i']    
		local v = `vm'[`i',`i']
		if `dum'[1,`i'] & `flag' != 2 {
			local star *
			local hasdummy 1
		}
		else {
			local star " "
		}
		local s = sqrt(`v')
		local ll = `C'-`Z'*`s'
		local ul = `C'+`Z'*`s'
		local z = `C'/`s' 
		local x = `xmat'[1,`i']

		di in smcl as txt %8s abbrev("``i''",8) "`star'{c |}  " /*
			*/ as res /*
			*/ %9.0g `C' `"  "' /*
			*/ %9.0g `s' `" "' /*
			*/ %8.2f `z'  `"  "' /*
			*/ %6.3f 2*normprob(-abs(`z')) `"  "' /* 
			*/ %8.0g `x' `"  "' /*
			*/ %8.0g `ll' `" "' /*
			*/ %8.0g `ul'
		local i=`i'+1
	}
	if "`meanoff'" != "" {
		di as txt %8s abbrev("`e(offset1)'",8) " {c |}   " _c
		di as txt "(offset)" _c
		di as res _col(52) %8.0g `meanoff'	
	}
	di in smcl as txt "{hline 9}{c BT}{hline 68}"

	if `hasdummy' {
		di as txt "(*) For dummy variables, marginal effects are " _c
		di as txt "calculated assuming the "
		di as txt "    variable is continuous. " _c
		di as txt "To obtain the discrete change from 0 to 1 "
		di as txt "    for these variables, use " _c
		local command `"mfx compute, predict(e(`llopt',`ulopt'))"'
		if "`atuser'"=="" {
			di `"{stata `command':`command'}"'
		}
		else {
			di as inp "`command' at(" _c
			di as txt "{it:atlist}" _c
			di as inp ")"
		}
	}

					/* post results		*/
	est local at 
	est mat at `xmat'
	est local dfdx 
	est mat dfdx `bm'
	est local V_dfdx 
	est mat V_dfdx `vm'
	if "`meanoff'" != "" {
		est scalar m_offset = `meanoff'
	}
end

⌨️ 快捷键说明

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