Announcement

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

  • Using foreach in regression

    Hello,

    I wish to run a few regressions over the same set of control variables and dependent variables.

    Independent Variables : var1 var2 var3
    Dependent Variables: var4 var5 var6

    I have to run 3 specifications as follows:

    Specification 1 : regress var4 var1 var2 var3 (Similarly with var5 and var6 as dependent variables too)
    Specification 2: regress var4 var1 var2 (Similarly with var5 and var6 as dependent variables too)
    Specification 3: regress var4 var1 var3 (Similarly with var5 and var6 as dependent variables too)


    I tried something like this for Specification 1 :

    local regressors1 var1 var2 var3
    global regressant var4 var5 var6

    foreach y of global regressant and foreach x of local regressor1 {
    reg `y' `x'
    }


    I even tried the second foreach within the first one, neither worked.


    But it did not work. Can you please tell me how to make two lists (macros) of X and Y variables? Also how to use outreg2 within foreach loop, need to keep appending!

  • #2


    Code:
    foreach y in var4 var5 var6 {
           regress `y' var1 var2 var3
           regress `y' var1 var2
           regress `y' var1 var3
    }
    I can't advise on outreg2 (SSC, as you are asked to explain). I don't ever use it.

    Comment


    • #3
      So basically there is no other way but to write the independent variables? A macro such as global or local cannot be used?

      Comment


      • #4
        Clearly you can use macros here.

        Here is a silly use of macros, for example:

        Code:
        local x1 var4 
        local x2 var5
        local x3 var6 
        with later code referring to such macros.

        Here is another one

        Code:
        local X1 var4 var5 
        local X2 var4 var6 
        local X3 var4 var5 var6
        with later code referring to such macros.

        If you can come up with shorter code, congratulations! Bear in mind, as above, that every macro you define requires one statement.

        The value of using macros is to make your code shorter, simpler and preferably both. I doubt that any further use of macros would do that here, but I am open to refutation.

        Comment


        • #5
          You should be able too double loop. Something like:


          local c c1 c2 c3
          foreach y in y1 y2 y3 {
          foreach x in x1 x2 x3 {
          reg `y' `x' `c'
          }
          }

          the local macro defines control variables.

          Comment


          • #6
            Brad: Referring back to #1

            Specification 1 : regress var4 var1 var2 var3 (Similarly with var5 and var6 as dependent variables too)
            Specification 2: regress var4 var1 var2 (Similarly with var5 and var6 as dependent variables too)
            Specification 3: regress var4 var1 var3 (Similarly with var5 and var6 as dependent variables too)
            your code doesn't deliver any of those as special cases. Leaving out the c's (I don't see any mentioned, but you are clearly right that there might be some in other examples) consider this:


            Code:
            foreach y in y1 y2 y3 {
                foreach x in x1 x2 x3 {
                    di "`y' `x'"
                }
            }
            
            
            
            y1 x1
            y1 x2
            y1 x3
            y2 x1
            y2 x2
            y2 x3
            y3 x1
            y3 x2
            y3 x3

            Comment


            • #7
              I guess I was just thinking he could use this as a framework for his model specifications. But I clearly didn't look at his model specifications as carefully as I should have. Probably one of those situations where getting the code correct takes longer than just specifying the models.

              Comment


              • #8
                Probably one of those situations where getting the code correct takes longer than just specifying the models.
                Indeed. People (me too) will often spend much more time trying to make rough code more elegant than they will ever save in computation. Still, you don't become a better programmer without experience in finessing code.

                Comment


                • #9
                  Let me add the following.

                  Perhaps the initial statement of the problem was oversimplified, and in the actual problem there are more than three dependent variables and more than three independent variables. Then the following (demonstrated on the simplified example) can reduce the typing burden but only if the variables x1 through x3 and x4 through x6 are present in the data in that order with no other variables intervening between x1 and x3 and between x4 and x6.
                  Code:
                  foreach y of varlist var4-var6 {
                         regress `y' var1-var3
                         regress `y' var1 var2
                         regress `y' var1 var3
                  }
                  See help varlist for details on the ways of specifying a list of variables briefly.

                  Comment


                  • #10
                    I often do this kind of thing, usually when there is something complicated I want to do after each model and only want to code that bit once.


                    Code:
                    * dependent vars
                    local dvar1 var4
                    local dvar2 var8 
                    
                    * independent vars
                    local ivars1 var1 var2 var3
                    local ivars2 var1 var5 var6
                    local ivars3 var2 var6 var8 var1 var3 
                    
                    * loops
                    forv d=1/2 {
                        forv i=1/3 {
                                 regress `dvar`d'' `ivars`i''
                        }
                    }
                    hth,
                    Jeph

                    Comment


                    • #11
                      Jeph's example makes an excellent point. His syntax would end up shorter and simpler if it avoided much repetition after each regression. But change the question, and the answer may indeed change. Taste is paramount here, but for what was asked in #1 I still prefer #2.

                      Comment

                      Working...
                      X