2017-07-25 3 views
2

私はcor()関数でPearson相関を実行しようとしていますが、出力では係数1ではなく係数-1だけが出力されます。ですから、私がcorrplot()を使って行列をプロットすると、それらの1と-1の値だけが表示されます。これをどうやって解決するのですか? 私のデータセットはhere見つけることができ、ダウン下の私のスクリプトを参照してください。Rのcor()関数から1と-1しか得られないのはなぜですか?

##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) 

答えて

3

:3以上のあなたがより多くのバリエーションを持つことになりますので、

。 2つの観測値からなる任意の2つの変数間の相関関係は、常に-1または1です。自分自身で確認するには、replicate(1e2, cor(rnorm(2), rnorm(2)))を実行して、2つの観測値からなる2つの変数間の100の相関を計算してみてください。結果は常に-1または1です。

+0

ありがとう、私の統計情報の知識の欠如は私の最高を得ました。 – Haley

2

あなたが列で2つしか観測を持っているためです。

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 

あなたはPearson correlation formulaを確認し、2つだけの値と異なる出力を期待することはできません。あなたのデータセットは、変数あたりわずか2の観測が含まれてい

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 
関連する問題