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

📄 estimates.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
📖 第 1 页 / 共 2 页
字号:

			if "`name'" != "." {
				est_hold `name' `esample'
			}
			else {
				_est hold `hcurrent', restore nullok estsystem
			}
		}
		if `rc' {
			exit `rc'
		}
	}
end


/*
	subcmd dir [name-list]
*/
program define Dir, rclass
	syntax [anything] [, Width(int -1) *]

	est_expand `"`anything'"', default(_all)
	local names `r(names)'
	if "`names'" == "" {
		exit
	}

	// store in locals cmd<i>, depvar<i>, title<im>, and npar<i>
	// set lent = max(length(title))

	tempname b hcurrent esample
	_est hold `hcurrent', restore nullok estsystem

	local lent 0
	local im 0
	foreach name of local names {
		local ++im

		nobreak {
			if "`name'" != "." {
				est_unhold  `name' `esample'
			}
			else {
				_est unhold `hcurrent'
			}

			local cmd`im' `e(cmd)'
			if "`e(cmd2)'" != "" {
				local cmd`im' `e(cmd2)'
			}
			local depvar`im' `e(depvar)'
			local title`im'  `"`e(_estimates_title)'"'
			matrix `b' = e(b)
			local npar`im' = colsof(`b')

			if "`name'" != "." {
				est_hold `name' `esample'
			}
			else {
				_est hold `hcurrent', restore nullok estsystem
			}
		}
		local lent = max(`lent', length(`"`title`im''"'))
	}
	local lent = max(`lent', 8)


	if `width' == -1 {
		// adjust to linesize and title length
		local lpiece = min(`: set linesize' - 47, `lent')
		local lent = 33 + `lpiece'
	}
	else {
		local width = max(`width', 60)
		// fixed width table
		local lpiece = `width' - 48
		local lent   = 33 + `lpiece'
	}


	di _n as txt "{hline 13}{c TT}{hline `lent'}"
	di as txt "model        {c |} command      depvar       npar  title "
	di as txt "{hline 13}{c +}{hline `lent'}"

	local im 0
	foreach name of local names {
		local ++im

		local ndp  : word count `depvar`im''
		local depn = cond(`ndp' == 0, "no depvar", ///
				  cond(`ndp' == 1, ///
				    abbrev("`depvar`im''",12), "mult. depvar"))
		if "`name'" != "." {
			local abname = abbrev("`name'", 12)
			local clicktxt "{stata estimates replay `name':`abname'}"
		}
		else {
			local clicktxt .
		}
		local ti : piece 1 `lpiece' of `"`title`im''"'

		di as txt   "{ralign 12:`clicktxt'}"             ///
		            _col(14) "{txt:{c |}}"               ///
		   as res   _col(16) "{lalign 12:`cmd`im''}"     ///
		            _col(29) "{lalign 12:`depn'}"        ///
		            _col(42) %4.0f `npar`im''            ///
		            _col(48) `"{it:`ti'}"'

		// remainder of title
		local j 2
		local ti : piece `j' `lpiece' of `"`title`im''"'
		while `"`ti'"' != "" {
			di _col(14) "{txt}{c |}" _col(48) `"{res}{it:`ti'}"'
			local ++j
			local ti : piece `j' `lpiece' of `"`title`im''"'
		}
	}
	di as txt "{hline 13}{c BT}{hline `lent'}"

	return local names `names'
end


/*
	subcmd stats [name-list]
*/
program define Stats, rclass
	syntax [anything] [, /// 
		df(str)      /// undocumented
	] 

	est_expand `"`anything'"', default(_all)
	local names `r(names)'
	if `"`names'"' == "" {
		exit
	}
	
	if `"`df'"' != "" { 
		capture confirm number `df' 
		local df_is_number = _rc==0
	}	

	tempname hcurrent S esample

	local nnames : word count `names'
	matrix `S' = J(`nnames', 6, .)

	_est hold `hcurrent', restore nullok estsystem
	local is 0
	foreach name of local names {
		nobreak {
			if "`name'" != "." {
				est_unhold  `name' `esample'
			}
			else {
				_est unhold `hcurrent'
			}

			local ++is
			matrix `S'[`is',1] = e(N)
			matrix `S'[`is',2] = e(ll_0)
			matrix `S'[`is',3] = e(ll)

			if `"`df'"' == "" {  
				GetRankV `name'
				matrix `S'[`is',4] = r(rankV) // == df
			}
			else if `df_is_number' { 
				matrix `S'[`is',4] = `df' 
			}
			else { 
				capture confirm number `e(`df')' 
				if _rc==0 { 
					matrix `S'[`is',4] = e(`df')  
				}
			}
			
			if "`name'" != "."{
				est_hold `name' `esample'
			}
			else {
				_est hold `hcurrent', restore nullok estsystem
			}
		}

		// AIC = -2*ll + 2*df
		matrix `S'[`is',5] = -2*`S'[`is',3] + 2*`S'[`is',4]

		// BIC = -2*ll + log(N)*edf
		matrix `S'[`is',6] = -2*`S'[`is',3] + log(`S'[`is',1])*`S'[`is',4]
	}


	di _n as txt "{hline 13}{c TT}{hline 64}"
	di as txt "       Model {c |}    Obs    ll(null)   ll(model) " ///
	          "    df          AIC         BIC"
	di as txt "{hline 13}{c +}{hline 64}"

	local is 0
	foreach name of local names {
		local ++is
		if "`name'" != "." {
			local abname = abbrev("`name'",12)
			local click "{stata estimates replay `name':`abname'}"
		}
		else {
			local click .
		}
		di as txt "{ralign 12:`click'}"         ///
		          _col(14) "{c |}"              ///
		   as res _col(17)  %5.0f  `S'[`is',1]  ///
		          _col(25)  %9.0g  `S'[`is',2]  ///
		          _col(37)  %9.0g  `S'[`is',3]  ///
		          _col(48)  %5.0f  `S'[`is',4]  ///
		          _col(57)  %9.0g  `S'[`is',5]  ///
		          _col(69)  %9.0g  `S'[`is',6]
	}
	di as txt "{hline 13}{c BT}{hline 64}"

	mat colnames `S' = N ll0 ll df AIC BIC
	mat rownames `S' = `names'
	return matrix S `S'
end


/*
	subcmd drop name-list
*/
program define Drop
	syntax anything

	est_expand `"`anything'"'
	local names `r(names)'

	foreach name of local names {
		if `"`name'"' != "." {
			_est drop `name'
		}
		if `"`name'"' == "`e(_estimates_name)'" {
			ClearFields
		}
	}
