Dunnett 在 R 中的测试每次返回不同的值

机器算法验证 r 多重比较
2022-03-10 04:44:49

我正在使用 R 'multcomp' 库 ( http://cran.r-project.org/web/packages/multcomp/ ) 来计算 Dunnett 的测试。我正在使用下面的脚本:

Group <- factor(c("A","A","B","B","B","C","C","C","D","D","D","E","E","F","F","F"))
Value <- c(5,5.09901951359278,4.69041575982343,4.58257569495584,4.79583152331272,5,5.09901951359278,4.24264068711928,5.09901951359278,5.19615242270663,4.58257569495584,6.16441400296898,6.85565460040104,7.68114574786861,7.07106781186548,6.48074069840786)
data <- data.frame(Group, Value)
aov <- aov(Value ~ Group, data)
summary(glht(aov, linfct=mcp(Group="Dunnett")))

现在,如果我通过 R 控制台多次运行此脚本,我每次都会得到非常不同的结果。这是一个例子:

         Simultaneous Tests for General Linear Hypotheses

Multiple Comparisons of Means: Dunnett Contrasts


Fit: aov(formula = Value ~ Group, data = data)

Linear Hypotheses:
           Estimate Std. Error t value Pr(>|t|)   
B - A == 0 -0.35990    0.37009  -0.972  0.76545   
C - A == 0 -0.26896    0.37009  -0.727  0.90019   
D - A == 0 -0.09026    0.37009  -0.244  0.99894   
E - A == 0  1.46052    0.40541   3.603  0.01710 * 
F - A == 0  2.02814    0.37009   5.480  0.00104 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Adjusted p values reported -- single-step method)

这是另一个:

         Simultaneous Tests for General Linear Hypotheses

Multiple Comparisons of Means: Dunnett Contrasts


Fit: aov(formula = Value ~ Group, data = data)

Linear Hypotheses:
           Estimate Std. Error t value Pr(>|t|)    
B - A == 0 -0.35990    0.37009  -0.972   0.7654    
C - A == 0 -0.26896    0.37009  -0.727   0.9001    
D - A == 0 -0.09026    0.37009  -0.244   0.9989    
E - A == 0  1.46052    0.40541   3.603   0.0173 *  
F - A == 0  2.02814    0.37009   5.480   <0.001 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Adjusted p values reported -- single-step method)

如您所见,上述两个结果差异很小,但足以将最后一组(F)从两颗星移到三颗星,这让我感到担忧。

我对此有几个问题:

  1. 为什么会这样?!当然,如果您每次输入相同的数据,您应该得到相同的数据。
  2. 在 Dunnett 的计算中是否使用了某种随机数?
  3. 每次这种细微的变化真的是个问题吗?
2个回答

您是对的,其中涉及随机数生成,并且它使计算因运行而异。罪魁祸首其实不是 Dunnett 程序,而是单步调整所需的多元 t 分布。

以下代码显示了一个计算示例P(X<0)带有 5 维向量X具有多元T5具有可交换相关性的分布:

> library(mvtnorm)
> cr2 <- matrix(rep(0.3, 25), nr=5); diag(cr2) <- 1
> cr2
     [,1] [,2] [,3] [,4] [,5]
[1,]  1.0  0.3  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3  0.3
[3,]  0.3  0.3  1.0  0.3  0.3
[4,]  0.3  0.3  0.3  1.0  0.3
[5,]  0.3  0.3  0.3  0.3  1.0
> b <- pmvt(lower=rep(-Inf,5), upper=rep(0,5), delta=rep(0,5), df=5, corr=cr2)
> a <- pmvt(lower=rep(-Inf,5), upper=rep(0,5), delta=rep(0,5), df=5, corr=cr2)
> all.equal(a,b)
[1] "Attributes: < Component 1: Mean relative difference: 0.1527122 >"
[2] "Mean relative difference: 0.0003698006"     

