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]

From |
Tim Evans <Tim.Evans@wmciu.nhs.uk> |

To |
"'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: Looping a poisson regression analysis for levelsof a variable |

Date |
Fri, 12 Oct 2012 15:06:01 +0100 |

Thanks for your replies. Nick, I take on board your comments regarding awkward code. The code you suggest works almost perfectly, although I needed to move the 'preserve' within the loop as only half of the regressions came out of the code. This was the final code: levelsof age, local(age) forval p = 2/5 { preserve keep if period==1 | period==`p' replace period = 2 if period==`p' foreach a of local age { cap nois: poisson st period if age==`a', exposure(pyears) irr } restore } -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox Sent: 12 October 2012 12:34 To: statalist@hsphsun2.harvard.edu Subject: Re: st: Looping a poisson regression analysis for levelsof a variable 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/ _DISCLAIMER: This email and any attachments hereto contains proprietary information, some or all of which may be confidential or legally privileged. It is for the exclusive use of the intended recipient(s) only. If an addressing or transmission error has misdirected this e-mail and you are not the intended recipient(s), please notify the author by replying to this e-mail. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this e-mail or any attachments, as this may be unlawful. * * 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/

**References**:**st: Looping a poisson regression analysis for levelsof a variable***From:*Tim Evans <Tim.Evans@wmciu.nhs.uk>

- Prev by Date:
**st: A Heckman Model with Multi-Logit Selection Equation** - Next by Date:
**Re: st: Manual calculation of scale factors after tobit and/or truncreg [econometrics help]** - Previous by thread:
**Re: st: Looping a poisson regression analysis for levelsof a variable** - Next by thread:
**Re: st: Looping a poisson regression analysis for levelsof a variable** - Index(es):