Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to create a loop to select 2 variables from a list?

    Hello!

    I would like to create a loop that allows me to select all the possible combinations of 2 variables from a list of n variables and fit a logistic regression model for each combination.

    For example, let's suppose that there are 8 variables x1 - x8.
    I want to fit 36 logistic models:

    logit y xk xl w z

    where y is the dependent variable, w and z are covariates, and xk and xl are 2 independent variables from the group above.

    Is it possible to do this with a loop instead of writing 36 lines of code?

    Thank you in advance for your help!

  • #2
    For example, let's suppose that there are 8 variables x1 - x8.
    I want to fit 36 logistic models:
    Something's wrong with your description. The number of combinations of 2 variables picked from 8 is 28, not 36. I'll assume you miscalculated that number and really want to do this for all pairs selected from 8 variables.

    Code:
    forvalues i = 1/7 {
        forvalues j = `=`i'+1'/8 {
            logit y x`i' x`j' w z
        }
    }
    By the way, off hand this does not sound like a sensible thing to be doing in the first place. Why are you trying out so many different models? Do you have a rigorous and statistically sound plan for what you are going to do with all of these results?

    Comment


    • #3
      I would use -tuples- by Joseph Luchman, Daniel Klein, and Nick Cox. For example
      Code:
      sysuse auto, clear
      tuples headroom trunk length , min(2) max(2) display
      forval i = 1/`ntuples' {
          regress mpg `tuple`i''
      }
      Code:
      . ssc desc tuples
      
      ---------------------------------------------------------------------------------------------------------------
      package tuples from http://fmwww.bc.edu/repec/bocode/t
      ---------------------------------------------------------------------------------------------------------------
      
      TITLE
            'TUPLES': module for selecting all possible tuples from a list
      
      DESCRIPTION/AUTHOR(S)
            
             tuples produces a set of local macros, each containing a list of
             the names defining a tuple selected from a given list. ...

      Comment


      • #4
        Check out -allpossible-, available at ssc, from whose helpfile I quote:
        allpossible by default (1) computes all possible models fitted by modelcmd to response depvar and subsets of up to 6 predictors from varlist and (2) tabulates various
        summary statistics for each model fitted. Alternatively, (1') the maximum number of predictors fitted may be specified as a number less than 6 by the npmax() option.


        It does all the messy work for you.

        Comment


        • #5
          Thank you all for the useful suggestions!

          I think I solved the issue using -tuples-.

          Re: the number of combinations, of course you are right. I wrote 36 because I was counting 8 additional models that are built using 2 Xp one of which is multiplied by log(X). The thing is that I am running second-order FP models, but I cannot use the -fp- command which would automatize the whole process because I need to check some additional features of each model.

          By the way, I have one more question about a loop. Is there a way to use -forvalues- with non-integer and negative numbers?
          Of course the following does not work:

          forvalues i = -3(0.5)3 {
          gen X`I’ = X^`i’
          replace X`i’ = log(X) if `i’==0
          }

          Many thanks!!


          Comment


          • #6
            By the way, I have one more question about a loop. Is there a way to use -forvalues- with non-integer and negative numbers?
            Of course the following does not work:

            forvalues i = -3(0.5)3 {
            gen X`I’ = X^`i’
            replace X`i’ = log(X) if `i’==0
            }
            The problem is not with forvalues. Your problem is with -gen X`I’ = X^`i’- which will fail because there is no local macro I, so Stata translates this as -gen X = X^`i'- and that fails because variable X already exists. I guess you meant -gen X`i' = X^`i'-.

            Also, the way you've used -if `i' == 0- in the loop is incorrect. This requires an -if- command-, not an -if- condition.

            After you fix that, however, you will have another problem: you can't have a variable named X-3 or X2.5. So this will fail there. So you need to pick some more complicated names for these variables like Xm3 or X2p5. Modifying the loop to do that will be a bit more challenging. Something like this
            Code:
            forvalues i = -3(0.5)3 {
                local suffix: display %2.1f `i'
                local suffix: subinstr local suffix "-" "m"
                local suffix: subinstr local suffix "." "p"
                if `i' != 0 {
                    gen X`xuffix' = X^`i'
                }
                else {
                    gen X`suffix' = log(X)
            }

            Comment


            • #7
              Thank you very much!!

              Comment

              Working...
              X