[英]Why am I only getting 1 and -1 from the cor() function in R?
I'm trying to perform a Pearson correlation with the cor() function, but the output only gives me 1 and -1, not the coefficient itself. 我正在尝试使用cor()函数执行Pearson相关,但是输出仅给出1和-1,而不给出系数本身。 So when I go to plot the matrix with corrplot(), I only see those 1 and -1 values.
因此,当我使用corrplot()绘制矩阵时,我只看到那些1和-1值。 How do I fix this?
我该如何解决? My dataset can be found here , and see my script down below:
我的数据集可以在此处找到,并在下面查看我的脚本:
##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)
Your dataset contains only 2 observations per variable. 您的数据集每个变量仅包含2个观察值。 The correlation between any two variables consisting of only two observations is always -1 or 1. To see for yourself, try running
replicate(1e2, cor(rnorm(2), rnorm(2)))
which calculates 100 correlations between two variables consisting of two observations. 仅由两个观察值组成的任意两个变量之间的相关性始终为-1或1。要亲自查看,请尝试运行
replicate(1e2, cor(rnorm(2), rnorm(2)))
,该操作将计算两个变量之间的100个相关性两个观察结果。 The result is always -1 or 1. 结果始终为-1或1。
It's because you have only two observations by column. 这是因为按列您只有两个观察值。
test <- data.frame(a=c(1,2),b=c(2,3),c=c(4,-2))
cor(test, use = "everything", method = "pearson")
a b c
a 1 1 -1
b 1 1 -1
c -1 -1 1
You can't expect a different output with only two values, check Pearson correlation formula . 您不能期望只有两个值的不同输出,请检查Pearson相关公式 。
Since three or more you will have more variation: 由于三个或更多,您将拥有更多的变化:
test <- data.frame(a=c(1,2,3),b=c(2,3,5),c=c(4,-2,-10))
cor(test, use = "everything", method = "pearson")
a b c
a 1.0000000 0.9819805 -0.9966159
b 0.9819805 1.0000000 -0.9941916
c -0.9966159 -0.9941916 1.0000000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.