Announcement

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

  • How to group categorical variables in marginsplot of an interaction

    I am running margins after a logistic regression to generate predicted probabilities for the interaction of two independent categorical variables.

    Code:
    webuse nhanes2
    quietly logit highbp ib1.race ib8.sizplace
    margins sizplace#race
    Now I'd like to use -marginsplot, recast(bar) horizontal- to create a plot of these probabilities.

    If I was doing this for a summary statistic, I could use -graph hbar- with multiple -, over()- options to get the categories nicely grouped on the y-axis:

    Code:
    graph hbar (mean) bpsystol, over(sizplace) over(race)
    Yields:

    Click image for larger version

Name:	hbar.png
Views:	1
Size:	63.9 KB
ID:	1683322


    Is there a way to replicate this layout using -marginsplot-? The closest I can get is:

    Code:
    marginsplot, recast(bar) horizontal xdimension(race sizplace) yscale(reverse)
    …which gives me 95% of the plot I want, but has repetitive labels and doesn't add spacing between the 'race' groups:

    Click image for larger version

Name:	marginsplot.png
Views:	1
Size:	87.0 KB
ID:	1683323


    I have also tried:

    Code:
    marginsplot, recast(bar) horizontal xdimension(race) plotdimension(sizeplace) yscale(reverse)
    Click image for larger version

Name:	marginsplot2.png
Views:	1
Size:	35.9 KB
ID:	1683324


    …but obviously having the bars overplotted on one another like this is not terribly useful.

    Thanks in advance!

  • #2
    Code:
    ssc install coefplot, replace

    Code:
    webuse nhanes2, clear
    quietly logit highbp ib1.race ib8.sizplace
    margins sizplace#race, post
    est sto m1
    set scheme s1mono
    coefplot (m1,  keep(*1.race) asequation(White) \m1, ///
    keep(*2.race) asequation(Black) \ m1, keep(*3.race) ///
    asequation(Other)), recast(bar) bcolor(black%40)
    I leave you to work a bit on changing the labels!
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	76.7 KB
ID:	1683645

    Last edited by Andrew Musau; 28 Sep 2022, 11:57.

    Comment


    • #3
      Thank you very much!

      Comment


      • #4
        Is it possible to have a different color for each level of Rural in the above chart? For instance, the first bars in White, Black, and Other groups would be the same color, the second bar in each group would be a different color, and so on.

        Comment

        Working...
        X