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

📄 svy_estat.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
字号:
*! version 1.1.9  24feb2005
program svy_estat
	version 9

	gettoken sub 0 : 0, parse(" ,")
	local lsub : length local sub

	if "`e(cmd)'" == "" {
		error 301
	}

	if inlist(`"`sub'"', "", ",") {
		di as err "subcommand expected"
		exit 198
	}

	if `"`sub'"' == "vce" {
		estat_default vce `0'
		exit
	}
	if `"`sub'"' == "svyset" {
		RequiresSVY
		syntax
		svyset, eclass
		exit
	}
	if `"`e(cmd)'"' == "tabulate" {
		RequiresSVY
		di as err "only estat svyset is available after svy: tabulate"
		exit 198
	}
	if `"`sub'"' == "size" {
		RequiresSVY
		Size `0'
		exit
	}
	if `"`sub'"' == substr("effects",1,max(3,`lsub')) {
		RequiresSVY
		CheckPost effects
		Effects `0'
		exit
	}
	if `"`sub'"' == substr("lceffects",1,max(5,`lsub')) {
		RequiresSVY
		CheckPost lceffects
		LCEffects `0'
		exit
	}
	di as err "invalid subcommand `sub'"
	exit 321
end

program RequiresSVY
	if `"`e(prefix)'"' != "svy" {
		di as err "svy estimation results not found"
		exit 301
	}
end

program Size, rclass
	is_svysum `e(cmd)'
	if !r(is_svysum) {
		if `"`e(cmd)'"' != "" {
			di as err ///
"estimation results from svy:`e(cmd)' are not supported by estat size"
			exit 301
		}
		else {
			error 301
		}
	}
	syntax [,			///
		OBS			///
		SIZE			///
		noHeader		///
		noLegend		///
		Verbose			///
	]
	local diopts `obs' `size'
	local ndiopts : word count `diopts'
	if `ndiopts' == 0 {
		local obs obs
		local size size
	}
	tempname Obs Size
	matrix `Obs' = e(_N)
	matrix `Size' = e(_N_subp)
	if "`obs'" != "" {
		local colopts `colopts' c(`Obs' "Obs")
	}
	if "`size'" != "" {
		local colopts `colopts' c(`Size' "Size")
	}
	DisplayTable, `colopts' `legend' `verbose' size
	_prefix_footnote
	return matrix _N_subp 	= `Size'
	return matrix _N	= `Obs'
end

