Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: mean and SD by categories


From   Federico Belotti <[email protected]>
To   [email protected]
Subject   Re: st: mean and SD by categories
Date   Tue, 16 Jul 2013 15:08:31 +0200

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
> [email protected]
> 
> 
> On 16 July 2013 13:56, Federico Belotti <[email protected]> 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
>>> [email protected]
>>> 
>>> 
>>> On 16 July 2013 12:43, Dherani, Mukesh <[email protected]> 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: [email protected]
>> 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: [email protected]
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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index