Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

RE: st: RE: foreach and levels of string variable


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: foreach and levels of string variable
Date   Tue, 28 Sep 2010 16:17:30 +0100

Eric is correct. In addition, my previous post showed exactly how to use -ptrend- to avoid the problem. I never suggested that you had to use -logistic-. 

Nick 
n.j.cox@durham.ac.uk 

Eric Booth

On Sep 28, 2010, at 2:43 AM, Paul O'Brien wrote:
> to avoid the problem you refer to, i could, of course, do a simple logistic on each study.
> but it does not seem to recognise the foreach, just repeats the same analysis of the whole dataset.

That's because you don't have any lname within the -foreach- loop that changes.  See -help foreach-.


Are you wanting to do something like this ?
********************!
clear all
input str16 study
`"Ashok 2002"' 
`"Creinin 2006"' 
`"Ellertson 2003"' 
`"Fine 2010"'
`"Glasier 2010"' 
`"Hamoda 2004"' 
`"Ho&Kwan 1993"' 
`"WHO 1998"' 
`"von Hertzen 2002"'
end
expand 1000
g p = rbinomial(1, .3)
g day = runiform()*100
g pop = int(runiform()*10)
**

levelsof study, local(levelstud)
di "`levelsstud'"
 foreach l of local levelstud {
	di "Study:  `l'"
  logistic p day[fw=pop] if study=="`l'"
}
********************!

On Sep 28, 2010, at 2:43 AM, Paul O'Brien wrote:

> thanks nick,
> 
> this is getting a bit complicated for me.
> 
> to avoid the problem you refer to, i could, of course, do a simple logistic on each study.
> 
> but it does not seem to recognise the foreach, just repeats the same analysis of the whole dataset.
> 
> . levelsof study, local(levelstud)
> `"Creinin 2006"' `"Glasier 2010"' `"Hamoda 2004"' `"Ho&Kwan 1993"' `"WHO 1998"' `"von Hertzen 2002"'
> 
> . foreach l of local levelstud {
>  2. logistic p day[fw=pop]
>  3. }
> 
> Logistic regression                               Number of obs   =       6755
>                                                  LR chi2(1)      =       4.34
>                                                  Prob > chi2     =     0.0373
> Log likelihood =  -604.4323                       Pseudo R2       =     0.0036
> 
> ------------------------------------------------------------------------------
>           p | Odds Ratio   Std. Err.      z    P>|z|     [95% Conf. Interval]
> -------------+----------------------------------------------------------------
>         day |   1.198313   .1013991     2.14   0.033      1.01518    1.414482
> ------------------------------------------------------------------------------
> 
> Logistic regression                               Number of obs   =       6755
>                                                  LR chi2(1)      =       4.34
>                                                  Prob > chi2     =     0.0373
> 

> On 27 Sep 2010, at 22:27, Nick Cox wrote:
> 
>> Here's a fix for you (better than that in the FAQ cited just below):
>> 
>> egen group = group(study), label 
>> su group, meanonly 
>> 
>> forval i = 1/`r(max)' { 
>> 	di "`: label (group) `i''" 
>> 	ptrend p1 np1 day if group == `i'
>> }
>> 
>> This strategy is covered by the FAQ you cited at the beginning of this thread. 
>> 
>> Nick Cox 
>> 
>> I got it. You are now being bitten by an old misfeature. See another Stata FAQ: 
>> 
>> Why does a program defined by an ado-file sometimes not work when given a string comparison? 
>> 
>> <http://www.stata.com/support/faqs/lang/define.html> 
>> 
>> Note that you should have spelled out, as requested by the FAQ, that you were using a user-written program. -ptrend- on SSC is written for Stata 4. 
>> 
>> Nick 
>> n.j.cox@durham.ac.uk 
>> 
>> Nick Cox 
>> 
>> This works for me:
>> 
>> . clear
>> 
>> . set obs 10
>> obs was 0, now 10
>> 
>> . gen which = cond(_n < 6, "some stuff", "other stuff")
>> 
>> . gen y = _n
>> 
>> . levelsof which, local(levels)
>> `"other stuff"' `"some stuff"'
>> 
>> . foreach l of local levels {
>> 2. su y if which == "`l'"
>> 3. }
>> 
>>   Variable |       Obs        Mean    Std. Dev.       Min        Max
>> -------------+--------------------------------------------------------
>>          y |         5           8    1.581139          6         10
>> 
>>   Variable |       Obs        Mean    Std. Dev.       Min        Max
>> -------------+--------------------------------------------------------
>>          y |         5           3    1.581139          1          5
>> 
>> I can't see what you're doing wrong. The code looks right. Are you doing something weird like running code from a do-file editor and the interactive session in combination? 
>> 
>> paul o'brien
>> 
>> thanks martin and neil with the advice on adding quotes. however, i
>> still get the same error:
>> 
>> . levelsof study, local(levelstud)
>> `"Ashok 2002"' `"Creinin 2006"' `"Ellertson 2003"' `"Fine 2010"'
>> `"Glasier 2010"' `"Hamoda 2004"' `"Ho&Kwan 1993"' `"WHO 19
>>> 98"' `"von Hertzen 2002"'
>> 
>> . foreach l of local levelstud {
>>  2. ptrend p1 np1 day if study == "`l'"
>>  3. }
>> Ashok not found
>> r(111);
>> 
>> removed " 2002" in "Ashok 2002" and others - same problem.
>> 
>> On 27 September 2010 11:42, Maarten buis <maartenbuis@yahoo.co.uk> wrote:
>>> 
>>> --- On Mon, 27/9/10, Paul O'Brien wrote:
>>>> . levelsof study, local(levelstud)
>>>> 
>>>> . foreach l of local levelstud {
>>>>  2. ptrend p1 np1 day if study == `l'
>>>>  3. }
>>>> Ashok not found
>>>> r(111);
>>>> 
>>>> where am i going wrong?
>>> 
>>> ptrend p1 np1 day if study == `l'
>>> 
>>> should be
>>> 
>>> ptrend p1 np1 day if study == "`l'"

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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