我在绘制GLM logit模型的结果时难以在对数刻度上显示为优势比。最后,我想从不同模型中获得估计值,并将结果绘制在一个图表上,如此处所示(https://www.ctspedia.org/do/view/CTS...ClinAEGraph001)。你有什么见解吗?
答案 0 :(得分:2)
我本周正在制作类似的情节,并发现最好先生成垂直运行的置信区间,最后使用coord_flip()
转换为水平:
library(tidyverse)
library(ggplot2)
library(broom)
set.seed(1234)
# Using the builtin Titanic dataset as example data for a GLM
tdf = as.data.frame(Titanic)
m1 = glm(Survived == "Yes" ~ Class + Sex, data = tdf, family = "binomial", weights = Freq)
m1_preds = tidy(m1, conf.int = TRUE, exponentiate = TRUE) %>%
mutate(Model = "m1")
# Create modified data by mixing up the frequencies - doesn't do anything meaningful,
# just a way to get different coefficients
tdf$FreqScrambled = sample(tdf$Freq)
m2 = glm(Survived == "Yes" ~ Class + Sex, data = tdf,
family = "binomial", weights = FreqScrambled)
m2_preds = tidy(m2, conf.int = TRUE, exponentiate = TRUE) %>%
mutate(Model = "m2")
# At this point we have a table of odds ratios and confidence intervals
# for plotting
ors = bind_rows(m1_preds, m2_preds)
ors
dodger = position_dodge(width = 0.3)
# Elements like pointrange and position_dodge only work when the outcome
# is mapped to y, need to go through with OR set as y then flip at the
# end
ggplot(ors, aes(y = estimate, x = term, colour = Model)) +
geom_pointrange(aes(ymin = conf.low, ymax = conf.high),
position = dodger,
size = 1.2) +
geom_hline(yintercept = 1.0, linetype = "dotted", size = 1) +
scale_y_log10(breaks = c(0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10),
minor_breaks = NULL) +
labs(y = "Odds ratio", x = "Effect") +
coord_flip(ylim = c(0.1, 10)) +
theme_bw()
结果是森林情节如下: