Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: help creating a table of means and diffs with svy data


From   Roy Wada <roywada@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: help creating a table of means and diffs with svy data
Date   Tue, 26 Jan 2010 16:02:52 -0800

> I want to have a table comparing the means for some variables for two
> subpopulations in my survey data, including a test on the diff btw the
> means.

This will actually do what you asked for.

You will need the latest verion of outreg2 and sxpose from ssc, in
addition to the stuff below. There must be at least two variables
specified.

use http://www.stata-press.com/data/r11/nhanes2f
meanout height age weight smsa using myfile.txt, over(sex) /*
	*/ category(Male Female) svy excel


*! meanout 0.9 26jan2010 by roywada@hotmail.com
prog def meanout
syntax varlist(min=2) using/, over(str asis) category(str asis) /*
	*/ [svy *]
version 8
qui {
tokenize `category'
if "`svy'"=="svy" {
	local svy "svy:"
}

noi `svy' mean `varlist', over(`over')
qui outreg2 using `using', noaster nonotes noobs replace side ct("coef")
preserve
insheet using `using', noname clear
drop in `=_N'
sxpose, clear
drop _var1 _var3

tempvar order
gen `order'=_n
set obs `=_N+2'
replace `order'=0.5 in `=_N-1'
replace `order'=1.5 in `=_N'
sort `order'
set obs `=_N+1'
drop `order'
replace _var4="(1)" in 1
replace _var5="(1)" in 1
replace _var2="" if _var2==_var2[_n-1]
outsheet using `using', noname replace noquote
restore

`svy' mean `varlist', over(`over')
tempname coef se
foreach var in `varlist' {
	lincom [`var']`1' - [`var']`2'
	mat `coef' = nullmat(`coef') \ `r(estimate)'
	mat `se' = nullmat(`se') \ `r(se)'
}

mkest, replace bmat(`coef') vmat(`se') depvar(Difference) /*
	*/ cmd(correlation) noobs noesample matname(`varlist')

} /* qui */

outreg2 using `using', nonotes noobs `options'
end

	

*! mkest 1.0.1 14jan2010 by roywada@hotmail.com
*! Convert variables to estimates matrix
program def mkest, eclass
version 8

syntax [if] [in], replace [b(str asis) v(str asis) bmat(str asis) /*
	*/ vmat(str asis) depvar(str asis) cmd(str asis) obs(numlist) /*
	*/ NOESAMPLE NOOBS matname(str asis)]

tempname ebmat eVmat
marksample touse

if "`depvar'"=="" {
	local depvar `b'
}
if "`cmd'"=="" {
	local cmd `depvar'
}

if "`bmat'"=="" {
	* get from variables
	mkmat `b' if `touse', matrix(`ebmat') nomiss
	mat `ebmat'=`ebmat''
	
	mkmat `v' if `touse', matrix(`eVmat') nomiss
	mat `eVmat'=`eVmat''
	mat `eVmat'=(`eVmat''*`eVmat')
}
else {
	* get from matrices
	mat `ebmat'=`bmat''
	
	mat `eVmat'=`vmat''
	mat `eVmat'=(`eVmat''*`eVmat')
}

if "`obs'"=="" {
	local obs=colsof(`ebmat')
}
if "`noobs'"~="" {
	local obs .
}
if "`matname'"~="" {
	matname `ebmat' `matname', col(.)
	matname `eVmat' `matname', row(.)
	matname `eVmat' `matname', col(.)
}


tempvar sample
cap gen `sample'=1 if `b'~=. & `touse'
cap gen `sample'=1 if `b'~="" & `touse'
cap qui replace `sample'=0 if `sample'==.

	if "`replace'"=="replace" {
		eret clear
		if "`noesample'"=="" {
			eret post `ebmat' `eVmat', esample(`sample')
		}
		else {
			eret post `ebmat' `eVmat'
		}
		eret local depvar `"`depvar'"'
		eret local cmd "`cmd'"
		eret scalar N = `obs'
	}

end
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index