由于我已经遇到了类似的问题here,所以我遇到了另一个问题:stats::t.test
返回了p-value < 2.2e-16
。但是从数学中我得到p_value <- 2 * pt(-abs(t), df) # [1] 1.929352e-17
。怎么了?
我刚刚按照那里的建议开始调试:
debug(stats:::t.test.default)
vals <- data.frame(a = c(4, 2, 4, 7, 3, 4, 8, 8, 3, 0, 1, 5, 4, 6, 4, 8, 7,
9, 6, 6, 3, 6, 7, 4),
b = c(5, 7, 6, 13, 12, 6, 14, 16, 4, 2, 7, 7, 4, 8, 9, 9,
11, 13, 12, 8, 3, 8, 7, 7))
stats::t.test(x = vals)
# One Sample t-test
# data: vals
# t = 13.214, df = 47, p-value < 2.2e-16
然后逐步执行,直到到达第98行,
其中pval
被评估为1.929352e-17
,这是我所期望的。
直到行112设置了类class(rval) <- "htest"
为止,它一直保持不变。
怎么了?类会改变值吗?不幸的是,我不知道如何理解/调试第112行后面的代码。
答案 0 :(得分:4)
No. Classes don't change values, but they do change how they are printed. Since the class that's being returned is a htest
object, the stats:::print.htest()
is used to draw the result. To make things prettyier, this function formats numbers so they have a reasonable number of decimal places. It uses the format.pval
function to make p-values look pretty. After p-values get really small it doesn't really make sense to show all the digits so R just tells you at some point it's less than a certian value. For example
format.pval(1e-20)
# [1] "< 2.22e-16
The "real" value is still stored in the object
x <- stats::t.test(x = vals)
x$p.value
# [1] 1.929352e-17
This is a very common pattern in R where just print()
ing a value doesn't necessarily show what's "really" there; it's just a pretty display of that object.