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

📄 svytab_7.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
📖 第 1 页 / 共 4 页
字号:
				else {
					local key`keyi' /*
					*/ "standard errors of `item'"
				}
				local keyi = `keyi' + 1
			}
		}
		if "`ci'"!="" {
			tempname t
			tempvar lb ub
			scalar `t' = invt(e(N_psu)-e(N_strata),`level'/100)

			if "`count'"=="" {
				local x "`xp'`xrow'`xcol'"
				if `porp' == 100 {
					local x "(`x'/100)"
				}

				gen double `lb' = `porp'/(1 + /*
				*/ exp(-(log(`x'/(1-`x')) /*
				*/ - `t'*`xse'/(`porp'*`x'*(1-`x')))))

				gen double `ub' = `porp'/(1 + /*
				*/ exp(-(log(`x'/(1-`x')) /*
				*/ + `t'*`xse'/(`porp'*`x'*(1-`x')))))
			}
			else {
				gen double `lb' = `xc' - `t'*`xse'
				gen double `ub' = `xc' + `t'*`xse'
			}

			local xci "`lb' `ub'"

			if "`se'"=="" { local xse /* erase tempvar */ }

			if "`vertica'"=="" {
				local key`keyi' /*
*/ `"[`=strsubdp("`level'")'% confidence intervals for `item']"'
			}
			else {
				local key`keyi' /*
*/ `"lower `=strsubdp("`level'")'% confidence bounds for `item'"'
				local keyi = `keyi' + 1
				local key`keyi' /*
*/ `"upper `=strsubdp("`level'")'% confidence bounds for `item'"'
			}
			local keyi = `keyi' + 1
		}
		if "`vertica'"=="" & "`se'`ci'"!="" {
			if "`se'"=="" {
				local nose "*"
			}
			if "`ci'"=="" {
				local noci "*"
			}

			`nose' tempvar xxse
			`nose' gen str1 `xxse' = "" in 1
			`noci' tempvar xci
			`noci' gen str1 `xci' = "" in 1

			local i 1
			while `i' <= `N' {
				`nose' local ses : di `format' `xse'[`i']
				`nose' replace `xxse' = "("+trim("`ses'")+")" /*
				*/ in `i' if `xse'<.

				`noci' local slb : di `format' `lb'[`i']
				`noci' local sub : di `format' `ub'[`i']
				`noci' replace `xci' = "["+trim("`slb'") /*
				*/ +","+trim("`sub'")+"]" in `i' /*
				*/ if `lb'<. & `ub'<.
				local i = `i' + 1
			}

			`nose' local xse "`xxse'"
		}
		if "`deff'"!="" {
			tempvar xdeff
			DeffVar `xdeff' f `Nr1' `Nr2' `Nc1' `Nc2' /*
			*/ "`margina'" "`row'" "`column'"
			local key`keyi' "deff for variances of `item'"
			local keyi = `keyi' + 1
		}
		if "`deft'"!="" {
			tempvar xdeft
			DeffVar `xdeft' t `Nr1' `Nr2' `Nc1' `Nc2' /*
			*/ "`margina'" "`row'" "`column'"
			local key`keyi' "deft for variances of `item'"
			local keyi = `keyi' + 1
		}
		if "`obs'"!="" {
			tempname Obs
			tempvar xobs
			if "`e(ObsSub)'" ~= "" {
				mat `Obs' = e(ObsSub)
			}
			else mat `Obs' = e(Obs)
			gen long `xobs' = `Obs'[`iii',`jjj'] in 1/`ncat'
			if "`margina'"=="" {
				tempname J
				mat `J' = J(1,e(c),1)
				mat `J' = `J'*`Obs''
				replace `xobs' = `J'[1,_n-`Nr1'+1] /*
				*/ in `Nr1'/`Nr2'
				mat `J' = J(1,e(r),1)
				mat `J' = `J'*`Obs'
				replace `xobs' = `J'[1,_n-`Nc1'+1] /*
				*/ in `Nc1'/`Nc2'
				if "`e(N_sub)'" ~= "" {
					replace `xobs' = e(N_sub) in l
				}
				else replace `xobs' = e(N) in l
			}
			local key`keyi' "number of observations"
			local keyi = `keyi' + 1
		}
	}

	local vars "`xc' `xp' `xrow' `xcol' `xse' `xci' `xdeff' `xdeft' `xobs'"

	tabdisp `e(rowvar)' `e(colvar)', cell(`vars') `total' /*
	*/ format(`format') /*
	*/ `cellwid' /*
	*/ `csepwid' /*
	*/ `stubwid' /*
	*/

	if "`e(tab)'"!="" {
		di in gr "  Tabulated variable:  `e(tab)'" _n
	}

	di in gr "  Key:  " _c
	local col 1
	local i 1
	while `i' < `keyi' {
		di _col(`col') in ye "`key`i''"
		local col 9
		local i = `i' + 1
	}
end

program define DeffVar
	args var ft Nr1 Nr2 Nc1 Nc2 margina row column

	local ncat = e(r)*e(c)
	tempname def
	mat `def' = e(Def`ft')

	quietly {
		gen double `var' = cond(`def'[1,_n]!=0,`def'[1,_n],.) /*
		*/ in 1/`ncat'

		if "`margina'"=="" {
			if "`row'"=="" {
				mat `def' = e(Def`ft'_row)
				replace `var' = cond(`def'[1,_n-`Nr1'+1]!=0, /*
				*/ `def'[1,_n-`Nr1'+1],.) in `Nr1'/`Nr2'
			}
			if "`column'"=="" {
				mat `def' = e(Def`ft'_col)
				replace `var' = cond(`def'[1,_n-`Nc1'+1]!=0, /*
				*/ `def'[1,_n-`Nc1'+1],.) in `Nc1'/`Nc2'
			}
		}
	}
end

program define FixMiss
	args lblname var missval N N1 N2
	local missval = round((`missval')+1,1)

	local lab : value label `var'
	if "`lab'"=="" {
		local lab `lblname'
		label define `lab' `missval' "."
		label value `var' `lab'
	}
	else	label define `lab' `missval' "." , add

	qui replace `var' = `missval' if `var'>=.&(_n<=`N'|(`N1'<=_n&_n<=`N2'))
end

program define MakeLab
	args labname var stub

	tempname imat
	local dim = colsof(matrix(`stub'))
	local i 1
	while `i' <= `dim' {
		mat `imat' = `stub'[1,`i'..`i']
		local lab : colnames `imat'
		if "`lab'"!="hoborxqc" {
			local x = `stub'[1,`i']
			label define `labname' `x' "`lab'", `add'
			local add "add"
		}
		local i = `i' + 1
	}
	if "`add'"!="" {
		label value `var' `labname'
	}
end

program define DispStat
	args title stat null

	local col0  5 /* left margin */
	local col1 19 /* statistic   */
	local col2 35 /* equal sign  */
	local col3 51 /* p-value     */

	local dfnom = (e(r)-1)*(e(c)-1)
	local df1 "e(df1_`stat')"
	local df2 "e(df2_`stat')"

	if `df1' != int(`df1') {
		local df1 "%4.2f `df1'"
	}
	if `df2' != int(`df2') {
		local df2 "%4.2f `df2'"
	}

	di _n in gr "  `title'"

	di in gr _col(`col0') "Uncorrected" _col(`col1') "chi2(" in ye /*
	*/ `dfnom' in gr ")" _col(`col2') "= " in ye %9.4f e(cun_`stat')

	if "`null'"!="" {
		local df1n "e(df1_`null')"
		local df2n "e(df2_`null')"

		if `df1n' != int(`df1n') {
			local df1n "%4.2f `df1n'"
		}
		if `df2n' != int(`df2n') {
			local df2n "%4.2f `df2n'"
		}

		di in gr _col(`col0') "D-B (null)" _col(`col1') "F(" in ye /*
		*/ `df1n' in gr ", " in ye `df2n' in gr ")" _col(`col2') "= " /*
		*/ in ye %9.4f e(F_`null') _col(`col3') in gr "P = " in ye /*
		*/ %6.4f e(p_`null')
	}

	di in gr _col(`col0') "Design-based" _col(`col1') "F(" in ye `df1' /*
	*/ in gr ", " in ye `df2' in gr ")" _col(`col2') "= " in ye %9.4f /*
	*/ e(F_`stat') _col(`col3') in gr "P = " in ye %6.4f e(p_`stat')
end

program define DispWald
	args title stat unadj

	local col0  5 /* left margin */
	local col1 19 /* statistic   */
	local col2 35 /* equal sign  */
	local col3 51 /* p-value     */

	di _n in gr "  `title'"

	local df1 = (e(r) - 1)*(e(c) - 1)

	di in gr _col(`col0') "Unadjusted" _col(`col1') "chi2(" in ye /*
	*/ `df1' in gr ")" _col(`col2') "= " in ye %9.4f /*
	*/ e(cun_`stat')

	if "`unadj'"!="" {
		di in gr _col(`col0') "Unadjusted" _col(`col1') "F(" in ye /*
		*/ `df1' in gr ", " in ye e(df_r) /*
		*/ in gr ")" _col(`col2') "= " in ye %9.4f e(Fun_`stat') /*
		*/ _col(`col3') in gr "P = " in ye %6.4f e(pun_`stat')
	}

	di in gr _col(`col0') "Adjusted" _col(`col1') "F(" in ye /*
	*/ `df1' in gr ", " in ye e(df_r)-`df1'+1 in gr ")" /*
	*/ _col(`col2') "= " in ye %9.4f e(F_`stat') _col(`col3') /*
	*/ in gr "P = " in ye %6.4f e(p_`stat')
end

exit

/*-------------------------------- old code  --------------------------------*/

program define Check
	args tr tra b b0 V row col X1 X2 D Da

	local nrow = colsof(`row')
	local ncol = colsof(`col')

	tempname A trc
	di in gr "tr(Ho)         = " in ye %12.8f `tr'
	di in gr "tr(Ha)         = " in ye %12.8f `tra'

	TraceD $S_VYnobs `nrow' `ncol' `b' `b0' `V' `trc'
	di in gr "tr(comparison) = " in ye %12.8f `trc' /*
	*/ %12.1e abs((`trc'-`tr')/`tr')

	Fuller `b0' `V' `row' `col' `A'
	scalar `trc' = trace(`A')
	di in gr "tr(Fuller)     = " in ye %12.8f `trc' /*
	*/ %12.1e abs((`trc'-`tr')/`tr')

	Nested `b0' `V' `X1' `X2' `A'
	scalar `trc' = trace(`A')
	di in gr "tr(Nested b0)  = " in ye %12.8f `trc' /*
	*/ %12.1e abs((`trc'-`tr')/`tr') %12.1e mreldif(`A',`D')

	Nested `b' `V' `X1' `X2' `A'
	scalar `trc' = trace(`A')
	di in gr "tr(Nested b)   = " in ye %12.8f `trc' /*
	*/ %12.1e abs((`trc'-`tra')/`tra') %12.1e mreldif(`A',`Da')
end

program define Nested /* With b0 as input, this program yields D with the same
                         trace(D) and trace(D^2) as DeffMat with b0, but D's are
                         different.  With b as input, gives same traces as
                         DeffMat with b (except when there are zeros in table).
		      */
	args b0 V X1 X2 D

	tempname A P Xt2

/* Construct misspecified variance estimate `P' under Ho. */

	mat `P' = `b0''*`b0'
	mat `A' = diag(`b0')
	mat `P' = `A' - `P'

/* Project `X2' onto the orthogonal complement of `X1' space w.r.t. `P' norm. */

	ProjOper `X1' `P' `A'

	mat `Xt2' = `A'*`X2'

/* Compute generalized design effects matrix. */

	mat `A' = `P'*`Xt2'
	mat `A' = `Xt2''*`A'
	SymInv `A'
	mat `D' = `V'*`Xt2'
	mat `D' = `Xt2''*`D'
	mat `D' = `A'*`D'
	mat `D' = $S_VYnobs*`D'
end

program define Fuller /* This program yields D with the same trace(D) and
                         trace(D^2) as DeffMat with b0, but D's are different;
                         they are not even the same dimension.  When called
			 with b, gives something not related to other methods.
		      */
	args b0 V row col D

	tempname A B X

	local nrow = colsof(`row')
	local ncol = colsof(`col')

/* Compute `X' matrix. */

	mat `A' = I(`nrow')
	mat `B' = `col''
	mat `X' = `A' # `B'
	mat `A' = I(`ncol')
	mat `B' = `row''
	mat `A' = `B' # `A'
	mat `X' = `X' , `A'
	local dim = `nrow' + `ncol' - 1
	mat `X' = `X'[1...,1..`dim']

/* Compute projection matrix. */

	mat `A' = diag(`b0')
	mat `A' = syminv(`A')

	ProjOper `X' `A' `D'

	mat `D' = `A'*`D'
	mat `D' = `D'*`V'
	mat `D' = $S_VYnobs*`D'
end

program define ProjOper /* returns P = I - X (X'WX)^-1 X'W */
	args X W P
	tempname A
	mat `A' = `W'*`X'
	mat `A' = `X''*`A'
	SymInv `A'
	mat `A' = `A'*`X''
	mat `A' = `X'*`A'
	Sym `A'
	mat `A' = `A'*`W'
	local dim = colsof(`A')
	mat `P' = I(`dim')
	mat `P' = `P' - `A'
end

program define TraceD
	args n nrow ncol b b0 V tr

	tempname A B row col Vrow Vcol

/* Get marginal row and column sums. */

	mat `A' = I(`nrow')
	mat `B' = J(1,`ncol',1)
	mat `A' = `A' # `B'
	mat `row' = `b'*`A''
	mat `Vrow' = `V'*`A''
	mat `Vrow' = `A'*`Vrow'

	mat `A' = J(1,`nrow',1)
	mat `B' = I(`ncol')
	mat `A' = `A' # `B'
	mat `col' = `b'*`A''
	mat `Vcol' = `V'*`A''
	mat `Vcol' = `A'*`Vcol'

	scalar `tr' = 0
	local i 1
	while `i' <= `nrow'*`ncol' {
		scalar `tr' = `tr' + `V'[`i',`i']/`b0'[1,`i']
		local i = `i' + 1
	}
	local i 1
	while `i' <= `nrow' {
		scalar `tr' = `tr' - `Vrow'[`i',`i']/`row'[1,`i']
		local i = `i' + 1
	}
	local i 1
	while `i' <= `ncol' {
		scalar `tr' = `tr' - `Vcol'[`i',`i']/`col'[1,`i']
		local i = `i' + 1
	}

	scalar `tr' = `n'*`tr'
end

/*---------------------------- end of old code ------------------------------*/

(1) Non-Wald statistics:

    Pear = Pearson with observed misspecified variance
    Penl = Pearson with null misspecified variance
    LR   = LR      with observed
    LRnl = LR      with null

    all these yield

	cun_Pear   uncorrected (i.e., misspecified) chi2
	F_Pear     F-statistic: F = chi2/df1
	df1_Pear   df1 = tr^2/tr2
	df2_Pear   df2 = df1*(#psu - #strata)
	p_Pear     p-value for above F-statistic ~ F(df1,df2)

(2) Fuller variant:

    Full = Pearson with null misspecified variance using Fuller et al.'s
    formula.

	F_Full     F = chi2/df1 ~ F(df1,df2)
	df1_Full   df1 = <complex formula>
	df2_Full   df2 = #psu - #strata
	p_Full     p-value for above F-statistic ~ F(df1,df2)

(3) Wald statistics:

    Wald = "Pearson" Wald
    LLW  = log-linear Wald

    both of these produce (with W or T in place of W below):

	F_Wald     adjusted F = (dfv-df1+1)*chi2/(df1*dfv)
	df1_Wald   (# rows - 1)*(# columns - 1)
	df2_Wald   df2 = dfv - df1 + 1, where dfv = #psu - #strata
	p_Wald     p-value for adjusted F-statistic ~ F(df1,df2)

	cun_Wald   unadjusted chi2
	Fun_Wald   unadjusted F = chi2/df1
	pun_Wald   p-value for unadjusted F-statistic ~ F(df1,dfv)

-------------------------------------------------------------------------------
<end of file>

⌨️ 快捷键说明

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