Announcement

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

  • Run Multiple regression on Loop with one control variable changing

    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 dependent variables is v1....v10, variable of interest is T, 10 control variables(X: x1....x10) and further 7 control variables (v1_, ..... v7_) which will go differently to each regression.
    For example v1_ will go with regression where dependent variables is v1, likewise.

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

    reg v1 T v1_ X ,
    reg v2 T v2_ X
    reg v3 T v3_ X

    .
    .
    .
    .
    reg v7 T v7_ X
    reg v8 T X
    reg v9 T X
    reg v10 T X

    for each regression I need output them into word file using esttab *

    Can anyone help me on this ?

    Thank you!

  • #2
    If your variables are really named v1 through v10 and v1_, v2_, ..., v7_, then you can do this:

    Code:
    local estimates
    forvalues j = 1/7 {
        reg v`j' T v`j'_ X
        estimates store  v`j'
        local estimates `estimates' v`j'
    }
    forvalues j = 8/10 {
        reg v`j' T X
        estimates store v`j'
        local estimates `estimates' v`j'
    }
    
    esttab `estimates'
    Note: Code not tested as no example data is provided.

    If your variables are not actually named that way, then the above code will not do the trick. In that case, please use the -dataex- command to show example data that includes the relevant variables, and point out which variables correspond to the ones you called v1-v10 and v1_-v7_ in #1. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Thank you for your reply.

      My variables are really named like I have mentioned except one mistake I have done in explaining the situation. The 7 control variables (v1_, ..... v7_) which will go differently to each regression are not in order. For example, v1_ to v3_ are controlled, but not the following variables; there is a discontinuity. 7 out of these 10 are controlled.

      To be more specific, 7 v`j' have their counterpart v`j'_ to be controlled.

      Data could not be shared due to confidentiality clause.

      Comment


      • #4
        My variables are really named like I have mentioned except one mistake I have done in explaining the situation. The 7 control variables (v1_, ..... v7_) which will go differently to each regression are not in order. For example, v1_ to v3_ are controlled, but not the following variables; there is a discontinuity. 7 out of these 10 are controlled.

        To be more specific, 7 v`j' have their counterpart v`j'_ to be controlled.
        Sorry, I don't understand at all.

        Comment


        • #5
          what I want to do is run 10 regressions by adding different control variable one at a time.

          reg v1 T v1_ X ,
          reg v2 T v2_ X
          reg v3 T v3_ X
          reg v4 T X ,
          reg v5 T v5_ X
          reg v6 T X
          reg v7 T X ,
          reg v8 T v8_ X
          reg v9 T v9_ X
          reg v10 T v10_ X

          How can I make a set of these 7 variables and make some foreach loop to draw the specific control variable for the respective dependent variable?

          Comment


          • #6
            Code:
            forvalues i = 1/10 {
                if !inlist(`i', 4, 6, 7) {
                    reg v`i' T v`i'_ X
                }
                else {
                    reg v`i' T X
                }
            }

            Comment


            • #7
              Thank you a lot..

              Comment

              Working...
              X