Announcement

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

  • Running Multiple Regressions using the Same Independent Variables

    Hi all,

    I have 30 different regressions I would like to run using the same independent variables. I understand that when I set up locals as follows, independent variables are easy to reference during the regression (let y1 be some outcome of interest):

    Code:
    local controls x1 x2 x3 i.x4
    reg y1 $controls
    However, when I define a local with a set of outcome variables, I cannot get it to work in a loop:

    Code:
    local outcomes y1 y2
    foreach i in outcomes {
        reg $outcomes $controls
    }
    The above only produces one regression that has y1 as the dependent variable and y2 as an independent variable (the controls work). Does anybody know what I'm doing wrong here? Also, of secondary concern, I would like to store each estimate as the name of the dependent variable so that I can visualize the results with

    Code:
    estimates table y1 y2, se p keep(coefficient of interest)
    Last edited by Mark Weiss; 02 Feb 2023, 14:42.

  • #2
    You are looping over outcomes, so you want

    Code:
    local controls x1 x2 x3 i.x4
    local outcomes y1 y2
    foreach outcome of local outcomes{
        reg `outcome' `controls'
        est sto `outcome'
    }
    Also note that you are mixing up locals and globals in #1. Locals are referenced with single quotes as above, globals with dollar signs.
    Last edited by Andrew Musau; 02 Feb 2023, 15:01.

    Comment


    • #3
      Hey Andrew,
      Thanks for the response! And apologies for the global/local mixup, I originally tried this with globals instead and forgot to change my code.

      I ran the loop and the .do file runs without producing any output. When I run estout `outcomes', the console tells me "last estimates (.) not found." If I run estout y1, for example, the console tells me "estimation result y1 not found." Any idea what's producing these errors?

      Comment


      • #4
        Do you have the dataset loaded? It's hard to say without seeing the actual Stata output. Copy and paste this within CODE delimiters.

        Comment


        • #5
          Here's what happens:

          Code:
          local controls i.year i.statename female black asian hispanic age65yr college hs own_home employed
          
          . local outcomes coverage persdoc2
          
          .
          . foreach outcome of local outcomes {
            2.         reg `outcome' `controls' [aw = weight], cluster(statename)
            3.         est sto `outcome'
            4. }
          
          .
          . estout coverage
          estimation result coverage not found

          Comment


          • #6
            I do not see any commands loading the dataset, e.g.,

            Code:
            use mydata, clear

            You need to make sure that the dataset is loaded and includes all the variables specified. Also, why not run this directly by copying and pasting into the command window? Any problems will become evident in this way.

            Comment


            • #7
              Hey Andrew - coming back to this thread to say that your method worked once I explicitly wrote a command to load the data. Thank you so much for your help!

              Comment


              • #8
                Originally posted by Andrew Musau View Post
                You are looping over outcomes, so you want

                Code:
                local controls x1 x2 x3 i.x4
                local outcomes y1 y2
                foreach outcome of local outcomes{
                reg `outcome' `controls'
                est sto `outcome'
                }
                Also note that you are mixing up locals and globals in #1. Locals are referenced with single quotes as above, globals with dollar signs.
                Hi Andrew,

                is it possible to run the regressions but different regressors and regressands simultaneously?

                e.g., reg1: y1 x1 x2, reg2: y1 x1 x2 x3, reg3: y2 x1 x2 etc etc

                thanks in advance!

                Comment


                • #9
                  Yes, try

                  Code:
                  help sureg
                  Best wishes

                  (Stata 16.1 MP)

                  Comment

                  Working...
                  X