Announcement

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

  • Storing and accessing output of multiple estimation commands within program

    I've defined a program that generates random data and calls two estimation commands: reg and ppml. I want to store the regression coefficients from each command.

    Code:
    capture program drop case1
    program case1, eclass
      clear
      set obs 1000
      gen z1 = runiform()
      gen z2 = runiform() < 0.2
      gen mu = exp(z1 + z2)
      gen lognorm_sd = 1/mu
      gen epsilon = exp(1 + lognorm_sd*rnormal())
      gen x = epsilon*mu
      gen lnx = log(x)
      reg lnx z1 z2
      ereturn scalar case1_b1 = e(b)[1]
      ereturn scalar case1_b2 = e(b)[2]
      ppml x z1 z2
      ereturn scalar case1_bp1 = e(b)[1]
      ereturn scalar case1_bp2 = e(b)[2]
    end
    I then want to perform a Monte Carlo simulation that calls this program and then summarizes the regressions coefficients across the simulations:

    Code:
    simulate case1_b1=e(case1_b1) case1_b2=e(case1_b2) case1_bp1=e(case1_bp1) case1_bp2=e(case1_bp2), reps(1000): case1
    summarize case1_*
    Unfortunately I get the following error:
    Code:
    invalid syntax
    an error occurred when simulate executed case1
    r(198);
    My guess is that I've stored and.or accessed the estimation coefficients incorrectly, but I haven't found anything in the docs on this case. Can anyone spot the error?

  • #2
    Perhaps this example will point you in a helpful directioni.
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . quietly regress price weight foreign
    
    . scalar case1_b1 = e(b)[1]
    invalid syntax
    r(198);
    
    . matrix list e(b)
    
    e(b)[1,3]
           weight    foreign      _cons
    y1  3.3207368  3637.0013  -4942.844
    
    . scalar case1_b1 = e(b)[1,1]
    
    . display case1_b1
    3.3207368

    Comment


    • #3
      Hi William,

      Thanks for that! I forget that e(b) is a matrix rather than just a vector.

      With the indices fixed, I now face the following problem: my `summarize` command yields
      Code:
      . summarize case1_b1 case1_b2 case1_bp1 case1_bp2
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
          case1_b1 |          0
          case1_b2 |          0
         case1_bp1 |      1,000    .9911039    .0027236   .9803289   .9988012
         case1_bp2 |      1,000    .9966939    .0018173   .9908563   1.001536
      It seems that my OLS coefficients are not being saved. Have I messed up the syntax for that, too?

      Comment


      • #4
        Your regress command creates a set of estimates, and the subsequent ereturn commands add two scalars to the set of estimates. Then your ppml command replaces all those estimates, including the added scalars, with a new set of estimates.

        You need something like the following.
        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . 
        . capture program drop gnxl
        
        . program gnxl, eclass
          1.     quietly regress price weight
          2.     scalar bw1 = e(b)[1,1]
          3.     quietly regress price weight foreign
          4.     scalar bw2 = e(b)[1,1]
          5.     ereturn clear
          6.     ereturn scalar bw1 = bw1
          7.     ereturn scalar bw2 = bw2
          8. end
        
        . 
        . gnxl
        
        . ereturn list
        
        scalars:
                        e(bw1) =  2.044062585696486
                        e(bw2) =  3.32073677075247

        Comment

        Working...
        X