如何使用ggplot()绘制glht()置信区间?

时间:2018-05-07 19:22:36

标签: r confidence-interval data-visualization ggplot2

使用(get-first-assessor t-conn {})包中的glht(),可以计算不同治疗方案的置信区间,如此(source):

multcomp

然后可以使用Simultaneous Confidence Intervals Multiple Comparisons of Means: Tukey Contrasts Fit: lm(formula = Years ~ Attr, data = MockJury) Quantile = 2.3749 95% family-wise confidence level Linear Hypotheses: Estimate lwr upr Average - Beautiful == 0 -0.3596 -2.2968 1.5775 Unattractive - Beautiful == 0 1.4775 -0.4729 3.4278 Unattractive - Average == 0 1.8371 -0.1257 3.7999 显示这些间隔: Confidence interval plot

是否可以使用plot()绘制这些间隔(为了保持一致性和美观性)?如果是这样,怎么样?

如果没有,是否有解决方法使输出类似于ggplot()图表?

1 个答案:

答案 0 :(得分:1)

如果将confint的输出转换为数据框,则可以直接在ggplot2中绘制输出。以下是一种方法(使用帮助文件中的glht示例),该方法使用tidy中的broom函数将confint()输出转换为适合的library(multcomp) library(tidyverse) library(broom) lmod <- lm(Fertility ~ ., data = swiss) m = glht(lmod, linfct = c("Agriculture = 0", "Examination = 0", "Education = 0", "Catholic = 0", "Infant.Mortality = 0")) confint(m) %>% tidy %>% ggplot(aes(lhs, y=estimate, ymin=conf.low, ymax=conf.high)) + geom_hline(yintercept=0, linetype="11", colour="grey60") + geom_errorbar(width=0.1) + geom_point() + coord_flip() + theme_classic() 数据框绘图:

geom_segment

enter image description here

更新:回复评论......

置信区间的曲线结束

我不确定向错误栏添加弯曲末端的简单方法,您可以使用confint(m) %>% tidy %>% ggplot(aes(x=lhs, y=estimate)) + geom_hline(yintercept=0, linetype="11", colour="grey60") + geom_segment(aes(xend=lhs, y=conf.low, yend=conf.high), size=0.4, arrow=arrow(ends="both", length=unit(0.05, "inches"), angle=70)) + geom_point() + coord_flip() + theme_classic() 和带有浅箭头角度的箭头来近距离接触。

lhs

enter image description here

lhs订购

在排序方面,estimate将按字母顺序排序,除非将其转换为具有特定订单的因子。例如,下面我们按confint(m) %>% tidy %>% arrange(estimate) %>% mutate(lhs = factor(lhs, levels=unique(lhs))) %>% # unique() returns values in the order they first appear in the data ggplot(aes(x=lhs, y=estimate)) + geom_hline(yintercept=0, linetype="11", colour="grey60") + geom_segment(aes(xend=lhs, y=conf.low, yend=conf.high), size=0.4, arrow=arrow(ends="both", length=unit(0.05, "inches"), angle=70)) + geom_point() + coord_flip() + theme_classic() 的值排序。

conda install -c asmeurer pattern

enter image description here