我正在尝试使用cor()函数执行Pearson相关,但是输出仅给出1和-1,而不给出系数本身。因此,当我使用corrplot()绘制矩阵时,我只会看到那些1和-1值。我该如何解决?
可以在here中找到我的数据集,并在下面查看我的脚本:

##Must load the libraries we will need! IF you have not installed the packages, do that before you start.
library("corrplot")
##Load in your datasets
D1=BPT5test
##if you don't have a Y (i.e, you want the same thing to be in both axis), leave this blank
D2=
##Run the spearman correlation. If you want to do a Pearson, change "spearman to "pearson"
##If you have 0s in your dataset, set use = "complete.obs", if you have no 0s, set use = "everything"
CorTest=cor(D1, use = "everything", method = "pearson")
##Let's get to plotting!
##Lots of changing you can do!
#Method can be "circle" "square" "pie" "color"
#ColorRampPalette can be changed, "blue" being the negative, "White" being '0', and "red" being the positive
#Change the title to whatever you want it to be
#tl.col is the color of your labels, this can be set to anything.. default is red
CorGraph=corrplot(CorTest, method = "circle", col = colorRampPalette(c("blue","white","red"))(200), title = "Pearson's Correlation of High-Fat Sugar at 8 weeks", tl.cex = .5, tl.col = "Black",diag = TRUE, cl.ratio = 0.2)

最佳答案

您的数据集每个变量仅包含2个观察值。仅由两个观察值组成的任何两个变量之间的相关性始终为-1或1。要亲自观察,请尝试运行replicate(1e2, cor(rnorm(2), rnorm(2))),它计算由两个观察值组成的两个变量之间的100相关性。结果始终为-1或1。

关于r - 为什么我只从R中的cor()函数得到1和-1?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45312739/

10-12 19:21