Emmeans-控制与治疗有多个因素

时间:2020-03-30 12:09:53

标签: r p-value emmeans

我正在使用与对照组进行自定义比较。如果我只想比较一个因素,trt.vs.ctrl方法对我来说是完美的,但是当我将比较设置为更复杂时(例如,对照组由一个特定的人来描述),然后失败(或失败) 2个以上变量的组合)。

下面的示例代码。假设使用pigs数据,我想将所有饮食与低百分比的鱼类饮食进行比较。请注意,在nd数据框中,“ fish”仅与9%关联。但是,当我运行emmeans时,该功能未在嵌套中显示,并且在控件正确的同时,处理组还包括各种值的鱼和百分数。这意味着调整是错误的。

所以我可以想到两种方法:

  1. 在这种情况下,如何使emmeans嵌套,或者
  2. 如何手动进行dunnettx调整(=我可以使用“无”调整,然后退出我真正想要的测试,然后自行调整p值?)。
    library(emmeans)
    library(dplyr)

    pigs.lm <- lm(log(conc) ~ source + factor(percent), data = pigs)
    nd <- expand.grid(source = levels(pigs$source), percent = unique(pigs$percent)) %>%
        filter(percent == 9 | source != "fish")

    emmeans(pigs.lm, trt.vs.ctrl ~ source + percent, 
        data = nd, covnest = TRUE, cov.reduce = FALSE)

感谢您的帮助。

使用include的建议效果很好。在以后发布我的代码,以防将来其他人遇到同样的问题。

library(emmeans)
library(dplyr)
library(tidyr)

pigs.lm <- lm(log(conc) ~ source + factor(percent), data = pigs)
nd <- expand.grid(source = levels(pigs$source), percent =     unique(pigs$percent)) %>%
    filter(percent == 9 | source != "fish")

ems <- emmeans(pigs.lm, trt.vs.ctrl ~ source + percent, 
    data = nd, covnest = TRUE, cov.reduce = FALSE)

# to identify which levels to exclude - in this case, 
# I only want the low-percent fish to remain as the ref level
aux <- as.data.frame(ems[[1]]) %>%
    mutate(ID = 1:n()) %>%
    filter(!grepl("fish", source) | ID == 1)

emmeans(pigs.lm, trt.vs.ctrl ~ source + percent, 
    data = nd, covnest = TRUE, cov.reduce = FALSE, include = aux$ID)

1 个答案:

答案 0 :(得分:0)

对于您要完成的任务,我还不太清楚,但我认为不过滤数据是解决方案。

如果您的目标是将source的边际均值与(鱼,占9%)组合进行比较,则可以通过构造两组Emmeans,然后进行子集和组合来实现:

emm1 = emmeans(pigs.lm, "source")
emm2 = emmeans(pigs.lm, ~source*percent)
emm3 = emm2[1] + emm1      # or rbind(emm2[1], emm1)

那你得到

> confint(emm3, adjust ="none")
 source percent emmean     SE df lower.CL upper.CL
 fish   9         3.22 0.0536 23     3.11     3.33
 fish   .         3.39 0.0367 23     3.32     3.47
 soy    .         3.67 0.0374 23     3.59     3.74
 skim   .         3.80 0.0394 23     3.72     3.88

Results are averaged over some or all of the levels of: percent 
Results are given on the log (not the response) scale. 
Confidence level used: 0.95 

> contrast(emm3, "trt.vs.ctrl1")
 contrast        estimate     SE df t.ratio p.value
 fish,. - fish,9    0.174 0.0366 23 4.761   0.0002 
 soy,. - fish,9     0.447 0.0678 23 6.595   <.0001 
 skim,. - fish,9    0.576 0.0696 23 8.286   <.0001 

Results are averaged over some or all of the levels of: percent 
Results are given on the log (not the response) scale. 
P value adjustment: dunnettx method for 3 tests 

另一种做同一件事的方法(更乏味,更容易出错)是获取因子组合的EMM,然后使用自定义对比:

> contrast(emm2, list(con1 = c(-3,0,0, 1,0,0, 1,0,0, 1,0,0)/4,
+                     con2 = c(-4,1,0, 0,1,0, 0,1,0, 0,1,0)/4,
+                     con3 = c(-4,0,1, 0,0,1, 0,0,1, 0,0,1)/4),
+          adjust = "mvt")

 contrast estimate     SE df t.ratio p.value
 con1        0.174 0.0366 23  4.761  0.0002 
 con2        0.447 0.0678 23  6.595  <.0001 
 con3        0.576 0.0696 23  8.286  <.0001 

Results are given on the log (not the response) scale. 
P value adjustment: mvt method for 3 tests 

(mvt调整是对dunnettx的一种精确修正,它只是一个近似值。它不默认为mvt,因为它在大量测试中计算量很大。)

为回答问题的最后一部分,您可以使用exclude(或include)来关注关卡的一部分;参见? pairwise.emmc