Announcement

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

  • Marginsplot x axis

    Dear Statlist,

    I am using a regression model with a continous outcome variable and two binary independent variables. this are included as main effects and as interaction.
    this is followed by the margins command and afterwards the margins plot.

    my code reads as follows:

    Code:
    reg TOTAL sex##age 
    margins, at(age=(0 1) sex=(1 2))
    marginsplot, ytitle(TOTAL) xtitle(agegroup) title("Total") ///
    plot1opts(lcolor(black) lpattern("....") mcolor(black)) ///
    plot2opts(lcolor(gs6) lpattern("--") mcolor(black)) ciopt(color(black)) ///
    xlabel(0 "kids" 1 "teens") graphregion(margin(large))
    everyhing works so far but what i do not like is that on the x axis the kids and teens label as well as the dots in the plot are very much at the left/right of the plot, id like it to be more centered because its an indicator variablen which can only be kids or teens...

    can anyone help me?
    I use Stata 15 (unfortunately...)


    Best and thank you so much

    Anne

  • #2
    Just extend the range of the x axis. Make use of transparency or offset the confidence intervals so as to clearly indicate where one starts and ends.

    Code:
    clear
    set seed 09202023
    set obs 2000
    gen TOTAL = int(rnormal()*1000)
    gen female= runiformint(0,1)
    gen age= runiformint(1,2)
    reg TOTAL i.female##c.age 
    margins, at(female=(0 1) age=(1 2))
    marginsplot, ytitle(TOTAL) xtitle("") title("") ///
    plot1opts(lcolor(red) lpattern("....") mcolor(red)) ci1opt(color(red%50)) ///
    plot2opts(lcolor(blue) lpattern("--") mcolor(blue)) ci2opt(color(blue%50)) ///
    xlabel(0 "kids" 1 "teens", noticks) xsc(range(-1 2)) graphregion(margin(large)) ///
    legend(order(3 "Male" 4 "Female") pos(1) ring(0) region(lstyle(none)) row(2))

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.6 KB
ID:	1727715

    Comment


    • #3
      thats fantastic, thank you so much!

      but Andrew Musau what i dont understand in the regression command is why to use c.age? age is a binary variable in my model?

      thank you for an explanation!
      Last edited by anne jagdberg; 21 Sep 2023, 01:13.

      Comment


      • #4
        With a binary variable, it does not make a difference. The adjusted predictions will be the same either way. However, yes, you should always indicate categorical variables with the 'i.' notation. My not doing so results from the habit of considering age as a continuous variable.

        Comment

        Working...
        X