如何利用ggplot()绘制multcomp包glht()计算得到的置信区间?
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 ContrastsFit: lm(formula = Years ~ Attr, data = MockJury)
Quantile = 2.3749
95% family-wise confidence levelLinear 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
lwrtoupr) 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 - Beautifulincludes 0, so we don’t have evidence of a meaningful difference between these groups. The interval forUnattractive - Averagejust barely includes 0, suggesting a marginally non-significant difference.
- If the interval (spanning
内容的提问来源于stack exchange,提问作者Khashir

