绘制因子水平组的最小二乘均值

Plot least-squares means for groups of factor levels

我正在寻找一种简单的方法来为另一个因素的每个水平提取(和绘制)一个因素的指定水平组合的最小二乘均值。

示例数据:

set.seed(1)
model.data <- data.frame(time = factor(paste0("day", rep(1:8, each = 16))),
  animal = factor(rep(1:16, each = 8)),
  tissue = factor(c("blood", "liver", "kidney", "brain")),
  value = runif(128)
  )

为因子 "time" 设置自定义对比:

library("phia")
custom.contrasts <- as.data.frame(contrastCoefficients(
   time ~ (day1+day2+day3)/3 - (day4+day5+day6)/3,
   time ~ (day1+day2+day3)/3 - (day7+day8)/2,
   time ~ (day4+day5+day6)/3 - (day7+day8)/2,
   data = model.data, normalize = FALSE))

colnames(custom.contrasts) <- c("early - late",
  "early - very late",
  "late  - very late")

custom.contrasts.lsmc <- function(...) return(custom.contrasts)

拟合模型并计算最小二乘意味着:

library("lme4")
tissue.model <- lmer(value ~ time * tissue + (1|animal), model.data)
library("lsmeans")
tissue.lsm <- lsmeans(tissue.model, custom.contrasts ~ time | tissue)

绘图:

plot(tissue.lsm$lsmeans)
dev.new()
plot(tissue.lsm$contrasts)

现在,第二个图有我想要的组合,但它显示了组合均值之间的差异,而不是均值本身。

我可以从 tissue.lsm$lsmeans 中获取单个值并自己计算组合均值,但我有一种挥之不去的感觉,即有一种我只是看不到的更简单的方法。毕竟所有的数据都应该在lsmobj里面。

early.mean.liver = mean(model.data$value[model.data$tissue == "liver" & 
  model.data$time %in% c("day1", "day2", "day3")])
late.mean.liver = mean(model.data$value[model.data$tissue == "liver" & 
  model.data$time %in% c("day4", "day5", "day6")])
vlate.mean.liver = mean(model.data$value[model.data$tissue == "liver" & 
  model.data$time %in% c("day7", "day8")])
# ... for each level of "tissue"


#compare to tissue.lsm$contrasts
early.mean.liver - late.mean.liver 
early.mean.liver - vlate.mean.liver
late.mean.liver - vlate.mean.liver

期待听到您的意见或建议。谢谢!

除了您在 custom_contrasts 中计算的组均值差异的对比系数之外,另一种方法是计算感兴趣的组均值的对比系数。例如,您可以单独执行此操作 custom.contrasts2.

custom.contrasts2 <- as.data.frame(contrastCoefficients(
    time ~ (day1+day2+day3)/3,
    time ~ (day4+day5+day6)/3,
    time ~ (day7+day8)/2,
    data = model.data, normalize = FALSE))

colnames(custom.contrasts2) <- c("early",
                          "late",
                          "very late")

custom.contrasts2.lsmc <- function(...) return(custom.contrasts2)

lsmeans(tissue.model, custom.contrasts2 ~ time | tissue)$contrasts

这里只是 liver 的输出,这就是您要查找的组。

...
 tissue = liver:
 contrast    estimate         SE   df t.ratio p.value
 early      0.4481244 0.07902715 70.4   5.671  <.0001
 late       0.4618041 0.07902715 70.4   5.844  <.0001
 lvery late 0.3824247 0.09678810 70.4   3.951  0.0002

如果您知道需要组均值和组均值差值,您只需添加到通过contrastCoefficients 创建的对比系数矩阵即可。

custom.contrasts <- as.data.frame(contrastCoefficients(
    time ~ (day1+day2+day3)/3,
    time ~ (day4+day5+day6)/3,
    time ~ (day7+day8)/2,
    time ~ (day1+day2+day3)/3 - (day4+day5+day6)/3,
    time ~ (day1+day2+day3)/3 - (day7+day8)/2,
    time ~ (day4+day5+day6)/3 - (day7+day8)/2,
    data = model.data, normalize = FALSE))

然后命名并相应地制作 .lsmc 函数。

按照@aosmith 的例子:

custom.means <- as.data.frame(contrastCoefficients(
   time ~ (day1+day2+day3)/3,
   time ~ (day4+day5+day6)/3,
   time ~ (day7+day8)/2,
   data = model.data, normalize = FALSE))

colnames(custom.means) <- c("early",
  "late",
  "very late")

custom.means.lsmc <- function(...) return(custom.means)

tissue.means <- confint(lsmeans(tissue.model, custom.means ~ time | tissue)$contrasts)

library("ggplot2")
p <- ggplot(tissue.means, 
  aes(x = contrast, y = estimate, ymin = lower.CL, ymax = upper.CL)) +
  geom_errorbar() + facet_wrap(~ tissue, ncol = 4) + xlab("time")

print(p)