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: Looping a poisson regression analysis for levelsof a variable


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Looping a poisson regression analysis for levelsof a variable
Date   Fri, 12 Oct 2012 12:34:16 +0100

I see various bugs in your code.

1. You -restore- just once, outside all your loops. That won't do what
you want.

2. If you manipulate -period- to be just 0 and 1 then the test

inlist(period, 0,`l'),

will only find two periods if `l' is in fact 1. If `l' is 2, 3, or 4,
then the test will be

inlist(period,0,2)

etc. which will only find one period as there are no values with
period == 2 etc. (you just ensured that).

Awkward features in your code are that

3. As you always want period 0, you should not be cycling over all the
values of period.

4. You are repeating some operations unnecessarily for each value of -age-.

5. I try to avoid lower case l as a macro name because it is hard to
read and to distinguish from the numeral one. This is important for
you when you want others to read your code.

What you want is I think more nearly

levelsof age, local(age)
preserve
forval p = 1/4 {
	keep if period==0 | period==`p'
	replace period = 1 if period==`p'	
	
	foreach a of local age {
			cap nois: poisson st period if age==`a', exposure(pyears) irr
	}
	
	restore
}


On Fri, Oct 12, 2012 at 9:35 AM, Tim Evans <Tim.Evans@wmciu.nhs.uk> wrote:

> I am running a Poisson regression on counts of surgery between two periods (Stata 11.2), however what I have is more than two periods in my data. Want I want to do is work out a loop that keeps the base period, but then cycles through the other periods. In this I have realized that my variable needs to be coded 0,1 or 1,2 etc, but my periods are 0,1,2,3,4. I cant 'keep' periods 0 and 2 as this returns incorrect IRRs.
>
> My data are set up as follows
>
>
> age     period  st      pyears
> 1       0       1266    1002323
> 2       0       3230    1687257
> 3       0       1251    614507
> 1       1       1553    1061158
> 2       1       4072    1665670
> 3       1       1517    658308
> 1       2       1536    1033397
> 2       2       4648    1739356
> 3       2       1700    671664
> 1       3       1562    1065275
> 2       3       5452    1799757
> 3       3       1770    649461
> 1       4       1901    1189778
> 2       4       6311    1900265
> 3       4       1765    651825
>
> The code I have tried and failed with is this (it fails to restore the data so that I can keep period 0 and 2, period 0 and 3 and so on) At the finish I would like to age for each age group, IRR for period 1 v 0, period 2 v 0, period 3 v 0 and period 4 v 0:
>
> levelsof period, local(levels)
> levelsof age, local(age)
> preserve
> foreach l of local levels {
> foreach a of local age {
> keep if period==0 | period==`l'
> tab period
> replace period = 1 if period==`l'
> cap nois: poisson st period if age==`a' & inlist(period, 0,`l'), exposure(pyears) irr
> }
> }
> restore
>

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index