Bookmark and Share

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

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

RE: st: using foreach in regression models

From   Tim Evans <>
To   "''" <>
Subject   RE: st: using foreach in regression models
Date   Tue, 25 Oct 2011 11:10:04 +0100


Thanks for your comments, they have opened up possibilities I never considered existed with macros - I've more work to do clearly.

Phil, thanks for your comments. Afraid that the iterate(10) option was a throwback to some lazy cutting and pasting of code from another model - which incidentally did have a problem until I fixed it. My models do converge so I should just remove this.
With regards to factor variables - does this only work on numerical values - I've used your code (which incidentally worked fine - thanks for that) but had to generate a new variable for one of my string variables.

Why is using -logistic- a better idea than using -logit- with O/R option if they give the same output? 

-----Original Message-----
From: [] On Behalf Of Phil Schumm
Sent: 25 October 2011 10:45
Subject: Re: st: using foreach in regression models

On Oct 25, 2011, at 4:03 AM, Tim Evans wrote:
> I am running a number of logistic regression models (Stata 11.2), using the same dependant variable, but cycling through a number of independent variables to obtain 'unadjusted' regressions like such:
> xi: logit treatmentinsccnetre2 i.agegrp, iterate(10) or
> xi: logit treatmentinsccnetre2 i.NRCENT, iterate(10) or
> xi: logit treatmentinsccnetre2 i.id07, iterate(10) or
> xi: logit treatmentinsccnetre2 i.site_4, iterate(10) or
> I can condense this down to the following
> foreach v of varlist agegrp id07 NRCENT site_4 {
> xi: logit treatmentinsccnetre2 i.`v', iterate(10) or
> }
> This is fine, however, I also do want to run the analysis by introducing a range of other variables in order to 'adjust' for their effect. The list of variables will always be the same, but although the list is the same, some variables move around the list - if they are the variable of interest i.e:
> xi: logit treatmentinsccnetre2 i.agegrp i.NRCENT i.site_4 i.id07 i.yydx, iterate(10) or
> xi: logit treatmentinsccnetre2 i.NRCENT i.site_4 i.agegrp i.id07 i.yydx, iterate(10) or
> xi: logit treatmentinsccnetre2 i.id07 i.NRCENT i.site_4 i.agegrp i.yydx, iterate(10) or
> xi: logit treatmentinsccnetre2 i.site_4 i.agegrp i.id07 i.NRCENT i.yydx, iterate(10) or
> What I would like to do is use the loop I employ for the unadjusted model, but be able to use something like `v' in order to condense the code from the above.

No need to do anything fancy here:

    loc covariates "i.x3 i.x4 i.x5"
    foreach var of varlist x1 x2 x4 {
        logistic y i.`var' `covariates'

Note that in my example, x4 is both a "covariate of interest" and a "control variable."  In the third model, x4 will appear at the top of the results table.  To wit: repeating covariates in the varlist is not a problem; only the first instance of each will be used.

Also, a couple of additional comments:

    1) Use factor variables (the xi: prefix is no longer necessary)
    2) Consider using -logistic- if you want odds ratios
    3) I'm not sure why you're using the -iterate()- option above; if your model is failing
       to converge then you've got a problem

-- Phil

*   For searches and help try:

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:

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