program Effects, rclass
	syntax [,			///
		lceffects		///
		noLegend		///
		Verbose			/// undocumented
		*			/// effects options
	]

	// NOTE: the -verbose- option is not documented because it only has an
	// effect on output when used with -svy:brr- or -svy:jackknife-

	if "`lceffects'" == "" {
		_svy_mkdeff
	}

	EffectsOpts, `options'
	local deff `s(deff)'
	local deft `s(deft)'
	local meff `s(meff)'
	local meft `s(meft)'
	local sub  `s(sub)'
	local colopts	// start empty
	local matlist deft`sub' deff`sub'

	tempname Deff`sub' Deft`sub' Meff Meft
	matrix `Deff`sub'' = e(deff`sub')
	matrix `Deft`sub'' = e(deft`sub')
	if "`lceffects'" == "" & "`meff'`meft'" != "" & `"`e(V_msp)'"' == "" {
		_svy_mkvmsp
	}
	if `"`e(meft)'"' == "matrix" {
		local matlist `matlist' meft meff
		matrix `Meft' = e(meft)
		matrix `Meff' = hadamard(`Meft',`Meft')
	}
	else {
		matrix `Meft' = .
		matrix `Meff' = .
	}
	if "`deff'" != "" {
		local colopts `colopts' c(`Deff`sub'' "Deff")
	}
	if "`deft'" != "" {
		local colopts `colopts' c(`Deft`sub'' "Deft")
	}
	if "`meff'`meft'" != "" {
		if "`meff'" != "" {
			local colopts `colopts' c(`Meff' "Meff")
		}
		if "`meft'" != "" {
			local colopts `colopts' c(`Meft' "Meft")
		}
	}
	DisplayTable, `colopts' `legend' `verbose'
	_prefix_footnote, `deff'
	tempname x
	foreach mat of local matlist {
		local Mat = proper("`mat'")
		return matrix `mat' ``Mat''
	}
end

program LCEffects, rclass
	syntax anything(name=lexp id="linear expression" equalok) [, * ]
	_svy_mkdeff
	EffectsOpts, `options'
	local deff `s(deff)'
	local deft `s(deft)'
	local meff `s(meff)'
	local meft `s(meft)'
	local sub  `s(sub)'

	local matlist deft`sub' deff`sub'
	if "`meff'`meft'" != "" & `"`e(V_msp)'"' == "" {
		_svy_mkvmsp
	}
	if `"`e(V_msp)'"' == "matrix" {
		tempname Vmsp
		matrix `Vmsp' = e(V_msp)
	}

	tempname results x b V Vsrs Deff Deft
	_est hold `results', restore copy

	_test `lexp' = 0, notest
	quietly _test `lexp' = 0
	scalar `x' = r(F)
	matrix `b' = e(b)
	matrix `V' = e(V)
	matrix `Vsrs' = e(V_srs`sub')
	if `"`e(V_srswr)'"' == "matrix" {
		tempname Vswr
		matrix `Vswr' = e(V_srs`sub'wr)
	}

	_getbv `x' `b' `V' `"`lexp'"'

	GetCVC `Vsrs' `lexp'
	matrix `Deff' = `V'[1,1]/`Vsrs'[1,1]
	if "`Vmsp'" != "" {
		GetCVC `Vmsp' `lexp'
		tempname Meft
		matrix `Meft' = sqrt(`V'[1,1]/`Vmsp'[1,1])
	}
	if "`Vswr'" != "" {
		GetCVC `Vswr' `lexp'
		matrix `Deft' = sqrt(`V'[1,1]/`Vswr'[1,1])
	}
	else	matrix `Deft' = sqrt(`Deff'[1,1])

	_est unhold `results'
	local df = e(df_r)
	if `:word count `e(depvar)'' == 1 {
		local depvar `e(depvar)'
	}
	_est hold `results', restore
	Epost `b' `V' `Deff' `Deft' "`Meft'" "`depvar'" "`sub'"

	Effects, lceffects nolegend `options'
	if "`Vmsp'" != "" {
		matrix `x' = e(meft)
		return scalar meft = `x'[1,1]
		return scalar meff = `x'[1,1]^2
	}
	foreach mat of local matlist {
		matrix `x' = e(`mat')
		return scalar `mat' = `x'[1,1]
	}
	matrix `x' = e(V)
	return scalar se = sqrt(`x'[1,1])
	matrix `x' = e(b)
	return scalar estimate = `x'[1,1]
	return scalar df = `df'
end

program Epost, eclass
	args b V Deff Deft Meft depvar sub
	if "`depvar'" != "" {
		local dopt depname(`depvar')
	}
	local subpop `"`sub'"'
	ereturn post `b' `V', `dopt'
	ereturn matrix deff`sub' `Deff'
	ereturn matrix deft`sub' `Deft'
	ereturn local depvar	`depvar'
	ereturn local subpop	`"`subpop'"'
	ereturn local over	`"`over'"'
	ereturn local cmd	`"`cmd'"'
	if "`Meft'" != "" {
		ereturn matrix meft `Meft' 
	}
end

program EffectsOpts, sclass
	syntax [, DEFF DEFT MEFF MEFT SRSsubpop ]
	if "`srssubpop'" != "" {
		is_svysum `e(cmd)'
		if r(is_svysum) {
			local emptyover = "`e(over)'" == ""
		}
		else	local emptyover 1
		if `"`e(subpop)'"' == "" & `emptyover' {
			di as err ///
"option srssubpop requires subpopulation estimation results"
			exit 198
		}
		if "`deff'`deft'" == "" {
			local deff deff
			local deft deft
		}
		local sub sub
	}
	local diopts `deff' `deft' `meff' `meft'
	local ndiopts : word count `diopts'
	if `ndiopts' == 0 {
		if `"`e(deff)'"' == "matrix" {
			local deff deff
			local deft deft
		}
		if `"`e(meft)'"' == "matrix" {
			local meff meff
			local meft meft
		}
	}
	sreturn local deff `deff'
	sreturn local deft `deft'
	sreturn local meff `meff'
	sreturn local meft `meft'
	sreturn local sub  `sub'
end

program GetCVC, eclass
	gettoken V 0 : 0
	tempname w

	local dim = colsof(`V')
	matrix `w' = 0*`V'[1,1..`dim']

	ereturn post `w' `V'

	qui _test `0' = 1
	scalar `w' = r(chi2)

	matrix `V' = (0)
	if !missing(1/`w') {
		matrix `V'[1,1] = 1/`w'
	}
end

