Announcement

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

  • improving the visual display following margins plot

    hi there, I am trying to improve the visual display of my graphs , could you give me some ideas pls?


    using

    set scheme white_jet


    here my dataset:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(sex Zscore osteoperosis)
    0  -.78 1
    0  -.06 1
    1    .2 1
    1    .3 1
    0  -.66 1
    1  1.97 1
    0   .93 1
    0  -.77 1
    1   .14 1
    1    -1 1
    1  -.95 1
    1  1.38 1
    1  -.84 1
    0 -2.42 2
    1   .44 1
    0   .63 1
    1  -.49 1
    0 -1.26 2
    0   .09 1
    1   .12 1
    end
    label values sex g2
    label def g2 0 "female", modify
    label def g2 1 "male", modify
    label values osteoperosis g1
    label def g1 1 "normal", modify
    label def g1 2 "osteopenia", modify

    Code:
                     
    ologit osteoperosis i.sex age, or          
    
    margins, at(sex=(0/1)) predict(outcome(1)) atmeans 
    marginsplot, plot(sex) recast(bar) allxlabels by(sex) name(m1, replace)
    
    margins, at(sex=(0/1)) predict(outcome(2)) atmeans 
    marginsplot, plot(sex) recast(bar) allxlabels by(sex) name(m2, replace)
    
    margins, at(sex=(0/1)) predict(outcome(3)) atmeans 
    marginsplot, plot(sex) recast(bar) allxlabels by(sex) name(m3, replace)
    
    
    graph combine m1 m2 m3, ///
        title("Predicted Probabilities of Osteoporosis Outcomes by Sex") ///
        col(1)
    Click image for larger version

Name:	Screenshot 2025-07-03 at 15.10.37.png
Views:	1
Size:	997.7 KB
ID:	1779474






  • #2
    I would probably plot every estimate on the same graph. You can do this by adding the predict(outcome()) statement multiple times. In terms of what type of graph to use, I would probably use a scatter or perhaps a connected line (marginsplot default).
    Code:
    webuse margex, clear
    bysort outcome: sum y
    
    ologit group i.sex age
    eststo olog
    
    margins sex, predict(outcome(1)) predict(outcome(2)) atmeans 
    
    marginsplot, title("Predicted Probabilities of Outcome by Sex") /// or change to recast(scatter)
        subtitle("Adjusted predictions with 95% CIs") scheme(s2mono)
    Click image for larger version

Name:	ologit_margins.png
Views:	1
Size:	33.0 KB
ID:	1779488

    Comment


    • #3
      The solution lies in -margins-, to specify predict(outcome(#)) simultaneously. (NOTE: you don't provide full data, you forgot variable age, and depdent variable osteoperosis only have two values 1/2.) And see here for more information, especially answer by Andrew Musau
      https://www.statalist.org/forums/forum/general-stata-discussion/general/1740061-using-margins-to-predict-from-2-level-categorical-variable
      https://www.statalist.org/forums/forum/general-stata-discussion/general/1573269-marginsplot-after-ordered-logit-model


      Code:
      clear
      input float(sex Zscore osteoperosis)
      0  -.78 1
      0  -.06 1
      1    .2 1
      1    .3 1
      0  -.66 1
      1  1.97 1
      0   .93 1
      0  -.77 1
      1   .14 1
      1    -1 1
      1  -.95 1
      1  1.38 1
      1  -.84 1
      0 -2.42 2
      1   .44 1
      0   .63 1
      1  -.49 1
      0 -1.26 2
      0   .09 1
      1   .12 1
      end
      label values sex g2
      label def g2 0 "female", modify
      label def g2 1 "male", modify
      label values osteoperosis g1
      label def g1 1 "normal", modify
      label def g1 2 "osteopenia", modify
      
      ologit osteoperosis i.sex, or          
      
      margins, at(sex=(0/1)) predict(outcome(1)) predict(outcome(2)) atmeans 
      marginsplot, plot(sex) recast(bar) allxlabels by(sex) plotopts(barwidth(0.7)) xtitle("") name(m1, replace)
      Click image for larger version

Name:	m1.png
Views:	1
Size:	130.4 KB
ID:	1779490

      Comment

      Working...
      X