R multcomp:提取cld输出

R multcomp: extract cld output

我构建了一个线性混合模型来分析我的数据,并使用包 multcomp 进行成对比较。我已经用模型输出创建了一个 ggplot,我想 cld() 输出(字母)放在条 .

上方

但我不知道如何从 cld() 输出中提取字母。这是一个 10 的列表,包含许多对象和向量,坦率地说我不完全理解其中的一半(此外,我对列表的经验很少,尽管我多次阅读它们并做了很多基本练习,但我仍然觉得很难掌握)。

当你在 R 中 运行 cld() 时,它会在控制台中准确显示你需要的内容,这就是我想要提取的内容。到目前为止,我的互联网搜索没有产生任何解决方案。

代码

# packages
library(lme4)
library(multcomp)
library(ggplot2)

# dummy dataset
treatment <- c(rep("X",4),rep("Y",4),rep("Z",4))
replicate <- rep(c("A","B","C","D"),3)
Y <- c(18.853,20.165,20.120,21.000,18.772,19.825,20.874,19.001,22.007,21.875,21.235,21.904)
data <- data.frame(treatment,replicate,Y)

# model
lm1 <- lmer(Y ~ treatment + (1|replicate), data=data)
drop1(lm1, test="Chisq")

# pairwise comparison
pc1 <- glht(lm1, mcp(treatment = "Tukey"))
summary(pc1)
cld(pc1, level=0.05) # how to extract this result?

# parameter estimates
lm1.e <- lmer(Y ~ treatment -1 + (1|replicate), data=data)

# plotfile
pfile <- data.frame(treatment=c("X","Y","Z"))
pfile$Y <- fixef(lm1.e)
pfile$SE <- summary(lm1.e)$coefficients[,2]
pfile$minSE <- pfile$Y-pfile$SE
pfile$maxSE <- pfile$Y+pfile$SE

# plot
ggplot(pfile, aes(treatment, Y)) + # how to add cld() result above bars?
  xlab("Treatment") +
  ylab(expression(paste("Y (g)", sep=""))) +
  geom_col(position = "dodge", color="black", fill="white") +
  geom_errorbar(aes(ymin=minSE, ymax=maxSE), width=.2, position=position_dodge(0.9))

使用 multcomp:::print.cld 你可以看到代码,你会看到它基本上调用了:print(x$mcletters$Letters)