program DisplayTable
	syntax [,			///
		C1(namelist)		///
		C2(namelist)		///
		C3(namelist)		///
		C4(namelist)		///
		noLegend		///
		Verbose			///
		SIZE			///
	]

	// check for -_svy_summarize- results
	is_svysum `e(cmd)'
	local is_sum = r(is_svysum)

	// check for total number of equations
	local k_eq 0
	Chk4PosInt k_eq
	if `k_eq' == 0 {
		local k_eq : coleq e(b), quote
		local k_eq : list clean k_eq
		local k_eq : word count `k_eq'
	}
	// check for auxiliary parameters
	local k_aux 0
	Chk4PosInt k_aux
	// check for extra equations
	local k_extra 0
	Chk4PosInt k_extra

	Header, `legend' `verbose' is_sum(`is_sum')

	local fmt %8.0g
	local tfmt2 %11s %10s %10s %10s
	if "`c1'" == "" {
		local width2  0  0  0  0
		local pad2    0  0  0  0
	}
	else if "`c2'" == "" {
		local width2 11  0  0  0
		local pad2    3  0  0  0
	}
	else if "`c3'" == "" {
		local width2 11 10  0  0
		local pad2    3  2  0  0
	}
	else if "`c4'" == "" {
		local width2 11 10 10  0
		local pad2    3  2  2  0
	}
	else {
		local width2 11 10 10 10
		local pad2    3  2  2  2
	}
	if "`size'" != "" {
		local fmt %14.0g
		local tfmt2 %17s %16s %16s %16s
		if "`c1'" == "" {
			local width2  0  0  0  0
			local pad2    0  0  0  0
		}
		else if "`c2'" == "" {
			local width2 17  0  0  0
			local pad2    3  0  0  0
		}
		else {
			local width2 17 16  0  0
			local pad2    3  2  0  0
		}
	}

	// create _tab object
	tempname Tab
	.`Tab' = ._tab.new, col(7) lmargin(0) ignore(.b)
	// identify element properties for a full table
	// column        1      2     3      4      5      6     7
	local width1    13|    12    11
	local tfmt1      .      .  %11s
	local nfmt1      .  %9.0g  %9.0g
	local nfmt2                      `fmt'  `fmt'  `fmt' `fmt'
	local pad1       .      2     1
	local titles0                     `"""     ""     ""    """'
	local elements0                   `"""     ""     ""    """'
	// NOTE: there 4 columns available for display, there is no room to
	// add more columns given a fixed 80 (actually 78) char column table

	// first 3 column elements are already known
	local col1 12
	local widths "`width1'"
	local tfmts
	local nfmts
	local pads

	tempname error
	local coleq : coleq e(b), quote
	local coleq : list clean coleq
	local colnm : colname e(b)
	local dim : word count `colnm'
	if "`e(error)'" == "matrix" {
		matrix `error' = e(error)
	}
	else	matrix `error' = J(1,`dim',0)
	forval j = 1/4 {
		if "`c`j''" != "" {
			local C`j' : word 1 of `c`j''
			gettoken tok width2 : width2
			local widths `widths' `tok'
			gettoken tok tfmt2 : tfmt2
			local tfmts `tfmts' `tok'
			gettoken tok nfmt2 : nfmt2
			local nfmts `nfmts' `tok'
			gettoken tok pad2 : pad2
			local pads `pads' `tok'
			gettoken tok titles0 : titles0
			local titles `"`titles' "`:word 2 of `c`j'''""'
			gettoken tok elements0 : elements0
			local row `macval(row)' `C`j''[1,\`i']
		}
	}

	// automatically determine neq
	local neq = `k_eq'-`k_aux'-`k_extra'
	if `neq' < 0 {
		if `k_aux' & `k_extra' {
			di as err ///
"estimation command error: e(k_eq) is less than e(k_aux) + e(k_extra)"
			exit 322
		}
		local name = cond(`k_aux',"k_aux","k_extra")
		di as err ///
"estimation command error: e(k_eq) is less than e(`name')"
		exit 322
	}
	if !`is_sum' & `neq' == 1 & `k_extra' == 0 {
		local first first
	}
	local neq = `neq' + `k_aux'
	local neq1 = `k_eq'-`k_aux'-`k_extra'

	// set the table parameters
	.`Tab'.width    `widths' `width2'
	.`Tab'.titlefmt `tfmt1'  `tfmts'  `tfmt2'
	.`Tab'.numfmt   `nfmt1'  `nfmts'  `nfmt2'
	.`Tab'.pad      `pad1'   `pads'   `pad2'
	//               1     2 3 4 5 6 7
	.`Tab'.strfmt    . %-64s . . . . .

	GetColTitles `neq1' : depvar coef

	// start drawing the table
	.`Tab'.sep, top
	if `"`e(vcetype)'"' != "" {
		local vcetype `"`e(vcetype)'"'
		local vcewd : length local vcetype
		if `vcewd' <= 12 {
			local vcewd = `vcewd' + ceil((12-`vcewd')/2)
		}
		if "`e(vcetype)'" == "Bootstrap" {
			local obc "Observed"
		}
		// column        1       2                3   4  5  6  7
		.`Tab'.titlefmt  .       .        %`vcewd's   .  .  .  .
		.`Tab'.titles   "" "`obc'" `" `e(vcetype)'"' "" "" "" ""
		.`Tab'.titlefmt  .       .             %11s   .  .  .  .
	}
	.`Tab'.titles   "`depvar'" "`coef'" "Std. Err." `titles' `titles0'
	if "`:word 1 of `coleq''" == "_" {
		local coleq
		local eq
		.`Tab'.sep
	}
	local error1 " (no observations)"
	local error2 " (stratum with 1 PSU detected)"
	local error3 " (sum of weights equals zero)"
	local error4 " (denominator estimate equals zero)"
	local ieq 0
	local parameter 0
	forval i = 1/`dim' {
		local curreq : word `i' of `coleq'
		local name : word `i' of `colnm'
		if "`curreq'" != "" {
			local beq "[`curreq']"
		}
		local curreq = abbrev("`curreq'",`col1')
		local currname = abbrev("`name'",`col1')
		if "`eq'" != "`curreq'" {
			local ++ieq
			if ! `parameter' {
				.`Tab'.sep
			}
			if "`first'" == "" & `ieq' <= `neq1' {
				di as res %-`col1's "`curreq'" as txt " {c |}"
			}
			if `ieq' > `neq1' {
				local parameter 1
			}
		}
		if `parameter' {
			local currname = abbrev("/`curreq'",`col1')
		}
		local ei = `error'[1,`i']
		if inlist(`ei',1,2,3,4) {
			.`Tab'.row "`currname'" " `error`ei''" . . . . .
		}
		else {
			.`Tab'.row "`currname'"			///
					`beq'_b[`name']		///
					`beq'_se[`name']	///
					`row' `elements0'
		}
		local eq `curreq'
	}
	.`Tab'.sep, bottom
