Announcement

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

  • coefplot margins groups

    Dear Stata community,

    I would like to use -coefplot- to visualize average marginal effects (dydx) that stem from 4 models and are arranged into 2 groups.
    An example of the margins computation (sorry for the complicated variable names):

    Code:
    eststo modelRULEmp: margins, dydx(c.z_v2x_rule) over(i.v2x_regime_01)
    at((mean) z_e_migdppcln regionMY) coeflegend post
    Click image for larger version

Name:	1.png
Views:	1
Size:	30.6 KB
ID:	1552923





    Syntax for -coefplot-
    Code:
    coefplot modelRULEmp modelJUCONmp modelWBrulemp modelHORmp, baselevels
    groups(z_v2x_rule:0bn.v2x_regime_01 z_v2x_rule:1.v2x_regime_01 z_v2x_jucon:0bn.v2x_regime_01 z_v2x_jucon:1.v2x_regime_01 = "{bf:Group 1}"
    c.z_e_wbgi_rle:0bn.v2x_regime_01 c.z_e_wbgi_rle:1.v2x_regime_01 c.z_v2x_horacc:0bn.v2x_regime_01 c.z_v2x_horacc:1.v2x_regime_01 = "{bf:Group 2}")
    mlabel format(%9.2f) mlabposition(12) mlabgap(*2) xline(0) xtitle("Average marginal effects (AME)")
    Click image for larger version

Name:	2.png
Views:	1
Size:	22.4 KB
ID:	1552924





    The graph is organized around "Autocracy" and "Democracy" which was not my intention. I would like to organized the results into "Group 1" and "Group 2".

    Stata does not report any error. I use the latest version of -coefplot-, Stata 16, Win 10. When I visualize coefficients that stem from 1 fracreg model, everything is fine. However, recalling average marginal effects from multiple models remains to be a riddle for me.

    I would appreciate your help.

    Have a good one!
    Pavel
    Last edited by Pavel Satra; 13 May 2020, 10:35.

  • #2
    I'll admit I'm not 100% sure how you want your plot to work. It might be easier to get answers if you give a sample dataset and all of the code.

    Here's a stab at what you might be asking for, using the auto dataset and the appendmodels code by Ben Jann. My solution is quite hack-ey so I hope someone else chimes in to give you more elegant advice.


    capt prog drop appendmodels

    *! version 1.0.0 14aug2007 Ben Jann
    program appendmodels, eclass
    // using first equation of model
    *version 8
    syntax namelist
    tempname b V tmp
    foreach name of local namelist {
    qui est restore `name'
    mat `tmp' = e(b)
    local eq1: coleq `tmp'
    gettoken eq1 : eq1
    mat `tmp' = `tmp'[1,"`eq1':"]
    local cons = colnumb(`tmp',"_cons")
    if `cons'<. & `cons'>1 {
    mat `tmp' = `tmp'[1,1..`cons'-1]
    }
    mat `b' = nullmat(`b') , `tmp'
    mat `tmp' = e(V)
    mat `tmp' = `tmp'["`eq1':","`eq1':"]
    if `cons'<. & `cons'>1 {
    mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
    }
    capt confirm matrix `V'
    if _rc {
    mat `V' = `tmp'
    }
    else {
    mat `V' = ///
    ( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
    ( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )
    }
    }
    local names: colfullnames `b'
    mat coln `V' = `names'
    mat rown `V' = `names'
    eret post `b' `V'
    eret local cmd "whatever"
    end

    sysuse auto

    poisson price gear_ratio weight length c.headroom#c.gear_ratio


    eststo m1: margins, dydx(weight) over(foreign) ///
    at((mean) length headroom) coeflegend post


    poisson price gear_ratio c.weight#c.mpg mpg c.headroom#c.mpg#foreign


    eststo m2: margins, dydx(mpg) over(foreign) ///
    at((mean) headroom) coeflegend post
    ereturn list

    poisson price c.weight#c.mpg c.mpg#foreign#c.headroom

    eststo m3: margins, dydx(headroom) over(foreign) ///
    at((mean) mpg) coeflegend post

    eststo bigmodel: appendmodels m1 m2 m3
    ereturn list

    coefplot bigmodel, baselevels ///
    ///
    headings(mpg:0.foreign = "{bf:Silly Referee Suggestions}" weight:0.foreign = "{bf:Not Policy Relevant}" ) ///
    format(%9.2f) ///
    keep(mpg:0.foreign mpg:1.foreign headroom:0.foreign headroom:1.foreign weight:0.foreign weight:1.foreign) ///
    order(mpg:0.foreign mpg:1.foreign headroom:0.foreign headroom:1.foreign weight:0.foreign weight:1.foreign) ///
    mlabposition(12) mlabgap(*2) ///
    xtitle("Average marginal effects (AME)") mlab

    Comment


    • #3
      Forgot to post the end result.
      Click image for larger version

Name:	example.png
Views:	1
Size:	10.1 KB
ID:	1552956

      Comment


      • #4
        Wow, thank you very much, Alecia! I will proceed as you suggested. Later on, I will report the results in this thread.

        Comment


        • #5
          Originally posted by Pavel Satra View Post
          Wow, thank you very much, Alecia! I will proceed as you suggested. Later on, I will report the results in this thread.
          In the end, I used the "program appendmodels" by Alecia to plot results of multiple average marginal effects that come from different models. It worked without any modifications. I used the graph editor to finalize the headings as things got too complicated.

          Comment


          • #6
            Must have missed this. Yeah, the headings are annoying. Glad you were able to get a result that's acceptable.

            Comment

            Working...
            X