Announcement

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

  • marginsplot with predictive margins for several variables in the same plot

    Hello everyone,

    I'm estimating a model using
    Code:
     xtlogit y x1 x2, re
    in Stata 14 and have calculated margins using
    Code:
    margins,  at (x1=(0(1)1)) at(x2=(0(1)1)) predict(pu0)
    to get two predictive margins at the fixed values 0 and 1 each for the two regressors x1 and x2. Now I would like to use
    Code:
    marginsplot
    to get a graph running from 0 to 1 on the x-axis and depicting the predictive margins for the two regressors on the y axis. So, for example, when I am at 0 along the x-axis, I would like to have the predictive margin for x1=0 and the margin for x2=0 on the y-axis. When I simply type
    Code:
    marginsplot
    , the result is not useful as it does not show all four margins and seems to have something mixed up along the x-axis, see below.

    Click image for larger version

Name:	Statalist.png
Views:	1
Size:	44.2 KB
ID:	1416181

    Help would be very much appreciated!
    All the best,
    PA

  • #2
    I believe your specification of the at() options in margins is not giving you what you think it is.

    Code:
    sysuse auto
    gen hirep=rep78>=4 & rep78<.
    gen himpg = mpg>21
    logit foreign himpg hirep
    
    * Incorrect
    margins, at(hirep=(0(1)1)) at(himpg=(0(1)1))
    
    * Correct
    margins, at(hirep=(0(1)1) himpg=(0(1)1))
    In the first call to margins, you are calculating four margins. The first sets hirep to 0, *and leaves each observation of himpg as it is. The second sets hirep to 1, leaving other variables as-is; the third sets himpg to 0, all-else as is, and so on.

    In the second, you are setting both variables simultaneously, which is what I think you want.

    Following the second version, marginsplot gives this:
    Click image for larger version

Name:	m1.png
Views:	1
Size:	32.8 KB
ID:	1416222

    Or (shameless plug), with my -mplotoffset- and a few options:

    Code:
    mplotoffset, recast(scatter) xsca(ra(-.5 1.5))
    Click image for larger version

Name:	m2.png
Views:	1
Size:	22.3 KB
ID:	1416223

    Finally, if x1 and x2 in your case are factor (dummy) variables, you might want to let Stata know as much:

    Code:
    xtlogit y i.x1 i.x2 ...
    ...
    margins x1#x2
    ...

    Comment


    • #3
      Dear Nicholas,

      thank you for your reply!

      Actually, though, I am indeed interested in calculating the four margins, exactly as you stated
      The first sets hirep to 0, *and leaves each observation of himpg as it is. The second sets hirep to 1, leaving other variables as-is; the third sets himpg to 0, all-else as is, and so on.
      . The only reason why I am putting both variables into the same command, instead of getting margins for x1 and x2 seperately, is to get these four margins plotted into the same graph to save space ( I don't want two seperate graphs for the four margins). To my knowledge, if I calculated
      Code:
      margins, at(x1=(0(1)1))
      and afterwards
      Code:
      margins, at(x2=(0(1)1))
      , which gives me the margins I want, marginsplot would not be able to combine the results of the two seperate commands into one plot. If I am mistaken here do let me know!

      All the best,
      PA

      Comment


      • #4
        Got it.

        Well, my first response is that I think that's a bad idea. It saves space, perhaps, but at the cost of considerable confusion. I don't think viewers are going to have an easy time figuring out what you've done, and are likely to misunderstand. How about this:

        Code:
        logit foreign himpg hirep
        margins, at(hirep=(0(1)1))
        marginsplot, name(m1,replace) title("") xtitle("First variable") xsca(ra(-.25 1.25))
        margins, at(himpg=(0(1)1))
        marginsplot, name(m2,replace) title("") xtitle("Second variable") xsca(ra(-.25 1.25))
        gr combine m1 m2, ycommon title(Impact of X1 and X2 on Y) note(Each plot holds the other variable at its mean)
        Click image for larger version

Name:	g1.png
Views:	1
Size:	25.3 KB
ID:	1416316
        If you absolutely must have what you ask for, my -combomarginsplot- will help you. It is designed to combine the results of multiple calls to -margins- into one marginsplot. It does not allow you to combine multple different variables into a single dimension, which is what you want. But you can trick it, if you must, into believing that you have the same x-axis variable in each plot:

        Code:
        gen MYVAR = himpg
        logit foreign MYVAR hirep
        margins, at(MYVAR=(0(1)1)) saving(file1, replace)
        
        replace MYVAR=hirep
        * same logit, different varnames
        logit foreign MYVAR himpg
        margins, at(MYVAR=(0(1)1)) saving(file2, replace)
        
        combomarginsplot file1 file2, labels("Impact of X1" "Impact of X2") xtitle("")
        Click image for larger version

Name:	g2.png
Views:	1
Size:	24.9 KB
ID:	1416317
        This approach should work, though you should check carefully that it is really giving you what you want (and that your readers/viewers can properly understand what the plot is showing).

        Comment


        • #5
          Apologies for my late reply, I indeed found your response very helpful and would like to express a belated thank you for your elaborate answer.

          Kind regards

          Comment


          • #6
            Greetings,
            I am using Stata 15.1 for an mlogit model and would like to show margins (with CIs) for my 4 (bi-variate) y vars by my primary x var, controlling for other vars in the model. I want to do this in the same margins plot. Do you have any suggestion on how to execute that? Thanks,

            Comment

            Working...
            X