为什么 R 的 t 检验函数存在错误 and/or 不一致的自由度?

Why are there wrong and/or inconsistent degrees of freedom from R's t-test function?

我有一个简单的问题。我已经在 R 中看到了 t 检验和相关性的这种行为。

我做了一个简单的配对 t 检验(在本例中,两个长度为 100 的向量)。所以配对 t 检验的 df 应该是 99。但这不是 t 检验结果输出中出现的内容。

dataforTtest.x <- rnorm(100,3,1)
dataforTtest.y <- rnorm(100,1,1)
t.test(dataforTtest.x, dataforTtest.y,paired=TRUE)

这个输出是:

Paired t-test

data:  dataforTtest.x and dataforTtest.y
t = 10, df = 100, p-value <2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
1.6 2.1
sample estimates:
mean of the differences 
                1.8 

但是,如果我实际查看生成的对象,df 是正确的。

> t.test(dataforTtest.x, dataforTtest.y,paired=TRUE)[["parameter"]]

df 
99 

我是不是漏掉了一些非常愚蠢的东西? 我是 运行 R 版本 3.3.0 (2016-05-03)

如果舍入数字的全局设置在 R 中发生变化,则可能会发生此问题,这可以通过类似 options(digits=2) 的方式来完成。

在更改此设置之前注意 t 检验的结果:

    Paired t-test

data:  dataforTtest.x and dataforTtest.y
t = 13.916, df = 99, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 1.700244 2.265718
sample estimates:
mean of the differences 
               1.982981 

设置选项后(位数=2):

Paired t-test

data:  dataforTtest.x and dataforTtest.y
t = 13.916, df = 100, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 1.700244 2.265718
sample estimates:
mean of the differences 
                      2 

在 R 中,出于这个原因更改全局设置可能很危险。它可以在用户不知情的情况下完全改变统计分析的结果。相反,我们可以直接在数字上使用 round() 函数,或者对于像这样的测试结果,我们可以将它与 broom 包结合使用。

round(2.949,2)
[1] 2.95

#and

require(broom)

glance(t.test(dataforTtest.x, dataforTtest.y,paired=TRUE))

estimate statistic      p.value parameter cnf.low cnf.high       method alternative
1.831433  11.31853 1.494257e-19        99 1.51037 2.152496 Paired t-test  two.sided