end

program Header
	syntax [, noHeader noLegend Verbose is_sum(integer 0) ]

	local blank
	if "`legend'" == "" {
		if inlist("`e(vce)'","jackknife","brr") ///
		 & "`e(cmd)'" == "`e(vce)'"{
			_prefix_legend `e(vce)', `verbose'
			if "`e(vce)'" == "jackknife" ///
				& "`e(jkrweight)'" == "" ///
				& "`e(wtype)'" != "iweight" {
				_jk_nlegend `s(col1)' ///
					`"`e(nfunction)'"'
				local blank blank
			}
		}
		if `is_sum' {
			_svy_summarize_legend `blank'
			local blank `s(blank)'
		}
	}
	if "`blank'" == "" {
		di
	}
end

program Chk4PosInt
	args ename
	if `"`e(`ename')'"' != "" {
		capture confirm integer number `e(`ename')'
		if !c(rc) {
			if `e(`ename')' > 0 {
				c_local `ename' `e(`ename')'
			}
		}
	}
end

// setup macros for column headings
program GetColTitles
	args neq COLON depvar coef
	is_svysum `e(cmd)'
	local is_sum = r(is_svysum)
	if `is_sum' {
		if "`e(over)'" != "" {
			c_local `depvar' "Over"
		}
		c_local `coef' = proper("`e(cmd)'")
	}
	else {
		local dv `"`e(depvar)'"'
		if `:word count `dv'' == 1 ///
		 & (`neq' <= 1 | !`:list dv in coleq') {
			c_local `depvar' = abbrev("`dv'",12)
		}
		c_local `coef' "Coef."
	}
	
end

program CheckPost
	args sub
	if `"`e(poststrata)'"' != "" {
		di as err ///
		"estat `sub' is not allowed with poststratification"
		exit 322
	}
	is_svysum `e(cmd)'
	if r(is_svysum) & `"`e(stdize)'"' != "" {
		di as err ///
		"estat `sub' is not allowed with direct standardization"
		exit 322
	}
end

exit

⌨️ 快捷键说明

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