Announcement

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

  • for each value of a categorical variable, run a regression, save and append table

    Dear all,

    I have a categorical variable that takes six values categ=(1,2,3,4,5,6). I would like to run a regression for each category and append the results into a table.

    I am running the following commands as an example:

    bysort categ: reg lntrade war alliance
    outreg2 using OLScateg.doc, word replace
    outreg2 using OLScateg.doc, word append


    But this only gives me the table of the last category, where category=6

    How can I make a loop that takes each regression result and appends it into a table?

    Thanks so much

    Best,
    Ilona




  • #2
    Ilona:
    you may want to take a look at -help forval-.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Hi Carlo,

      I have tried this:

      forval categ = 1/6 {
      reg lntrade war alliance , vce(robust)
      outreg2 using OLScat.doc, word replace
      outreg2 using OLScat.doc, word append
      }

      which doesn't go through the values of the variable category. It loops the same things six times and gives one output. Maybe my explanation was bad. Let me try once more to explain:

      I have a variable called categ as a result of grouping two variables, power and contiguity: "egen categ=group(power contiguity)"

      Categ
      takes values from 1 to 6, each corresponding to a set of pair ids. I get the regression results for all six categories at once by applying:

      "bysort categ: reg lntrade war alliance, vce (robust)"

      However, I would like to append each category regression results in one table displaying all six categories next to each other.

      I am sorry if I fail to explain inadequately. I really do not know which command I need for this.

      Thanks for your support!

      Best,
      Ilona

      Comment


      • #4
        Ilona:
        try:
        Code:
        forval i = 1(1)6 {
        reg lntrade war alliance if  categ==`i', vce(robust)
        outreg2 using OLScat.doc, word replace
        outreg2 using OLScat.doc, word append
        }
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Thanks Carlo! By removing the code line with the replace command, it worked beautifully!

          Much appreciated

          Comment

          Working...
          X