【发布时间】:2018-10-05 03:41:19
【问题描述】:
我想在 emmeans 中创建一个自定义对比函数,它可以从输入向量中删除给定的级别列表,并在剩余级别上应用内置对比方法(“trt.vs.ctrl”)。 here 提供了一个示例数据集。我正在使用以下 R 代码来计算方差分析和事后比较:
options(contrasts=c("contr.sum", "contr.poly"))
my_lm <- lm(D1 ~ C*R, data=df)
Anova(my_lm, type = "III")
#show Interaction effects using emmeans
emmip(my_lm, C ~ R )
emm = emmeans(my_lm, ~ C * R)
emm
contrast(emmeans(my_lm, ~ C * R), "consec", by = "C")
#compare 1st with next 3 groups (how to remove other three levels?)
contrast(emmeans(my_lm, ~ C * R), "trt.vs.ctrl", by = "R")
内置的对比度选项(“trt.vs.ctrl”)将第一个级别与其后面的所有内容进行比较(C 中有 7 个因子级别,我想删除其中的最后 3 个并计算对比剩下的4)。 An example is provided in the official documentation写自定义对比函数。
skip_comp.emmc <- function(levels, skip = 1, reverse = FALSE) {
if((k <- length(levels)) < skip + 1)
stop("Need at least ", skip + 1, " levels")
coef <- data.frame()
coef <- as.data.frame(lapply(seq_len(k - skip - 1), function(i) {
sgn <- ifelse(reverse, -1, 1)
sgn * c(rep(0, i - 1), 1, rep(0, skip), -1, rep(0, k - i - skip - 1))
}))
names(coef) <- sapply(coef, function(x)
paste(which(x == 1), "-", which(x == -1)))
attr(coef, "adjust") = "fdr" # default adjustment method
coef
}
但是由于我的理解有限,我不太确定在哪里应用我需要自定义示例的修改。有什么想法吗?
【问题讨论】: