Announcement

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

  • jitter or offset points in marginsplot for categorical variables?

    Hi all

    I'm wondering if there is a way to jitter or offset points in a marginsplot graph such that the points/CIs are not plotted directly on top of each other? Nonsensical example code and graph below of what i'm trying to prevent.

    Code:
    sysuse auto
    mlogit foreign headroom
    margins, at(headroom=(1(1)5))
    marginsplot, plotdim(headroom) ///
        plotopts(connect(none))


    For example, something more like this (from: https://marginaleffects.com/bonus/plot.html)

    Click image for larger version

Name:	Screenshot 2025-01-07 at 3.13.27 PM.png
Views:	1
Size:	67.4 KB
ID:	1770410

  • #2
    I do not know of any way to get -marginsplot- to do this. However, you can use the -saving()- option (undocumented) to give you a data set underlying the graph, and then you can reorganize it to produce the result you want. I don't know of any program that does this generically, but it is not excessively difficult to write ad hoc code.

    Code:
    clear*
    sysuse auto
    tempfile plot
    mlogit foreign headroom
    margins, at(headroom=(1(1)5)) saving(`plot')
    
    frame create plot
    frame change plot
    use `plot'
    
    keep _predict _at _atopt _margin _ci_*
    label list _predict
    recode _predict (1 = 0 "Domestic") (2 = 1 "Foreign"), gen(foreign)
    replace foreign = foreign + (0.1*(_at - 3))
    label var foreign "Origin"
    reshape wide _margin, i(foreign) j(_at)
    foreach i of numlist 1/5 {
        label var _margin`i' "Headroom = `i'"
    }
    graph twoway (rcap  _ci_lb _ci_ub foreign, xlabel(0 1, valuelabel)) (scatter _margin* foreign)
    Last edited by Clyde Schechter; 07 Jan 2025, 17:24.

    Comment


    • #3
      Ah that makes sense. Works perfect - thanks!

      Comment

      Working...
      X