Announcement

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

  • How can I Graph the Logistic Regression Plot with Interaction Terms in Stata?

    Hi, There,
    I have a online dataset at
    use http://ssc.wisc.edu/sscc/pubs/files/grad.dta,clear

    The logit model and marginal effect with interaction term in Stata is
    logit grad treat##highSES, or
    margins highSES#treat

    I don't know how to graph the logistic regression plot with interaction terms in Stata below.


    Thank you for your help!
    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	75.3 KB
ID:	1630159

    Last edited by smith Jason; 03 Oct 2021, 19:46.

  • #2
    Possibly this might be helpful: The Stata Journal, 2005, 5 (1), 64-82, M.N. Mitchell & X. Chen, Visualizing main effects and interactions for binary logit models.

    Note that to install the packages discussed in this paper you should type in the command window:
    Code:
    findit viblmcc
    Last edited by ericmelse; 04 Oct 2021, 01:18.
    http://publicationslist.org/eric.melse

    Comment


    • #3
      And here another page with further explanation of this topic.
      http://publicationslist.org/eric.melse

      Comment


      • #4
        You should be able to see the plot for probability with -marginsplot- command followed by your commands already in your code:

        Code:
        logit grad treat##highSES, or
        margins highSES#treat
        marginsplot
        Roman

        Comment


        • #5
          Originally posted by Roman Mostazir View Post
          You should be able to see the plot for probability with -marginsplot- command followed by your commands already in your code:

          Code:
          logit grad treat##highSES, or
          margins highSES#treat
          marginsplot
          Thank you!
          Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	71.4 KB
ID:	1630225
          However, the code you provided can not generate the graph I want.

          Comment


          • #6
            Originally posted by ericmelse View Post
            Possibly this might be helpful: The Stata Journal, 2005, 5 (1), 64-82, M.N. Mitchell & X. Chen, Visualizing main effects and interactions for binary logit models.

            Note that to install the packages discussed in this paper you should type in the command window:
            Code:
            findit viblmcc
            Thank you. However, the code in the resources you provided doesn't work. postgr3 is deprecated for many years.

            Comment


            • #7
              Can someone help me about my question above?
              Thank you!

              Comment


              • #8
                In the original post, the author drew a logistic cumulative distribution function using twoway function and the logistic CDF. The x-axis is basically XB from the regression. Then, they're drawing line segments. You're basically drawing from y = 0, x = XB (i.e. the XB for whatever combo you're drawing) to y = marginal probability, x = XB.

                You can keep searching for an implementation of this type of margins graph superimposed on the logistic CDF. The thing is, you can already see the marginal effects on the probability scale from marginsplot. What's the marginal utility (in the econ sense) of the plot on the logistic curve? I am not sure it's enormous. Actually, if I were teaching a class on logistic regression, it could be useful.

                Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

                When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

                Comment


                • #9
                  Could you please provide the Stata code to create the plot in the 1st floor?
                  Thank you!

                  Comment


                  • #10
                    Originally posted by smith Jason View Post
                    Could you please provide the Stata code to create the plot in the 1st floor?
                    Thank you!
                    I believe you actually want to run this code on your margins command to get the XBs. The second line will post the results to memory and give you the symbolic names of each combo.
                    Code:
                    margins highSES#treat, predict(xb)
                    margins highSES#treat, predict(xb) post coeflegend
                    This code plots the logistic curve.

                    Code:
                    twoway function y = logistic(x), range(-5 5) xtitle("XB") ytitle("Probability")
                    Then, for each line segment, you'd add the line coordinates. The format is y1 x1 y2 x2.

                    y1 = 0
                    x1 = XB
                    y2 = logistic(XB)
                    x2 = XB

                    To explain the above, look at the graph in your first post. It may not be obvious, but everything on that graph has X and Y coordinates. What's the scale? Well, the x-axis runs along the line y = 0. The scale is defined here by the values that the plot takes, and you plotted the logistic function, so inherently the y-axis is going to run from 0 to 1. So, you're typing something like:

                    Code:
                    twoway function y = logistic(x), range(-5 5) || pci 0 0 0.5 0, label("something") || pci . . . . , label("...") || ... || , xtitle("XB") ytitle("Probability")
                    You could obviously do this manually. You'd be better off using the symbolic names of each combination in margins to do the calculations. However, it seems like the pci command may only take actual numbers as inputs, i.e. you can't feed it a scalar or a variable as an input. Or at least, I haven't been able to do that.

                    Anything more detailed than that, and I think unfortunately you will have to ask others for help - remembering that people here aren't obliged to help, we all have our own jobs/dissertations/etc.
                    Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

                    When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

                    Comment


                    • #11
                      Thank you!

                      Comment


                      • #12
                        Can anybody help?
                        Thank you very much!

                        Comment


                        • #13
                          I believe I've given you the individual pieces you need to solve the puzzle, at least manually. I've shown how to plot a logistic curve. I've shown you how to manually plot each line segments (although note that Stata actually applies any point label to the origin, not the destination, so you should reverse the pairs of coordinates I gave you). I've shown how to derive the start and end coordinates for each line segment.

                          If you don't understand individual pieces, can you say which one is the sticking point?

                          If you are asking for an automated solution to prepackage all of this, then remember what I said at the end of #10: we all have our own jobs, so it's not a guarantee that someone finds this style of presentation compelling enough to cook up an automated solution.
                          Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

                          When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

                          Comment

                          Working...
                          X