Announcement

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

  • After logistic regression, and margins command, is there a way to easily plot odds ratios and confidence limits using marginsplot?

    Consider a simple logistic regression, followed by the margins command, and then marginsplot, as in this simple example (derived from an example in the manual section on logistic regression):

    webuse nhanes2
    logistic highbp sex##agegrp
    margins sex##agegrp
    marginsplot

    The logistic command gives the results in terms of the odds ratio (OR), which is what I want.
    However, the final marginsplot command plots the results using Pr(HighBP) on the y-axis, or the probability of highBP as a function of male and female, not the Odds Ratios and confidence limits that are output by the logistic command.

    ​I want to plot the Odds ratios and confidence limits (rather than the probabilities, as is the default). It seems to me that this should be easily doable somehow by using the simple marginsplot command with some type of option, rather than having to create a new plot from scratch.

    I have now spent many hours going through all the commands and options in the manuals and examples and was ​surprised that no method is to be found for doing this.
    Perhaps I am missing something somewhere, it seems that many people would want to do this, and there has to be an easy way to do it.

    I am an expert in statistics but am just getting familiar with the logistic methods in Stata, so perhaps I am missing something simple.

    Please advise, thanks!

  • #2
    Check out the expression option on the margins command. I am thinking it would be something like this but I haven't really checked it out:

    margins sex##agegrp, expression(exp(predict(xb)))

    Basically, you just have to get the expression command to compute the quantity you want. In this case I told it to exponentiate the log odds, hence computing the odds. I think it works correctly in this much simpler problem, but again, don't trust me, confirm it for yourself:

    Code:
    logistic highbp ibn.sex, nocons
    margins sex, expression(exp(predict(xb)))
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      Note that the method on p. 1147 of the r.pdf file “STATABASE REFERENCEMANUAL RELEASE 13” does not work:
      “expression(pnl exp) specifies the response as an expression. See [R] predictnl for a full description of pnl exp. After estimation by logistic, you might specify expression(exp(predict(xb))) to use relative odds rather than probabilities as the response.”

      The final numbers in the margin output and plot bear no resemblance to the original odds ratios calculated with the logistic command.
      webuse nhanes2
      logistic highbp i.sex i.agegrp
      margins i.sex i.agegrp, expression(exp(predict(xb)))
      marginsplot

      Output of logistic: Output of margins:
      sex Odds Ratio
      Female 0.6496698 0.7463825
      agegrp
      30-39 1.93289 0.3950169
      40-49 3.24251 0.6626585
      50-59 5.984632 1.223055
      60-69 7.013583 1.433338
      70+ 10.26252 2.097309

      Comment


      • #4
        Just skimming, it looks to me like these are just different ways of parameterizing the same numbers. For example, in the margins output, if you take female margin divided by male margin, you get

        .7463825/ 1.148864 = .64967002

        which is the value that logistic reported as the odds ratio for female. Similarly, if you take age group 30-39 margin divided by age group 20-29 margin, you get

        .3950169 / .2043659 = 1.9328905

        which is the odds ratio reported for the 30-39 group in the logistic output.

        So, there are clear relationships between the margins output and the original odds ratios.

        I am not sure it would make sense to expect the margins to be the same as the original logistic results. logistic doesn't report numbers for the reference categories, while margins does. I think the results you are getting are correct, but whether they are what you actually want I don't know. But again. I wouldn't expect margins to give you the exact same numbers that logistic did.
        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        StataNow Version: 19.5 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://www3.nd.edu/~rwilliam

        Comment


        • #5
          The output from margins in that example is the odds, while the output from logit is the odds ratio.
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            Thanks. I am not claiming that the margins command and the logistics command give different outputs, obviously they do. That is precisely the problem, as my goal is to plot the odds ratio with the marginsplot command. You cannot do that after the margins command as shown, because the margins command is not putting out the odds ratio.

            Using the expression "margins sex##agegrp, expression(exp(predict(xb)))" in the margins command (which is the method suggested in the manual) also does give the odds ratio in the final plot with the marginsplot command as the manual claims, as I show in the second post.

            So I appreciate the comments given, but please my goal is to use marginsplot to plot the odds ratios, and there is still no way to do that I can find. Help is still needed.

            Comment


            • #7
              Look into coefplot (Jann, SSC) as an alternative to marginslpot.

              Best
              Daniel

              Comment


              • #8
                Maarten is right about the odds, rather than odds ratios, being reported. I think the graphs are basically right but all numbers need to be divided by the odds of the reference category. Hopefully coefplot can do the trick but if not I wonder if something like the following can be generalized:

                Code:
                webuse nhanes2, clear
                logistic highbp i.sex
                margins sex, expression((exp(predict(xb))/.8824205))
                I manually filled in the odds for the baseline category but I imagine it could be automated.
                -------------------------------------------
                Richard Williams, Notre Dame Dept of Sociology
                StataNow Version: 19.5 MP (2 processor)

                EMAIL: [email protected]
                WWW: https://www3.nd.edu/~rwilliam

                Comment


                • #9
                  I thank you for your comments, but the main question of how to plot odds ratios using marginsplot is not yet correctly answered.

                  Here is a simpler demonstration of my question, which shows that none of the suggested methods works:

                  webuse nhanes2
                  logistic highbp i.sex, allbaselevels
                  margins i.sex
                  margins i.sex, expression(exp(predict(xb)))


                  Results:
                  Output of logistic:
                  Odds Ratio
                  Male 1 (base)
                  Female 0.6980043

                  Output of margins i.sex:
                  Margin
                  sex
                  Male 0.4687691
                  Female 0.3811626

                  It is claimed by one person that the margins command puts out the odds.
                  However, the ratio of female to male margins is 0.813113748, which is obviously not the odds ratio of 0.6980043 shown in the logistic regression output, so this margins command is not outputting the odds as claimed.

                  Another person suggested s the method in the manual on p. 1147 of the r.pdf file “STATABASE REFERENCE MANUAL RELEASE 13,” that is claimed to put out the relative odds (the odds ratio) from the margins command:

                  “After estimation by logistic, you might specify expression(exp(predict(xb))) to use relative odds rather than probabilities as the response.”

                  Here is the output of the Stata command "margins i.sex, expression(exp(predict(xb)))":
                  sex margins
                  Male 0.8824205 not OK
                  Female 0.6159333 not OK

                  Here, the margin for female is 0.6159333, which is obviously not the odds ratio for female which is 0.6980043 a shown by the logistic command. Therefore, the manual has an error and needs to be corrected.

                  It is apparent that the odds that are output by this expression, not the relative odds as claimed (the term “relative odds” in epidemiology refers to the odds ratio, or the odds relative to a baseline, not the odds themselves).

                  I discovered that if you take the ratio of the female margin to the male margin in the output above, you have 0.6159333/0.8824205 = 0.698004296137726, which is the correct odds ratio shown by the logistic command for females 0.6980043.

                  However, my original question and main problem still remains, as I wish to use marginsplot to easily plot the odds ratios. So I need a margins command that will output the odds ratios, not the odds or the probabilities.

                  I do not know enough about the intricacies of the Stata language to know how to make the margins command put out the odds ratio in a way that marginsplot can easily plot the odds ratios (to match those given by the logistic command), but maybe someone out there with more Stata experience can help.

                  Thanks!

                  Comment


                  • #10
                    Would this be what you want, or at least closer? It sets males 20-29 as the baseline category and then gives the odds ratios relative to it for every other combo of sex and age.

                    Code:
                    webuse nhanes2, clear
                    logistic highbp sex##agegrp
                    margins sex#agegrp, expression(exp(predict(xb)))
                    mat b = r(b)
                    scalar base = b[1,1]
                    margins sex#agegrp, expression((exp(predict(xb))/base))
                    marginsplot
                    -------------------------------------------
                    Richard Williams, Notre Dame Dept of Sociology
                    StataNow Version: 19.5 MP (2 processor)

                    EMAIL: [email protected]
                    WWW: https://www3.nd.edu/~rwilliam

                    Comment


                    • #11
                      Originally posted by richard young View Post
                      Using the expression "margins sex##agegrp, expression(exp(predict(xb)))" in the margins command (which is the method suggested in the manual) also does give the odds ratio in the final plot with the marginsplot command as the manual claims, as I show in the second post.
                      That is incorrect. The output of margins in your example gives you odds not odds ratios. The odds and the odds ratio are two very different beasts: An odds is the expected number of successes per failure. An odds ratio is a comparison of these odds across groups, e.g. the odds of success is a factor .65 smaller for women than for men.
                      ---------------------------------
                      Maarten L. Buis
                      University of Konstanz
                      Department of history and sociology
                      box 40
                      78457 Konstanz
                      Germany
                      http://www.maartenbuis.nl
                      ---------------------------------

                      Comment


                      • #12
                        It is claimed by one person that the margins command puts out the odds.
                        However, the ratio of female to male margins is 0.813113748, which is obviously not the odds ratio of 0.6980043 shown in the logistic regression output, so this margins command is not outputting the odds as claimed.
                        To be clear, you are using the wrong set of numbers. You are using the numbers from your first margins command, which is reporting the predicted probabilities; the odds were given by the second. The correct calculation is

                        Code:
                        . di .6159333 / .8824205
                        .6980043
                        The ,698 is the odds ratio reported by logistic. You do note that later in your post, but Maarten and I were both referring to the 2nd margins command when we said margins was outputting the odds.

                        EDIT: On p. 1147 of the reference manual it refers to "relative odds". I agree that the word "relative" seems wrong, It should just say odds. Here is the quote:

                        expression(pnl exp) specifies the response as an expression. See [R] predictnl for a
                        full description of pnl exp. After estimation by logistic, you might specify expres-
                        sion(exp(predict(xb))) to use relative odds rather than probabilities as the response.
                        Last edited by Richard Williams; 20 Jun 2014, 07:38.
                        -------------------------------------------
                        Richard Williams, Notre Dame Dept of Sociology
                        StataNow Version: 19.5 MP (2 processor)

                        EMAIL: [email protected]
                        WWW: https://www3.nd.edu/~rwilliam

                        Comment


                        • #13
                          If you want to try out -coefplot- Ben Jann has an entire paper on it at

                          ftp://repec.sowi.unibe.ch/files/wp1/...3-coefplot.pdf
                          -------------------------------------------
                          Richard Williams, Notre Dame Dept of Sociology
                          StataNow Version: 19.5 MP (2 processor)

                          EMAIL: [email protected]
                          WWW: https://www3.nd.edu/~rwilliam

                          Comment


                          • #14
                            Jann refers to odds ratios several times in his paper, starting on p. 10. Here is one example:

                            Code:
                            sysuse auto, clear
                            logit foreign mpg trunk length turn, nolog
                            coefplot, drop(_cons) xline(1) eform xtitle(Odds ratio)
                            Hopefully you can see the image created:

                            Graph1.gph
                            -------------------------------------------
                            Richard Williams, Notre Dame Dept of Sociology
                            StataNow Version: 19.5 MP (2 processor)

                            EMAIL: [email protected]
                            WWW: https://www3.nd.edu/~rwilliam

                            Comment


                            • #15
                              Originally posted by Richard Williams View Post
                              Would this be what you want, or at least closer? It sets males 20-29 as the baseline category and then gives the odds ratios relative to it for every other combo of sex and age.

                              Code:
                              webuse nhanes2, clear
                              logistic highbp sex##agegrp
                              margins sex#agegrp, expression(exp(predict(xb)))
                              mat b = r(b)
                              scalar base = b[1,1]
                              margins sex#agegrp, expression((exp(predict(xb))/base))
                              marginsplot
                              I am trying to use a version of this code to get ORs for a logistic model with a quadratic term as well as an interaction term (e.g. logit dx c.var1##c.var1##i.var2, or), but the CIs do not appear to be correct (or at least they are not the same as those that are output in a lincom statement). Please see my post here https://www.statalist.org/forums/for...teraction-term for more information. I would love some insight into why the CIs are different (i.e. am I doing something wrong, or should they be different because they are calculated differently?).

                              Comment

                              Working...
                              X