我认为你需要的是cor.test()
函数,除了相关性标准误差之外,它将返回你所需的所有内容。但是,正如你所看到的,计算标准误差的公式非常简单,如果你使用cor.test
,你就有了计算所需的所有输入。
使用示例数据(这样你可以自己与第14.6页上的结果进行比较):
> cor.test(mydf$X, mydf$Y)
Pearson's product-moment correlation
data: mydf$X and mydf$Y
t = -5.0867, df = 10, p-value = 0.0004731
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.9568189 -0.5371871
sample estimates:
cor
-0.8492663
cor.test.plus <- function(x) {
list(x,
Standard.Error = unname(sqrt((1 - x$estimate^2)/x$parameter)))
}
然后按以下方式使用:
cor.test.plus(cor.test(mydf$X, mydf$Y))
这里,“mydf”被定义为:
mydf <- structure(list(Neighborhood = c("Fair Oaks", "Strandwood", "Walnut Acres",
"Discov. Bay", "Belshaw", "Kennedy", "Cassell", "Miner", "Sedgewick",
"Sakamoto", "Toyon", "Lietz"), X = c(50L, 11L, 2L, 19L, 26L,
73L, 81L, 51L, 11L, 2L, 19L, 25L), Y = c(22.1, 35.9, 57.9, 22.2,
42.4, 5.8, 3.6, 21.4, 55.2, 33.3, 32.4, 38.4)), .Names = c("Neighborhood",
"X", "Y"), class = "data.frame", row.names = c(NA, -12L))
您不能简单地从返回值中获取检验统计量吗?当然,检验统计量是估计值/标准误差(estimate/se),因此您可以通过将估计值除以tstat来计算se:
在上面的答案中使用mydf
:
r = cor.test(mydf$X, mydf$Y)
tstat = r$statistic
estimate = r$estimate
estimate; tstat
cor
-0.8492663
t
-5.086732
?cor.test
代替。 - A5C1D2H2I1M1N2O1R2T1