[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Claire Kamp Dush" <ckamp-dush@ehe.osu.edu> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: svy: mean and descriptive tables |

Date |
Thu, 19 Jul 2007 14:03:56 -0400 |

Dear Statalist, Per Nick's message (st: "Thanks in advance" and closure of threads), let me wrap up my thread and say that Jeff Pitblado and Michael Blasnik along with Emma Slaymaker were so kind to reply to me and my original problem is well-solved with these programs. As a new programmer, it is incredibly helpful to get this kind of help and it pushes me much further on the path to programming these kinds of complicated programs myself, so I thank you for your goodwill towards me and patience as I try to learn. Claire Kamp Dush -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Jeff Pitblado, StataCorp LP Sent: Thursday, July 19, 2007 11:32 AM To: statalist@hsphsun2.harvard.edu Subject: Re: st: svy: mean and descriptive tables Claire Kamp Dush <ckamp-dush@ehe.osu.edu> wants to produce a table of means and standard deviations from variables in a survey dataset: > I am running several svy: mean commands for a descriptive statistic > table I am creating. Here is the code I am running: > > After svy setting the variables > > foreach var of varlist var1 var2 var3 { > svy: mean `var' > di sqrt(e(N) * el(e(V_srs), 1, 1)) > } > > I am using Stata 9. This code is working fine, and the first line > calculates the weighted mean and the second the standard deviation. My > question is: > Is there a way for me to write a code to create a table for my means and > standard deviations automatically? For instance, I would like a table > as follows: > > Var 1 mean1 sd1 > Var 2 mean2 sd2 > Var 3 mean3 sd3 > > > Any suggestions on how I could do this? Here is my first attempt at a > program: > > prog svymean > local mean = svy: mean `1' > local sd = di sqrt(e(N) * el(e(V_srs), 1, 1)) > display e(varlist) `mean' `sd' > end > > Then I thought I would run this for each of my variables and copy and > paste the results into a text file and then convert into an excel or > word file. But, this didn't work because it wouldn't calculate the svy: > mean of `1'. > > I searched Statalist and I did not see a ready solution, but I could > have missed something. > > In advance, I never know whether to e-mail thank yous and fill up > everyone's in boxes that much more, so in advance, a whole-hearted thank > you to anyone who replies. I really appreciate it. In Stata 10 there is a new -estat sd- command that does this, see the section of this email titled 'Stata 10 Users' for an example. Here is a rewrite of Claire's program that works. prog mysvymean quietly svy: mean `1' local sd = sqrt(e(N) * el(e(V_srs), 1, 1)) display e(varlist) " " _b[`1'] " " `sd' end ------------------------------------------------------------------------ ----- Stata 9 Users There are two ways I can think of building a table from the results of the -svy: mean- command. 1. Let -svy: mean- perform the estimation all at once, then pick the pieces I want from -e(b)- (via _b[...]) and -e(V_srs)-. This will restrict the estimation sample to the set of observations that contain no missing values in the variables of interest. 2. Collect the information from -svy: mean- by looping over the variables. This will allow the full set of observations to be used for computing the mean and sd for each varaible. I wrote a program for each case and named them -mean_sd_table1- and -mean_sd_table2-, corresponding to the above two descriptions. ***** BEGIN: program mean_sd_table1 syntax varlist tempname v quietly svy: mean `varlist' matrix `v' = e(N)*vecdiag(e(V_srs)) local k : list sizeof varlist display as txt %12s "Variable" /// as txt %10s "Mean" /// as txt %12s "Std. Dev." forval i = 1/`k' { local var : word `i' of `varlist' display as txt %12s "`var'" /// " " as res %9.0g _b[`var'] /// " " as res %9.0g sqrt(`v'[1,`i']) } end program mean_sd_table2 syntax varlist tempname sd local k : list sizeof varlist display as txt %12s "Variable" /// as txt %10s "N" /// as txt %12s "Mean" /// as txt %12s "Std. Dev." forval i = 1/`k' { local var : word `i' of `varlist' quietly svy: mean `var' scalar `sd' = sqrt(e(N)*el(e(V_srs),1,1)) display as txt %12s "`var'" /// " " as res %9.0g e(N) /// " " as res %9.0g _b[`var'] /// " " as res %9.0g `sd' } end ***** END: Here are some example results ***** BEGIN: . webuse nhanes2 . svyset psu [pw=finalwgt], strata(strata) pweight: finalwgt VCE: linearized Single unit: missing Strata 1: strata SU 1: psu FPC 1: <zero> . mean_sd_table1 smsa height weight bpsystol bpdiast tcresult tgresult Variable Mean Std. Dev. smsa 2.50289 1.235087 height 168.6915 9.613919 weight 72.05973 15.50079 bpsystol 126.1283 21.16074 bpdiast 80.38098 12.92431 tcresult 211.3975 47.08961 tgresult 138.576 95.90575 . mean_sd_table2 smsa height weight bpsystol bpdiast tcresult tgresult Variable N Mean Std. Dev. smsa 10351 2.496329 1.239231 height 10351 168.4599 9.699111 weight 10351 71.90064 15.43281 bpsystol 10351 126.9458 21.40073 bpdiast 10351 81.01726 12.79357 tcresult 10351 213.0977 48.45305 tgresult 5050 138.576 95.90575 ***** END: Note that I include a column for the sample size in -mean_sd_table2- to show that the estimation sample can differ between variables. ------------------------------------------------------------------------ ----- Stata 10 Users There is the new -estat sd- command allowed after -svy: mean- that produces a table similar to the one produced by -mean_sd_table2-. So a mean/std. dev. table could be produced using something like the following example . webuse nhanes2 . svyset psu [pw=finalwgt], strata(strata) . svy: mean smsa height weight bpsystol bpdiast tcresult tgresult . estat sd The table resulting from -estat sd- is ------------------------------------- | Mean Std. Dev. -------------+----------------------- smsa | 2.50289 1.235087 height | 168.6915 9.613919 weight | 72.05973 15.50079 bpsystol | 126.1283 21.16074 bpdiast | 80.38098 12.92431 tcresult | 211.3975 47.08961 tgresult | 138.576 95.90575 ------------------------------------- Note that -estat sd- also handles results that employed the -subpop()- option of -svy- and the -over()- option of -mean-. --Jeff jpitblado@stata.com * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: svy: mean and descriptive tables***From:*"Austin Nichols" <austinnichols@gmail.com>

**References**:**Re: st: svy: mean and descriptive tables***From:*jpitblado@stata.com (Jeff Pitblado, StataCorp LP)

- Prev by Date:
**st: "Thanks in advance" and closure of threads** - Next by Date:
**st: Urgency** - Previous by thread:
**Re: st: svy: mean and descriptive tables** - Next by thread:
**Re: st: svy: mean and descriptive tables** - Index(es):

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