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

eli*_*isa 9 r rounding t-test

我有一个简单的问题.我已经在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)
Run Code Online (Sandbox Code Playgroud)

这个输出是:

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 
Run Code Online (Sandbox Code Playgroud)

但是,如果我实际查看结果对象,则df是正确的.

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

df 
99 
Run Code Online (Sandbox Code Playgroud)

我错过了一些非常愚蠢的东西吗?我正在运行R版本3.3.0(2016-05-03)

www*_*www 2

如果 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 
Run Code Online (Sandbox Code Playgroud)

设置选项后(数字=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 
Run Code Online (Sandbox Code Playgroud)

在 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
Run Code Online (Sandbox Code Playgroud)