如何用r中的CI绘制多元logistic回归的奇数比

时间:2019-07-03 21:32:16

标签: r ggplot2 glm

我有一个模型,该模型的二进制结果为0.1,四个独立变量,所有变量均为分类变量。我想绘出我的glm的奇数比率。

My model is glm(outcome ~ v1 + v2 + v3, data = data, family = binomial)
All the variables v1, v2 and v3 have multiple categories.

我还发现,有些人建议使用sjp.glm绘制逻辑回归的比值比。但是我无法在r 3.6.0中下载该软件包。

我从该网站http://www.jscarlton.net/post/2015-10-24VisualizingLogistic/找到了以下代码     剧情的结果与我想要的结果非常相似。但是,我不想手动进行。有没有一种方法可以直接绘制glm模型的优势比?

library(ggplot2)

# Create labels
boxLabels = c("Past encounter", "Mutualism WVO:\nNon-hunting wildlife experiences", "Domination WVO:\nEthics of hunting", "Domination WVO:\nWildlife management", "Age", "Education", "Negative attitudes toward box turtles", "Gender")

# Enter summary data. boxOdds are the odds ratios (calculated elsewhere), boxCILow is the lower bound of the CI, boxCIHigh is the upper bound.

df <- data.frame(
  yAxis = length(boxLabels):1,
  boxOdds = c(2.23189,1.315737,1.22866,.8197413,.9802449,.9786673,.6559005,.5929812),
  boxCILow = c(.7543566,1.016,.9674772,.6463458,.9643047,.864922,.4965308,.3572142),
  boxCIHigh = c(6.603418,1.703902,1.560353,1.039654,.9964486,1.107371,.8664225,.9843584)
)

# Plot
p <- ggplot(df, aes(x = boxOdds, y = yAxis))
p + geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed") +
  geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = .2, color = "gray50") +
  geom_point(size = 3.5, color = "orange") +
  theme_bw() +
  theme(panel.grid.minor = element_blank()) +
  scale_y_continuous(breaks = yAxis, labels = boxLabels) +
  scale_x_continuous(breaks = seq(0,7,1) ) +
  coord_trans(x = "log10") +
  ylab("") +
  xlab("Odds ratio (log scale)") +
  annotate(geom = "text", y =1.1, x = 3.5, label ="Model p < 0.001\nPseudo R^2 = 0.10", size = 3.5, hjust = 0) + ggtitle("Intention to remove box turtles from the road")

软件包“ sjp.glm”不可用(对于R版本3.6.0)

0 个答案:

没有答案