plotstatamargins

Graphs from several margins in one marginsplot in Stata


I would like to draw margins resulting from the margins command in one marginsplot, but from different margins estimations. Important restriction: These coefficients are within the same min and max and therefore comparable. How do I do that?

Here is a code example:

webuse nhanes2, clear

tnbreg psu weight hdresult iron, iterate(5) // I am using this regression type so I stick with it here

I know that I can put all margins response graphs in one plot

margins, dydx(*)
marginsplot, horizontal xline(0) yscale(reverse) recast(scatter)

But in fact I am running three margins command for each of the regressors separately, because I want to compare the effects if that regressor would vary. The code hence is

foreach var in weight hdresult iron {
  * Procedure to get the numbers for margins right
  quietly summarize `var '
  local max = r(max)
  local step = round(r(max)/6)

  quietly margins, at(`cvar'=(1(`step')`max'))
  marginsplot, title("") ytitle("")
}

This gives me three separate files. But I want all the lines in one single figure, in different colors, of course.

Any suggestions how to do that?


Solution

  • Based on @RobertoFerrer's suggestion to use combomarginsplot I am now tricking that package (thanks to Nicholas Winter):

    webuse nhanes2, clear
    
    * Run regressions
    foreach var in weight hdresult iron {
      * Trick: always regress on the same variable
      gen testvar = `var'
    
      * Any regression where testvar enters first - the identical variable will be omitted
      tnbreg psu ///
         testvar weight hdresult iron, iterate(5)
    
      * Procedure to get the numbers for margins right
      quietly summarize testvar
      local max = r(max)
      local step = round(r(max)/6)
    
      * Margins post estimation
      quietly margins, at(testvar=(1(`step')`max')) saving(margins_`var', replace)
    
      * Drop testvar so that it can be reassigned within the loop
      drop testvar
    }
    
    * Combine the margins graph information
    combomarginsplot margins_weight margins_hdresult margins_iron, labels("Weight" "HDrestul" "Iron")
    

    Of course, it only makes sense to compare coefficients of variables that are all within the same range. This restriction wasn't part of my original answer - sorry for that.