Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Dherani, Mukesh" <M.K.Dherani@liverpool.ac.uk> |
To | "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |
Subject | RE: st: mean and SD by categories |
Date | Tue, 16 Jul 2013 15:01:38 +0000 |
Thank you Nick and Federico. This code runs fine but ends up on one error message: . estimation result m(all) not found While I digest the code can you please let me know why? BW,m -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Federico Belotti Sent: 16 July 2013 14:09 To: statalist@hsphsun2.harvard.edu Subject: Re: st: mean and SD by categories Ops. Sorry... I wrote the code too quickly qui { local lab cap mat drop all mean sd up lo forval a=1/2 { forval b=1/7 { forval c=1/5 { sum bmi if sex==`a' & agegp==`b' & ses==`c' mat mean = nullmat(mean) \ r(mean) mat sd = nullmat(sd) \ r(sd) mat up = nullmat(up) \ r(mean)+(1.96*r(sd)/sqrt(r(N))) mat lo = nullmat(lo) \ r(mean)-(1.96*r(sd)/sqrt(r(N))) local lab `"`lab' "sex=`a' agegp=`b' ses=`c'""' } } } mat all = (mean,sd,lo,up) mat rown all = `lab' mat coln all = mean sd lb ub noi estout m(all), varw(22) mlab(,none) } On Jul 16, 2013, at 3:02 PM, Nick Cox wrote: > You need to take square roots of the sample sizes in calculating > standard errors. > Nick > njcoxstata@gmail.com > > > On 16 July 2013 13:56, Federico Belotti <f.belotti@gmail.com> wrote: >> This could be a strategy to get a table using Ben Jann -estout- >> command >> >> qui { >> forval a=1/2 { >> forval b=1/7 { >> forval c=1/5 { >> sum bmi if sex==`a' & agegp==`b' & ses==`c' >> mat mean = nullmat(mean) \ r(mean) >> mat sd = nullmat(sd) \ r(sd) >> mat up = nullmat(up) \ r(mean)+(1.96*r(sd)/r(N)) >> mat lo = nullmat(lo) \ r(mean)-(1.96*r(sd)/r(N)) >> local lab `"`lab' "sex=`a' agegp=`b' ses=`c'""' >> } >> } >> } >> mat all = (mean,sd,lo,up) >> mat rown all = `lab' >> mat coln all = mean sd lb ub >> noi estout m(all), varw(22) mlab(,none) } >> >> Federico >> >> On Jul 16, 2013, at 2:23 PM, Nick Cox wrote: >> >>> There are various problems with your approach. >>> >>> 1. The immediate problem is that you cycle over combinations of >>> categories creating local macros with names like mean111, sd111, and >>> so forth. >>> >>> But a command like >>> >>> display mean111 >>> >>> is understood by Stata to mean that you want to see a value of a >>> variable -mean111- or a scalar -mean111-, but you have no such >>> variable or scalar, hence the message you received. >>> >>> To see the value of a local macro, you would need to do something >>> like >>> >>> display `mean111' >>> >>> 2. Adding +/- 1.96 SD to the mean is an attempt to get 95% >>> confidence intervals. But there are at least two problems here. 1.96 >>> is an optimistic multiplier and a more accurate multiplier would be >>> based on the t-distribution. However, that's secondary compared with >>> using SDs when you should be using standard errors. >>> >>> 3. A strategic difficulty here is that creating lots of local macros >>> first is not a good way to create graphs. In fact if you are a >>> novice in programming trying to program graphs from scratch is not advisable. >>> In your case 70 cross-combinations seems likely to produce only a >>> very complicated graph. Much depends on whether each >>> cross-combination is well enough represented to show stable patterns. >>> >>> -stripplot- from SSC may be of help. It has a -ci- option that means >>> that confidence intervals are shown for you. >>> >>> Nick >>> njcoxstata@gmail.com >>> >>> >>> On 16 July 2013 12:43, Dherani, Mukesh <M.K.Dherani@liverpool.ac.uk> wrote: >>>> Dear all, >>>> I am very novice in programming. >>>> I have a dataset with BMI by three categorical variables age (7 groups), sex (2 groups) and SES (5 groups). I was to generate a line graph showing mean BMI with 95%CI by age , sex and SES. Below is my code to create local that I want to use in twoway line graph: >>>> >>>> forval a=1/2 { >>>> forval b=1/7 { >>>> forval c=1/5 { >>>> sum bmi if sex==`a' & agegp==`b' & ses==`c' >>>> local mean`a'`b'`c'=r(mean) >>>> local sd`a'`b'`c'=r(sd) >>>> local up`a'`b'`c'=r(mean)+(1.96*r(sd)) local >>>> lo`a'`b'`c'=r(mean)-(1.96*r(sd)) di mean`a'`b'`c' sd`a'`b'`c' >>>> up`a'`b'`c' lo`a'`b'`c' >>>> } >>>> } >>>> } >>>> >>>> >>>> At the end of this command stata says mean111 was not found! >>>> Secondly, I want graph to show me mean bmi (y axis) and age (x axis) for each ses for each sex separately. Should I carry this out in the loop or outside the loop? >>>> >>>> Any help is highly appreciated. >>>> >>>> BW,m >>>> >>>> >>>> * >>>> * For searches and help try: >>>> * http://www.stata.com/help.cgi?search >>>> * http://www.stata.com/support/faqs/resources/statalist-faq/ >>>> * http://www.ats.ucla.edu/stat/stata/ >>> >>> * >>> * For searches and help try: >>> * http://www.stata.com/help.cgi?search >>> * http://www.stata.com/support/faqs/resources/statalist-faq/ >>> * http://www.ats.ucla.edu/stat/stata/ >> >> -- >> Federico Belotti, PhD >> Research Fellow >> Centre for Economics and International Studies University of Rome Tor >> Vergata >> tel/fax: +39 06 7259 5627 >> e-mail: federico.belotti@uniroma2.it >> web: http://www.econometrics.it >> >> >> * >> * For searches and help try: >> * http://www.stata.com/help.cgi?search >> * http://www.stata.com/support/faqs/resources/statalist-faq/ >> * http://www.ats.ucla.edu/stat/stata/ > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/faqs/resources/statalist-faq/ > * http://www.ats.ucla.edu/stat/stata/ -- Federico Belotti, PhD Research Fellow Centre for Economics and International Studies University of Rome Tor Vergata tel/fax: +39 06 7259 5627 e-mail: federico.belotti@uniroma2.it web: http://www.econometrics.it * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/