mtcars 数据集中的 cor 值是否相等?
Equal cor values in mtcars dataset?
当我运行 R 中的以下代码时:
print(cor(mtcars[,c(1,5)]),method="kendall")
我总是得到与 :
相同的结果
mpg drat
mpg 1.0000000 0.6811719
drat 0.6811719 1.0000000
无论我是否设置:
method="kendall" or "spearman" or "pearson"
所有三个相关性测试都为 R 中的 mtcars 数据集提供相同的相关系数是巧合还是我做错了什么?
那是因为您对 print
使用了 method
参数,而不是 cor
(即打字错误):
cor(mtcars[, c(1, 5)], method = "pearson")[1, 2]
# [1] 0.6811719
cor(mtcars[, c(1, 5)], method = "kendall")[1, 2]
# [1] 0.4645488
cor(mtcars[, c(1, 5)], method = "spearman")[1, 2]
# [1] 0.6514555
和
print(cor(mtcars[, c(1, 5)], method = "kendall"))
# mpg drat
# mpg 1.0000000 0.4645488
# drat 0.4645488 1.0000000
当我运行 R 中的以下代码时:
print(cor(mtcars[,c(1,5)]),method="kendall")
我总是得到与 :
相同的结果 mpg drat
mpg 1.0000000 0.6811719
drat 0.6811719 1.0000000
无论我是否设置:
method="kendall" or "spearman" or "pearson"
所有三个相关性测试都为 R 中的 mtcars 数据集提供相同的相关系数是巧合还是我做错了什么?
那是因为您对 print
使用了 method
参数,而不是 cor
(即打字错误):
cor(mtcars[, c(1, 5)], method = "pearson")[1, 2]
# [1] 0.6811719
cor(mtcars[, c(1, 5)], method = "kendall")[1, 2]
# [1] 0.4645488
cor(mtcars[, c(1, 5)], method = "spearman")[1, 2]
# [1] 0.6514555
和
print(cor(mtcars[, c(1, 5)], method = "kendall"))
# mpg drat
# mpg 1.0000000 0.4645488
# drat 0.4645488 1.0000000