Announcement

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

  • Help on writing a loop for multiple regressions

    I am working on stata to run multiple regressions, I would like to write a loop to achieve this goal.

    My case is there are 10 control variables, v1....v10, dependent variable is A, variable of interest is B

    what I want to do is run 11 regressions by adding control variable one at a time.

    reg A B,
    reg A B v1
    reg A B v1 v2
    reg A B v1 v2 v3
    .
    .
    .
    .

    reg A B v1 v2 v3 v4 v5 v6 v7 v8 v9
    reg A B v1 v2 v3 v4 v5 v6 v7 v8 v9 v10

    for each regression I need output them into word file using outreg2

    Can anyone help me on this, any suggestion and help will be appreciated advance.

    Thank you!


  • #2
    Not being a user of -outreg2-, I can't help you with that part of it, but I can advise on the writing of a loop.
    Code:
    local predictors B v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
    
    local regressors
    
    foreach p of local predictors {
        local regressors `regressors' `p'
        regress A `regressors'
        appropriate outreg2 command here
    }


    Comment


    • #3
      Thank you very much Clyde, it is very helpful!

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        Not being a user of -outreg2-, I can't help you with that part of it, but I can advise on the writing of a loop.
        Code:
        local predictors B v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
        
        local regressors
        
        foreach p of local predictors {
        local regressors `regressors' `p'
        regress A `regressors'
        appropriate outreg2 command here
        }

        Thank you very much Clyde, it is very helpful!

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          Not being a user of -outreg2-, I can't help you with that part of it, but I can advise on the writing of a loop.
          Code:
          local predictors B v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
          
          local regressors
          
          foreach p of local predictors {
          local regressors `regressors' `p'
          regress A `regressors'
          appropriate outreg2 command here
          }

          Hello, Clyder,

          I think I have another question that, each v* variable was from a macro define, for example, macro define v1 a b c d e f.

          When I run the regression, I would like to run v1 as a set combine with B. however the code you taught me still go with one by one, for example:

          when I regress A B v1, I would like to run regress A B a b c d e f.

          The code go with regress A B a

          regress A B a b
          regress A B a b c

          .
          .
          .

          regress A B a b c d e f.

          Please let me know if I did not explain well.

          Comment


          • #6
            I am unsure what you mean.

            Code:
            local v1 a b c d e f 
            local v2 g h
            etc.
            
            regress A B
            regress A B a
            regress A B a b
            ...
            regress A B a b c d e f
            
            regress A B a b c d e f g
            regress A B a b c d e f g h
            etc.
            then you would need an outer loop over v1 through v10 (as before) and then an inner loop over the actual variables within v1, v2, etc.
            Code:
            local predictors  v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
            
            local regressors
            
            foreach p of local predictors {
                foreach v of local `p'
                    local regressors `regressors' `v'
                    regress A B `regressors'
                    appropriate outreg2 command here
                }
            }
            Note that I have removed B from the predictors local macro and just written it separately in the -regress- command because I gather from your description that unlike v1 through v10, B is an actual variable, not a macro that contains a variable.

            Now, if what you meant was that each of v1 through v10 is a local macro containing a block of variables and you want to enter these blocks one at a time (not entering the individual variables in those blocks one at a time), then it is actually just a really minor modification of the original code:

            Code:
            local v1 a b c d e f
            local v2 g h
            local v3 i j k
            // etc.
            
            local predictors B v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
            
            local regressors
            
            foreach p of local predictors {
                local regressors `regressors' ``p''
                regress A `regressors'
                appropriate outreg2 command here
            }
            Note the second layer of macro dereferencing quotes around p in the -local regressors...- command.

            Comment


            • #7
              Originally posted by Clyde Schechter View Post
              I am unsure what you mean.

              Code:
              local v1 a b c d e f
              local v2 g h
              etc.
              
              regress A B
              regress A B a
              regress A B a b
              ...
              regress A B a b c d e f
              
              regress A B a b c d e f g
              regress A B a b c d e f g h
              etc.
              then you would need an outer loop over v1 through v10 (as before) and then an inner loop over the actual variables within v1, v2, etc.
              Code:
              local predictors v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
              
              local regressors
              
              foreach p of local predictors {
              foreach v of local `p'
              local regressors `regressors' `v'
              regress A B `regressors'
              appropriate outreg2 command here
              }
              }
              Note that I have removed B from the predictors local macro and just written it separately in the -regress- command because I gather from your description that unlike v1 through v10, B is an actual variable, not a macro that contains a variable.

              Now, if what you meant was that each of v1 through v10 is a local macro containing a block of variables and you want to enter these blocks one at a time (not entering the individual variables in those blocks one at a time), then it is actually just a really minor modification of the original code:

              Code:
              local v1 a b c d e f
              local v2 g h
              local v3 i j k
              // etc.
              
              local predictors B v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
              
              local regressors
              
              foreach p of local predictors {
              local regressors `regressors' ``p''
              regress A `regressors'
              appropriate outreg2 command here
              }
              Note the second layer of macro dereferencing quotes around p in the -local regressors...- command.
              Hello, Clyder,

              Thank you for your reply. The following is the code I am using, but is not working.

              local month "feb mar apr may jun jul aug sep oct nov dec"
              local cbsasize "size2 size3 size4 size5 size6 size7"
              local region "ma enc wnc sa esc wsc mt pac"
              local educ "sch_9 sch_10 sch_11 sch_12 ed_hs ed_assoc sch_ba sch_ma sch_pro sch_phd"
              local expr "exp expsq expcub expquar"
              local occp "A_occ02 A_occ03 A_occ04 A_occ05 A_occ06 A_occ07 A_occ08 A_occ09 A_occ10"
              local race "hisp black asian other"
              local emptype "private selfemp"

              local predictors snow month cbsasize region educ expr occp race emptype
              local regressors
              foreach p of local predictors {
              local regressors `regressors' `p'
              regress wkhours `regressors'
              }

              what I would like to do is:

              regress wkhours snow
              regress wkhours snow feb mar apr may jun jul aug sep oct nov dec
              regress wkhours snow feb mar apr may jun jul aug sep oct nov dec size2 size3 size4 size5 size6 size7
              regress wkhours snow feb mar apr may jun jul aug sep oct nov dec size2 size3 size4 size5 size6 size7 ma enc wnc sa esc wsc mt pac
              ​​​​​​​regress wkhours snow feb mar apr may jun jul aug sep oct nov dec size2 size3 size4 size5 size6 size7 ma enc wnc sa esc wsc mt pac sch_9 sch_10 sch_11 sch_12 ed_hs ed_assoc sch_ba sch_ma sch_pro sch_phd

              ​​​​​​​etc..




              Comment


              • #8
                Going back to Clyde's solution

                Code:
                 
                 local predictors B v1 v2 v3 v4 v5 v6 v7 v8 v9 v10  local regressors  foreach p of local predictors {     local regressors `regressors' `p'     regress A `regressors'     appropriate outreg2 command here }
                You just need to make one change - dereference "p" twice by using two sets of inverted commas:

                Code:
                 
                 local predictors v1 v2 v3 v4 v5 v6 v7 v8 v9 v10  local regressors  foreach p of local predictors {     local regressors `regressors' ``p''     regress A  B `regressors'     appropriate outreg2 command here }
                This will expand p to v1 and then v1 to its contents. Note that I have moved B down into the loop, since it is not a macro but a variable.

                hth,
                Jeph

                Comment


                • #9
                  Originally posted by Jeph Herrin View Post
                  Going back to Clyde's solution

                  Code:
                  local predictors B v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 local regressors foreach p of local predictors { local regressors `regressors' `p' regress A `regressors' appropriate outreg2 command here }
                  You just need to make one change - dereference "p" twice by using two sets of inverted commas:

                  Code:
                  local predictors v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 local regressors foreach p of local predictors { local regressors `regressors' ``p'' regress A B `regressors' appropriate outreg2 command here }
                  This will expand p to v1 and then v1 to its contents. Note that I have moved B down into the loop, since it is not a macro but a variable.

                  hth,
                  Jeph
                  Hello, Jeph,

                  That is really helpful, thank you very much for two of your time!

                  best,

                  Bo

                  Comment


                  • #10
                    Originally posted by Bo Liu View Post

                    Hello, Jeph,

                    That is really helpful, thank you very much for two of your time!

                    best,

                    Bo
                    Just curious, is there a way to use "global" or "macro define" to run this loop, I have simply tried replace all local to global, but it is not working. I have so many regressions to run using the same set of the independent variables and three dependent variables.

                    global month "feb mar apr may jun jul aug sep oct nov dec"
                    global cbsasize "size2 size3 size4 size5 size6 size7"
                    global region "ma enc wnc sa esc wsc mt pac"
                    global educ "sch_9 sch_10 sch_11 sch_12 ed_hs ed_assoc sch_ba sch_ma sch_pro sch_phd"
                    global expr "exp expsq expcub expquar"
                    global occp "A_occ02 A_occ03 A_occ04 A_occ05 A_occ06 A_occ07 A_occ08 A_occ09 A_occ10"
                    global race "hisp black asian other"
                    global emptype "private selfemp"
                    global predictors `$month' `$cbsasize' `$region' `$educ' `$expr' `$occp' `$race' `$emptype'
                    global regressors

                    reg wkhours snow
                    outreg2 using Table1, word replace title ("Table 1: The Relationship between hours worked and snowfall")

                    foreach p of global predictors {
                    local regressors `$regressors' ``p''
                    regress wkhours snow `$regressors'

                    }

                    Comment


                    • #11
                      Originally posted by Jeph Herrin View Post
                      Going back to Clyde's solution

                      Code:
                      local predictors B v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 local regressors foreach p of local predictors { local regressors `regressors' `p' regress A `regressors' appropriate outreg2 command here }
                      You just need to make one change - dereference "p" twice by using two sets of inverted commas:

                      Code:
                      local predictors v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 local regressors foreach p of local predictors { local regressors `regressors' ``p'' regress A B `regressors' appropriate outreg2 command here }
                      This will expand p to v1 and then v1 to its contents. Note that I have moved B down into the loop, since it is not a macro but a variable.

                      hth,
                      Jeph
                      Just curious, is there a way to use "global" or "macro define" to run this loop, I have simply tried replace all local to global, but it is not working. I have so many regressions to run using the same set of the independent variables and three dependent variables.

                      global month "feb mar apr may jun jul aug sep oct nov dec"
                      global cbsasize "size2 size3 size4 size5 size6 size7"
                      global region "ma enc wnc sa esc wsc mt pac"
                      global educ "sch_9 sch_10 sch_11 sch_12 ed_hs ed_assoc sch_ba sch_ma sch_pro sch_phd"
                      global expr "exp expsq expcub expquar"
                      global occp "A_occ02 A_occ03 A_occ04 A_occ05 A_occ06 A_occ07 A_occ08 A_occ09 A_occ10"
                      global race "hisp black asian other"
                      global emptype "private selfemp"
                      global predictors `$month' `$cbsasize' `$region' `$educ' `$expr' `$occp' `$race' `$emptype'
                      global regressors

                      reg wkhours snow
                      outreg2 using Table1, word replace title ("Table 1: The Relationship between hours worked and snowfall")

                      foreach p of global predictors {
                      local regressors `$regressors' ``p''
                      regress wkhours snow `$regressors'

                      }

                      Comment


                      • #12
                        You've made a few mistakes along the way here, not many. But I'm not going to show you how to fix them because you should not use globals for this purpose. Global macros are an inherently unsafe programming practice and should only be used when there is no viable alternative. This is not one of those circumstances. This should be done using local macros only, as both Jeph and I have shown you how to do.

                        Comment

                        Working...
                        X