9

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

我做了一个简单的配对 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)

4

1 回答 1

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 

在设置选项(数字= 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
于 2017-09-17T04:46:39.303 回答