Announcement

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

  • Plotting the coefficient estimates from hundreds of regressions

    I am running regressions of the form:
    Code:
    reg dependent_t treatment1 treatment2 baseline
    Here, dependent_t is the dependent variable measured at time t; I am looking at the 720 different times from t = 0 to t = 719. treatment1 and treatment2 are dummy variables. Finally, baseline is a (continuous) control variable.

    I was able to run the regressions using a loop:
    Code:
    forvalues n = 0/719 {
        reg dependent`n' treatment1 treatment2 baseline
    }
    Each regression gives me two estimated coefficients of interest, namely the coefficients associated with the treatment dummies. I want to plot the estimated coefficients along with their confidence intervals. Alternatively, I want to get these numbers in a table so I can make the plot with some other software.

    I have tried various things (e.g. coef plot) but keep running into issues (e.g. STATA doesn’t seem to let me store 720 ‘models’). If it’s useful, I can try to outline all of these issues but that would make this post much longer.

    Is there any easy way to do this? Many thanks in advance for any suggestions.

  • #2
    I would extract the coefficient (and other data of interest) from the regression on each iteration of the loop, then store those results in a table with the -table- command. Then once the loop finishes running, use the data stored in the table to generate the plot.

    Comment


    • #3
      Thanks Daniel, that's exactly what I'm after. I don't suppose you could give me a better idea of how to store the results in a table? I've tried looking at the documentation but can't figure it out.

      P.S. It does seem like I can get the numbers using outreg2:
      Code:
      forvalues n = 0/719 {
          quietly reg dependent`n' treatment1 treatment2 baseline
          outreg2 using estimates.xls, excel append
      }
      However, this is extremely slow!

      Comment


      • #4
        Hi Itzhak
        Here a small version of the code you could be using:
        Code:
         webuse nlswork
        levelsof year, local(lyear)
        matrix drop _all
        foreach i of local lyear {
            reg   ln_wage age race grade south if year == `i'
            matrix bb=nullmat(bb)\[e(b), `i']
            matrix vv=nullmat(vv)\vecdiag(e(V))
        }
        ssc install lbsvmat
        lbsvmat bb
        lbsvmat vv
        scatter bb5 bb6
        Notice that matrix vv will store the variance not SD of the coefficients, so that needs to be addressed. Alternatively, you could use the contents of r(table) and save instead the Lower and upper values of the CI.
        Im using lbsvmat, as an alternative to svmat, but you could use either.

        Best

        Comment


        • #5
          I'm sorry, I meant to point you toward the -frame- command, not the -table- command. Sorry about that. Although it is not as complete as the solution from FernandoRios, here is a start at another option using -frame- instead of the -matrix- command.

          Code:
          clear
          set obs 1000
          gen treatment1 = runiform()
          gen treatment2 = runiform()
          gen baseline = runiform()
          forvalues n = 0/719 {
              gen dependent`n' = runiform()
          }
          Code:
          frame create results n beta_t1 beta_t2 beta_baseline
          forvalues n = 0/719 {
              quietly reg dependent`n' treatment1 treatment2 baseline
              frame post results (`n') (_b[treatment1]) (_b[treatment2]) (_b[baseline])
          }
          frame change results

          Comment


          • #6
            Hi everyone, thanks for the suggestions. Fernando -- that code seems to work perfectly, and is much faster than outreg!

            Comment

            Working...
            X