Announcement

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

  • Organizing coefplot coefficients by variable rather than model?


    Hi everyone, I was wondering if it was possible to organize coefficients in coefplot by model rather than variable. Take the example produced by the code below. In descending order from the top of the graph, I would like to first show the coefficient for m1 (i.e., married), then the coefficient for m2 (i.e., south), and lastly the coefficients for m3 (i.e., married and south). In addition, I would like the coefficient colors to be constant within variables (e.g., all coefficients for married should be blue and all coefficients for south should be red, regardless of model).

    Any help would be appreciated. I have tried looking but have only found results abour arranging them via size or creating separate graphs (using ||)

    Code:
    sysuse nlsw88,clear
    
    logit union i.married
    margins, dydx(married) post
    estimates store m1
    
    logit union i.south
    margins, dydx(south) post
    estimates store m2
    
    logit union i.married i.south
    margins, dydx(married south) post
    estimates store m3
    
    coefplot m1 m2 m3, xline(0)

  • #2
    coefplot is from SSC (FAQ Advice #12). Thanks for the reproducible example.

    Code:
    sysuse nlsw88,clear
    
    logit union i.married
    margins, dydx(married) post
    estimates store m1
    
    logit union i.south
    margins, dydx(south) post
    estimates store m2
    
    logit union i.married i.south
    margins, dydx(married south) post
    estimates store m3
    
    coefplot (m1\ m3, keep(1.married) label(married))  ///
    (m2 \m3, keep(1.south) label(south)), ///
    aseq swapnames order(m1 m2 m3)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	24.9 KB
ID:	1773231

    Comment


    • #3
      Thank you so much, Andrew Musau !

      Comment


      • #4
        Hi Andrew Musau , I am presenting the analysis related to the question above at a conference, and I want to present the model results one by one without chaing the dimensions (height, width) of the plot. For example, I could do:

        Code:
          
        coefplot (m1, keep(1.married) label(married)), ///
        aseq swapnames graph export fig1-mod1.png, replace    
        
        coefplot (m1, keep(1.married) label(married))  ///
        (m2, keep(1.south) label(south)), ///
        aseq swapnames order(m1 m2) graph export fig1-mod2.png, replace    
        
        coefplot (m1\ m3, keep(1.married) label(married))  ///
        (m2 \m3, keep(1.south) label(south)), ///
        aseq swapnames order(m1 m2 m3) graph export fig1-mod3.png, replace
        However, I will be creating three different graphs such that the literal sizes and positions of the coefficients will change in size in each image. I would like to avoid that.

        When you have a moment, do you have any advice for how to address this?
        Last edited by Jason Smith; 09 Oct 2025, 08:40.

        Comment


        • #5
          Just create the plots using #2 and suppress whatever elements you need to across the plots.

          Code:
          sysuse nlsw88,clear
          
          logit union i.married
          margins, dydx(married) post
          estimates store m1
          
          logit union i.south
          margins, dydx(south) post
          estimates store m2
          
          logit union i.married i.south
          margins, dydx(married south) post
          estimates store m3
          
          
          coefplot (m1, keep(1.married) label(married))  ///
          (m2 \m3, keep(1.south) label(south) ciopts(lc(none)) mc(none)), ///
          aseq swapnames order(m1 m2 m3) leg(order(2)) ylab(1 "m1") ///
          saving(gr1, replace)
          
          coefplot (m1, keep(1.married) label(married))  ///
          (m3, keep(1.married) label(married) noci mcolor(none) )  ///
          (m2, keep(1.south) label(south) mc(stc2) ciopts(lc(stc2))), ///
          aseq swapnames order(m1 m2 m3) leg(order(2 5)) ylab(1 "m1" 2 "m2") ///
          saving(gr2, replace)
          
          coefplot (m1\ m3, keep(1.married) label(married))  ///
          (m2 \m3, keep(1.south) label(south)), ///
          aseq swapnames order(m1 m2 m3) ///
          saving(gr3, replace)
          
          gr combine gr1.gph gr2.gph gr3.gph, col(1)
          Res.:

          Click image for larger version

Name:	Graph.png
Views:	1
Size:	22.0 KB
ID:	1782330

          Comment

          Working...
          X