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

From |
jpitblado@stata.com (Jeff Pitblado, StataCorp LP) |

To |
statalist@hsphsun2.harvard.edu |

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

Date |
Thu, 19 Jul 2007 10:32:03 -0500 |

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/

**Follow-Ups**:**RE: st: svy: mean and descriptive tables***From:*"Claire Kamp Dush" <ckamp-dush@ehe.osu.edu>

- Prev by Date:
**Re: st: Simultaneous equations estimation** - Next by Date:
**Re: st: pseudo R2s for Generalized Linear Models** - Previous by thread:
**st: Re: svy: mean and descriptive tables** - Next by thread:
**RE: st: svy: mean and descriptive tables** - Index(es):

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