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: loop: integrate generate syntax in existing loop


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: loop: integrate generate syntax in existing loop
Date   Thu, 4 Jul 2013 11:46:36 +0100

Lots of problems here. The immediate problem is that this is one loop
only, not multiple nested loops. So, this should work so far as Stata
is concerned.

foreach x of varlist a-d {
         ttest `x', by(group)
         gen lower_`x' = r(mu_1) - r(sd_1)*1.96
         gen upper_`x' = r(mu_1) + r(sd_1)*1.96
         gen lower2_`x' = r(mu_2) - r(sd_2)*1.96
         gen upper2_`x' = r(mu_2) + r(sd_2)*1.96
}

That said, what are you trying to do here?  Mean +/- 1.96 SD looks
like an attempt to get a 95% confidence interval for each mean but the
multiplier of 1.96 would be better as a t-based multiplier (small
problem) and the SD is not the same as the standard error of the mean
(big problem). Better to use -ci- on each group, something like this.

foreach x of varlist a-d {
         ci  `x' if group == 1
         gen `x'_upper_1  = r(ub)
         gen `x'_lower_1  = r(lb)
         ci  `x' if group == 2
         gen `x'_upper_2  = r(ub)
         gen `x'_lower_2  = r(lb)
}

Naturally your values for -group- may not be 1 and 2.

All that said, the strategy of using -generate- to put several single
constants in new variables may not be ideal for what you really want
downstream, but I will stop there.

Nick
[email protected]


On 4 July 2013 11:27, Nick Bornschein <[email protected]> wrote:
> Hi,
>
> I want to include multiple generate commands in a ttest loop (which is
> working perfect without the generate), but I don't get it...
>
> foreach x of varlist a-d {
> ttest `x', by(group)    {
> gen lower_`x' = r(mu_1) - r(sd_1)*1.96    {
> gen upper_`x' = r(mu_1) + r(sd_1)*1.96    {
> gen lower2_`x' = r(mu_2) - r(sd_2)*1.96    {
> gen upper2_`x' = r(mu_2) + r(sd_2)*1.96    {
>     }
>     }
>     }
>     }
>     }
> }
>
> How to integrate the generate?
>
>
*
*   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