我有一个简单的问题。我已经在 R 中看到了 t 检验和相关性的这种行为。
我做了一个简单的配对 t 检验(在这种情况下,两个长度为 100 的向量)。所以配对 t-test 的 df 应该是 99。但是这不是出现在 t-test 结果输出中的。
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
关于r - 为什么 R 的 t 检验函数存在错误和/或不一致的自由度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38922802/