You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用ggplot()绘制multcomp包glht()计算得到的置信区间?

Calculating Simultaneous Confidence Intervals for Multiple Mean Comparisons with multcomp

If you need to compute simultaneous confidence intervals for pairwise treatment group comparisons in R, the glht() function from the multcomp package is a reliable tool—especially when using Tukey contrasts to adjust for multiple testing. Here's a practical walkthrough:

Step 1: Fit the Linear Model

First, we start by fitting a linear model to our dataset using lm():

# Fit the base linear model
model <- lm(Years ~ Attr, data = MockJury)

Step 2: Compute Tukey Contrasts & Confidence Intervals

Next, we use glht() to generate simultaneous confidence intervals with Tukey contrasts. This method adjusts the intervals to maintain a specified family-wise confidence level (we’re using 95% here):

# Load the multcomp package
library(multcomp)

# Calculate Tukey contrasts and corresponding confidence intervals
tukey_results <- glht(model, linfct = mcp(Attr = "Tukey"))

# View the formatted confidence interval output
confint(tukey_results)

Sample Output

Running the above code will produce results like this:

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

Key Interpretations

  • Family-wise confidence level: The 95% value means we’re 95% confident that all these intervals capture the true difference in means for their respective group pairs.
  • Linear Hypotheses breakdown: Each row represents a pairwise comparison of group means, with the null hypothesis being that the difference between the two groups is 0.
    • If the interval (spanning lwr to upr) includes 0, we can’t reject the null hypothesis—indicating no statistically significant difference between those two groups at the 95% family-wise level.
    • For example, the interval for Average - Beautiful includes 0, so we don’t have evidence of a meaningful difference between these groups. The interval for Unattractive - Average just barely includes 0, suggesting a marginally non-significant difference.

内容的提问来源于stack exchange,提问作者Khashir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:01:26