Announcement

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

  • best method for graphing logistic regression predictions for categorical independent variables

    Hi all,

    I have looked around this forum and on the internet for advice on graphing logistic regression results and haven't had much luck. If this has been answered before and I missed it, please let me know where and sorry for the double post

    In my regression, I am looking at the effect of neighborhood social status on the likelihood an aggravated assault case is solved (Do police use less effort to solve cases in lower status neighborhoods?) In my discussion, I would love to discuss the difference in probability a case is solved if the victim is a male or female. For example, (hypothetically) As neighborhood social status increases, the likelihood a case involving a female victim is solved increases, whereas the likelihood a case involving a male victim is solved decreases.

    DV - Case outcome: (1) solved (0) not solved
    IV - Neighborhood Status: ordinal, ranging from 4 - 20
    IV - Victim Sex: (1) female (0) male

    thanks,
    Brad
    Last edited by Brad Silberzahn; 24 Jan 2015, 17:02.

  • #2
    So, first your modeling: the result you are interested in describing is an interaction effect between status and gender. Not seeing your data, here's an example of something similar using the auto.dta

    Code:
    clear*
    sysuse auto
    logit foreign c.mpg##i.rep78
    margins rep78, at(mpg=(15(10)35)
    marginsplot
    In the above code, foreign is analogous to your "solved" variable. rep78 is analogous to your female/male variable. And mpg is analogous to your neighborhood status variable.

    -marginsplot- has a lot of options that allow you to fine-tune the appearance of the graph.

    Note the use of a double ## in the logit syntax; also the c. in front of mpg (status) is required, the i. in front of rep78 is optional (though I recommend using it for clarity).
    Last edited by Clyde Schechter; 24 Jan 2015, 17:19. Reason: After I posted this, I got an "error" message, so I thought it didn't come through, and reposted below. In the interim I noticed that I had mistakenly omitted a parenthesis in the code, so

    Comment


    • #3
      So, first your modeling: the result you are interested in describing is an interaction effect between status and gender. Not seeing your data, here's an example of something similar using the auto.dta

      Code:
      clear*
      sysuse auto
      logit foreign c.mpg##i.rep78
      margins rep78, at(mpg=(15(10)35))
      marginsplot
      In the above code, foreign is analogous to your "solved" variable. rep78 is analogous to your female/male variable. And mpg is analogous to your neighborhood status variable.

      -marginsplot- has a lot of options that allow you to fine-tune the appearance of the graph.

      Note the use of a double ## in the logit syntax; also the c. in front of mpg (status) is required, the i. in front of rep78 is optional (though I recommend using it for clarity).

      Comment


      • #4
        Clyde, Thanks for the quick reply!

        I used your code for my data and it worked great. the mpg=(startvalue(increment) endvalue) got me for a minute but other than that there was no problems.

        Thanks again!

        Comment


        • #5
          Also, I am a big fan of Patrick Royston's mcp command, available from SSC. His article on the command can be found at

          http://www.stata-journal.com/article...article=gr0056

          I describe it at

          http://www3.nd.edu/~rwilliam/stats3/Margins03.pdf

          Here is a simple example.

          Code:
          webuse nhanes2f, clear
          logit diabetes i.black i.female age c.age#c.age, nolog
          mcp age black
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	9.3 KB
ID:	674001
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          StataNow Version: 18.5 MP (2 processor)

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

          Comment


          • #6
            Richard Williams, thank you for pointing me in the direction of that command, it is very useful. I appreciate it!

            -Brad

            Comment


            • #7
              [QUOTE=Clyde Schechter;n673899]So, first your modeling: the result you are interested in describing is an interaction effect between status and gender. Not seeing your data, here's an example of something similar using the auto.dta

              Code:
              clear*
              sysuse auto
              logit foreign c.mpg##i.rep78
              margins rep78, at(mpg=(15(10)35))
              marginsplot
              Hi,Clyde. I am a new to STATA, i wonder is it possible using STATA to plot where LogOR instead of probability as y axis, and one variable in the model as x axis.

              Thank you very much!

              Comment


              • #8
                Yes, you can. Run the code below and you will see that the log odds are plotted for the model that has a single continuous predictor variable. It it just a modification of what Clyde showed earlier, removing the second variable in order to make it one variable as you ask about. I highlight in red-colored font the change in options that makes the plot of log odds instead of probability.
                Code:
                version 15.1
                
                quietly sysuse auto
                
                logit foreign c.mpg, nolog
                
                margins, at(mpg=(15(10)35)) noatlegend predict(xb)
                
                marginsplot , ///
                    title("") ///
                    ylabel( , angle(horizontal) nogrid) ///
                    plotopts(lcolor(black) mcolor(black) mfcolor(white)) ///
                    ciopts(lcolor(black))
                
                exit

                Comment


                • #9
                  Dear Joseph, i just saw you reply after five years.... i wish i could plot a LogHR instead of probability as y axis, and one variable in the model as x axis, piz check picture i uploaded, thanks very much.
                  https://www.ahajournals.org/cms/10.1...3508fig04.jpeg

                  Last edited by Krystaller Zhao; 09 Dec 2024, 20:00.

                  Comment


                  • #10
                    Originally posted by Krystaller Zhao View Post
                    i wish i could plot a LogHR instead of probability as y axis, and one variable in the model as x axis
                    You don't show what you're using to fit the model, but wouldn't the graph be made with something along the following lines?
                    Code:
                    version 18.0
                    
                    clear *
                    
                    webuse drugtr
                    
                    stcox i.drug c.age
                    
                    summarize age, meanonly
                    margins , at(age = (`r(min)' `=(r(min) + r(max))/2' `r(max)')) ///
                        predict(xb) // or equivalently expression(ln(predict(hr)))
                    marginsplot , ///
                        title("") scheme(s2color) ///
                        ciopts(lcolor(black)) level(67) ///
                        plotopts(lcolor(black) msize(medlarge) mcolor(black) mfcolor(white)) ///
                        ylabel( , angle(horizontal) nogrid)
                    
                    exit

                    Comment


                    • #11
                      "In fact, I am so confused to understand this graph. For example, how should I interpret Log hazard > 0, Log hazard == 0, and Log hazard < 0? Specifically, for intervention group group==1 and group==0. why we use margrins function here

                      Comment


                      • #12
                        Well, it's log hazard ratio and not log hazard. I'm not sure what you're referring to with the two intervention groups—you still haven't shown any code or data that you're using. Are you referring to the figure? I don't know what function that author or those authors used. All I've seen is the figure you've linked to and not the article itself.

                        Comment

                        Working...
                        X