Announcement

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

  • Changing an estimation stored macro with a previously stored macro

    I am trying to change an estimation stored macro with the stored result from the previous estimation. I manage to do so with `e(cmd)' but not `e(inexog)' below.

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . 
    . qui ivreg2 price (weight=length) i.turn headroom, cluster(turn) small
    
    . display e(inexog)
    31b.turn 32.turn 33.turn 34.turn 35.turn 36.turn 37.turn 38.turn 39.turn 40.turn 41.turn 42.t
    > urn 43.turn 44.turn 45.turn 46.turn 48.turn 51.turn headroom
    
    . local x = "`e(inexog)'"
    
    . display "`x'"
    31b.turn 32.turn 33.turn 34.turn 35.turn 36.turn 37.turn 38.turn 39.turn 40.turn 41.turn 42.t
    > urn 43.turn 44.turn 45.turn 46.turn 48.turn 51.turn headroom
    
    . 
    . cap prog drop pretend_to_be_ivreg2
    
    . prog pretend_to_be_ivreg2, eclass
      1.         ereturn local cmd = "ivreg2"
      2.         ereturn local inexog = "`x'"
      3. end
    
    . display "`x'"
    31b.turn 32.turn 33.turn 34.turn 35.turn 36.turn 37.turn 38.turn 39.turn 40.turn 41.turn 42.t
    > urn 43.turn 44.turn 45.turn 46.turn 48.turn 51.turn headroom
    
    . 
    . qui ivreghdfe price (weight=length) headroom, absorb(turn) cluster(turn) // coeff/SE: 4.509
    > 739   .7761883, Fstat: 69.125
    
    . display e(cmd)
    ivreghdfe
    
    . display e(inexog)
    headroom
    
    . pretend_to_be_ivreg2
    
    . display e(cmd)
    ivreg2
    
    . display e(inexog) // DOES NOT WORK
    .
    
    .
    I only managed to get `e(inexog)' to output the desired list of variables when setting, within the program, ereturn local inexog = "...." (list of variable). I would like to do it automatically by making reference to a local macro, as I tried above.

    Code:
    . cap prog drop pretend_to_be_ivreg2
    
    . prog pretend_to_be_ivreg2, eclass
      1.         ereturn local cmd = "ivreg2"
      2.         ereturn local inexog = "31b.turn 32.turn 33.turn 34.turn 35.turn 36.turn 37.turn
    >  38.turn 39.turn 40.turn 41.turn 42.turn 43.turn 44.turn 45.turn 46.turn 48.turn 51.turn he
    > adroom"
      3. end
    
    . qui ivreghdfe price (weight=length) headroom, absorb(turn) cluster(turn) // coeff/SE: 4.509
    > 739   .7761883, Fstat: 69.125
    
    . display e(cmd)
    ivreghdfe
    
    . display e(inexog)
    headroom
    
    . pretend_to_be_ivreg2
    
    . display e(cmd)
    ivreg2
    
    . display e(inexog) // NOW, IT WORKS
    31b.turn 32.turn 33.turn 34.turn 35.turn 36.turn 37.turn 38.turn 39.turn 40.turn 41.turn 42.t
    > urn 43.turn 44.turn 45.turn 46.turn 48.turn 51.turn headroom
    
    .
    Last edited by Paula de Souza Leao Spinola; 25 Jan 2023, 14:11.

  • #2
    I am not sure why you would want to do what you are trying to do here. If there is some (post-estimation) command that works after ivreg2 but not after ivreghdfe there are probably good reasons for that. Manipulating returned results like that is dangerous. Even if you (technically) trick Stata into doing what it would not otherwise do, that does not mean the results you are getting are valid.

    Having that said, the problem with

    Code:
    prog pretend_to_be_ivreg2, eclass
            [...]
            ereturn local inexog = "`x'"
    end
    is that the local(!) macro x is not visible inside the program because it was defined in the (local) namespace outside of the program. You can try something like

    Code:
    prog pretend_to_be_ivreg2, eclass
            [...]
            fvexpand i.turn
            ereturn local inexog = r(varlist)
    end
    where you obviously need some code to extract turn and add it to the list of covariates. I lack the time right now to give this a closer look.

    Comment

    Working...
    X