end


/*
	subcmd clear

	only results that were -estimate store-d are dropped
*/
program define Clear
	* get appropriate err msg if cmdline is specified
	syntax

	Drop _all
end


// ============================================================================
// subroutines
// ============================================================================


program define CheckName
	args name

	if `"`e(_estimates_name)'"' != "`name'" {
		di as err "estimation results for `name'" /*
		*/ " are not stored via {cmd:estimates}"
		exit 198
	}
end


program define ChangeFields, eclass
	syntax [, title(str) scorevars(str) ]

	if `"`scorevars'"' != "" {
		eret local scorevars  `"`scorevars'"'
	}
	if `"`title'"' != "" {
		eret local _estimates_title  `"`title'"'
	}
end


program define ClearFields, eclass
	eret local _estimates_title ""
	eret local _estimates_name ""
end


/* GetRankV name

   returns in r(rankV) the dimension of a model, determined as the rank of
   the (co)variance matrix of the coefficients.

   rank determination via -syminv-
*/
program define GetRankV, rclass
	args name

	tempname r V

	scalar `r' = e(rank)
	if `r' >= . {
		capt mat `V' = syminv(e(V))
		local rc = _rc
		if `rc' == 111 {
			dis as txt "(`name' does not contain matrix e(V); rank = 0 assumed)"
			scalar `r' = 0
		}
		else if `rc' != 0 {
			// reproduce error message
			mat `V' = syminv(e(V))
		}
		else {
			scalar `r' = colsof(`V') - diag0cnt(`V')
		}
	}
	return scalar rankV = `r'
end


/* Header style

   displays a replay - header for estimation results name (possibly .)
   style = title forces a compact header
*/
program define Header
	args style

	if "`e(_estimates_name)'" != "" {
		local txt "Model {hi:`e(_estimates_name)'}"
		if `"`e(_estimates_title)'"' != "" {
			local txt "`txt' ({it:`e(_estimates_title)'})"
		}
	}
	else {
		local txt "active results"
	}

	if "`style'" == "title" {
		di _n(2) as txt `"{title:`txt'}"'
	}
	else {
		di _n as txt "{hline}"
		di as txt `"{p 0 8}`txt'{p_end}"'
		di as txt "{hline}"
	}
end


/* Replay_u dopt

   replays the last estimation command, with display options dopt
*/
program define Replay_u

	if "`e(cmd2)'" != "" {
		// try e(cmd2)
		version $T_EST_VERSION:  `e(cmd2)' , `0'
	}
	else if "`e(cmd)'" != "" {
		// try e(cmd)
		version $T_EST_VERSION:  `e(cmd)' , `0'
	}
	else {
		di as err "impossible to replay estimation command"
		exit 198
	}
end


/* SubcmdMoved subcmd cmd

   reports that subcommand subcmd was moved to the command cmd
*/
program define SubcmdMoved
	args subcmd cmd

	di as err "the subcommand {cmd:`subcmd'} has been moved " ///
	          "from {cmd:estimates} to {cmd:`cmd'}"
	exit 198
end


program define TooManyModels
	di _n as err "system limit exceeded"
	di as err "you need to drop one or more models"
	est dir _all
end

exit

⌨️ 快捷键说明

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