Announcement

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

  • Saving the results of multiple regression on a single or separate tables

    Hello,

    I am trying to save in one table the results of multiple regressions. Below the regression of interest

    bysort a1: regress d2 e30 a14y f1 e2 gdp d4 d14 a6a ecad8a

    If I use outreg2 it only saves the last estimate rather than all of the coefficients.

    I think foreach might be possible, but I can figure out how to code it.

    ​Many thanks for your help!

  • #2
    If you are going to use -outreg- or its descendants, then you will have to write a loop to store all the results and then pass them along to -outreg-. (I don't use -outreg- or -outreg2- myself, so I can't be sure exactly how you should do this. I'm assuming that, like -esttab-, -outreg2- will accept a list of names of stored estimates as its source of data. So, you need to loop over the levels of a1, storing the estimates at each iteration of the loop, and building up a list of the names of the stored estimates as well. It will look like this:

    Code:
    local for_outreg
    levelsof a1, local(a1s)
    foreach a of local a1s {
        regress d2 e30 a14y f1 e2 gdp d4 d14 a6a ecad8a if a1 == `a'
        estimates store est_`a'
        local for_outreg `for_outreg' est_`a'
    }
    outreg2 `for_outreg'
    Note: This code assumes a1 is a numeric variable. If it's a string, then -if a1 == `a'- should read -if a1 == `"`a'"'-. And also, further complications in the naming of the stored estimates could arise if the values a1 takes on include inadmissible characters.


    But consider an alternative that may be simpler in any case. -statsby- with the -saving()- option will create a data set with the results you want. You can then -list- that out or -export- it to a text file or spreadsheet as you please.

    Comment


    • #3
      Indeed, the merge option of outreg isn't compatible with a bysort command.
      The simplest solution I see (and I use) is to rewrite the bysort command with a foreach loop.

      Code:
      foreach a1 in v1 v2 { /*or foreach a1 in ...*/
      regress d2 e30 a14y f1 e2 gdp d4 d14 a6a ecad8a
      outreg, merge
      }
      This is only an example as there is many ways to code a foreach loop (see help)

      This should work, an is still more elegant to write all the regressions.

      I hope this helped.
      Charlie

      Comment


      • #4
        Thank you Clyde and Charlie for your quick response and insight.

        My problem with the loop is that when doing so, the number of observations is larger than when using the bysort option. I thought this was because when using foreach I am using all observations even from other countries while the 'regressor' variable captures the country-specific effect.

        Would you suggest any other code to save the coefficients, r-squares etc for each regression in excel format aside from using outreg2?

        @Clyde: a1 is numeric as you expected.

        Many thanks!

        Comment


        • #5
          The code in #3 is almost certainly incorrect because the a1 which indexes the loop is never referred to inside the loop. As written, it will simply perform the same regression on all of the data over and over. Probably what is needed is -if a1 == `a1'- at the end of the -regress- command.

          Comment


          • #6
            You are right, I completely mistaken in the previous code
            A correct code would be
            Code:
            foreach v in a1_1 a1_2 .... { /* where a1_1 corresponds to the first value of a1, and so on.*/
            regress d2 e30 a14y f1 e2 gdp d4 d14 a6a ecad8a if a1==`v'
            outreg,merge
            }
            Sorry for the previous mistake, I wrote without checking, just by memory, but my memory doesn't seem to be trustful.
            Thanks for you vigilance Clyde.

            I hope this formula makes more sens.
            Charlie

            Comment


            • #7
              local for_outreg
              levelsof year, local(years)
              foreach y of local years {
              logit A170 i.X047 log_pcBIP i.sex i.age_recoded i.kids i.health_status i.marital_status i.education_level i.religion i.freedom i.conf_gov i.year [pweight= S017] if year=y, vce(robust) or
              estimates store est_`y'
              local for_outreg `for_outreg' est_`y'
              }
              outreg2 `for_outreg'


              Hello, if I do this, it always says invalid syntax..... what is my fault?

              Comment

              Working...
              X