如果这值得关注,只需set.seed在计算之前调用任何参数以使其完全可重现。

顺便说一句,对输出中的错误进行了确认和量化glht

> ss <- summary(glht(aov, linfct=mcp(Group="Dunnett")))
> attr(ss$test$pvalues, "error")
[1] 0.0006597562

我通过示例一起回答您的前两个问题。

library(multcomp)

Group <- factor(c("A","A","B","B","B","C","C","C","D","D","D","E","E","F","F","F"))
Value <- c(5,5.09901951359278,4.69041575982343,4.58257569495584,4.79583152331272,5,5.09901951359278,4.24264068711928,5.09901951359278,5.19615242270663,4.58257569495584,6.16441400296898,6.85565460040104,7.68114574786861,7.07106781186548,6.48074069840786)
data <- data.frame(Group, Value)

fit <- aov(Value ~ Group, data)

set.seed(20140123)
Dunnet <- glht(fit, linfct=mcp(Group="Dunnett"))
summary(Dunnet)

结果:

     Simultaneous Tests for General Linear Hypotheses

Multiple Comparisons of Means: Dunnett Contrasts


Fit: aov(formula = Value ~ Group, data = data)

Linear Hypotheses:
           Estimate Std. Error t value Pr(>|t|)   
B - A == 0 -0.35990    0.37009  -0.972  0.76536   
C - A == 0 -0.26896    0.37009  -0.727  0.90012   
D - A == 0 -0.09026    0.37009  -0.244  0.99895   
E - A == 0  1.46052    0.40541   3.603  0.01794 * 
F - A == 0  2.02814    0.37009   5.480  0.00112 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Adjusted p values reported -- single-step method)

再次运行(不设置种子):

summary(Dunnet)

不同的结果:

     Simultaneous Tests for General Linear Hypotheses

Multiple Comparisons of Means: Dunnett Contrasts


Fit: aov(formula = Value ~ Group, data = data)

Linear Hypotheses:
           Estimate Std. Error t value Pr(>|t|)   
B - A == 0 -0.35990    0.37009  -0.972  0.76535   
C - A == 0 -0.26896    0.37009  -0.727  0.90020   
D - A == 0 -0.09026    0.37009  -0.244  0.99895   
E - A == 0  1.46052    0.40541   3.603  0.01767 * 
F - A == 0  2.02814    0.37009   5.480  0.00105 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Adjusted p values reported -- single-step method)

再次运行(使用固定的种子):

set.seed(20140123)
Dunnet <- glht(fit, linfct=mcp(Group="Dunnett"))
summary(Dunnet)

结果相同:

     Simultaneous Tests for General Linear Hypotheses

Multiple Comparisons of Means: Dunnett Contrasts


Fit: aov(formula = Value ~ Group, data = data)

Linear Hypotheses:
           Estimate Std. Error t value Pr(>|t|)   
B - A == 0 -0.35990    0.37009  -0.972  0.76536   
C - A == 0 -0.26896    0.37009  -0.727  0.90012   
D - A == 0 -0.09026    0.37009  -0.244  0.99895   
E - A == 0  1.46052    0.40541   3.603  0.01794 * 
F - A == 0  2.02814    0.37009   5.480  0.00112 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Adjusted p values reported -- single-step method)

通过在每次运行之前设置种子,您可以获得一致的结果。因此,似乎在计算 p 值时使用了随机数。

我认为这种微小的变化是一个问题吗?我不是很喜欢它,但我会忍受它。使用固定种子将使您的结果具有可重复性。我建议不要根据旁边有多少颗星来考虑 p 值——而是选择一个alpha这对你来说是有意义和有用的。我尽量不被小数点后 5 或 6 位发生的事情所困扰,除非我正在从事的项目确实需要这种精确度。在这种情况下,我认为大多数人都会同意,即使 p 值计算略有变化,结果的解释也是一样的。