Hi,

if you like me work in medical research, you have to plot the results of multiple logistic regressions every once in a while. As I have not yet found a great solution to make these plots I have put together the following short skript. Do not expect too much, it’s more of a reminder to my future self than some mind-boggling new invention. The code can be found below the resulting figure looks like this:

Here comes the code. It takes the model and optionally a title as an input and generates the above plot.

plot_odds<-function(x, title = NULL){

tmp<-data.frame(cbind(exp(coef(x)), exp(confint(x))))

odds<-tmp[-1,]

names(odds)<-c(‘OR’, ‘lower’, ‘upper’)

odds$vars<-row.names(odds)

ticks<-c(seq(.1, 1, by =.1), seq(0, 10, by =1), seq(10, 100, by =10))ggplot(odds, aes(y= OR, x = reorder(vars, OR))) +

geom_point() +

geom_errorbar(aes(ymin=lower, ymax=upper), width=.2) +

scale_y_log10(breaks=ticks, labels = ticks) +

geom_hline(yintercept = 1, linetype=2) +

coord_flip() +

labs(title = title, x = ‘Variables’, y = ‘OR’) +

theme_bw()

}

P.s. I know about ggplots “annotation_logticks” but they messed up my graphics, also it is not very often that ORs span more than three orders of magnitude. If they do consider playing with ggplots function or update the line beginning with “ticks <- ” in the above example

**Update 29-01-2013: I replaced the nasty ” as they resulted in some nasty copy-past errors…**

larryThe vcd package offers a good way to make log odds ratio plots. I’ve been using it for years.

kareemi think all the R meta-analysis packages (meta, rmeta and metafor) have that plot

JoseYour error bars include crossbars, which some people take issue with. For example, Andrew Gelman

(http://andrewgelman.com/2011/07/any-good-articles-on-the-use-of-error-bars/):

“My only advice is to get rid of those horrible crossbars at the ends of the error bars. The crossbars draw attention to the error bars’ endpoints, which are generally not important at all.”

Here’s a blog about how to present error bars/confidence intervals.

http://psychologicalstatistics.blogspot.co.uk/2012/05/aesthetics-of-error-bars.html

Regards,

José

gerhiPost authorHi,

thanks for the comment.

You can use “geom_pointrange” instead of “geom_errorbar” to get rid of the crossbars.

Best Gerrit

Pingback: Evolution of a logistic regressionSustainable Research | Sustainable Research

DanielThank you very much for sharing this function!

Although I sometimes got this fuction working, I mostly get an error message and don’t know why.

Here the error message:

“Waiting for profiling to be done…

Fehler in row.names(odds) : Objekt ‘odds’ nicht gefunden”

(Error in row.names(odds): Object ‘odds’ not found)

I simply copied your code snippet and saved it as R-file:

logreg <- glm(Gender ~ ServiceUsage + LowEducation, family=binomial(link="logit"), data=myData)

source("lib/plotOdds.R")

plot_odds(logreg,"Test")

I'm using R-Studio. Does anybody know why this happens?

gerhiPost authorHi Daniel,

I think this was due to the ” and the call to row names(odds) in the original function. The minimal example below should work now.

Best Gerrit

library(reshape2)

library(ggplot2)

x<-rbinom(1000, 1, .5)

y<-rbinom(1000, 1, .5)

z<-rbinom(1000, 1, .5)

data<-data.frame(x, y, z)

model_1<-glm(z~x+y, family=binomial, data = data)

plot_odds<-function(x, title = NULL){

tmp<-data.frame(cbind(exp(coef(x)), exp(confint(x))))

odds<-tmp[-1,]

names(odds)<-c(‘OR’, ‘lower’, ‘upper’)

odds$vars<-row.names(odds)

ticks<-c(seq(.1, 1, by =.1), seq(0, 10, by =1), seq(10, 100, by =10))

ggplot(odds, aes(y= OR, x = reorder(vars, OR))) +

geom_point() +

geom_errorbar(aes(ymin=lower, ymax=upper), width=.2) +

scale_y_log10(breaks=ticks, labels = ticks) +

geom_hline(yintercept = 1, linetype=2) +

coord_flip() +

labs(title = title, x = ‘Variables’, y = ‘OR’) +

theme_bw()

}

plot_odds(model_1)

Pingback: Plotting lm and glm models with ggplot #rstats | Strenge Jacke!