相関係数

MikuHatsune2013-06-04

相関係数にはPearson, Spearman, Kendallがあるらしい。

Usage:

     var(x, y = NULL, na.rm = FALSE, use)
     
     cov(x, y = NULL, use = "everything",
     
Arguments:

       x: a numeric vector, matrix or data frame.

       y:NULL(default) or a vector, matrix or data frame with
          compatible dimensions to ‘x’.  The default is equivalent to
          ‘y = x’ (but more efficient).

   na.rm: logical. Should missing values be removed?

     use: an optional character string giving a method for computing
          covariances in the presence of missing values.  This must be
          (an abbreviation of) one of the strings ‘"everything","all.obs","complete.obs","na.or.complete", or
          ‘"pairwise.complete.obs"’.

  method: a character string indicating which correlation coefficient
          (or covariance) is to be computed.  One of ‘"pearson"(default),"kendall", or ‘"spearman", can be
          abbreviated.

       V: symmetric numeric matrix, usually positive definite such as a
          covariance matrix.

以前ラブライブの総選挙結果をプロットしたが、第五回がすべて出揃ったので描き直す。

ケンダールの順位相関をやってみよう。

"高坂穂乃果"	"絢瀬絵里"	"南ことり"	"園田海未"	"星空凛"	"西木野真姫"	"東條希"	"小泉花陽"	"矢澤にこ"
1	8	9	2	4	6	3	7	5
4	9	2	5	6	8	3	7	1
1	7	2	3	8	6	4	9	5
6	9	1	5	8	3	7	4	2
1	5	4	2	8	6	9	7	3
7	6	2	3	9	8	5	1	4
3	2	4	5	7	1	8	9	6
3	2	6	5	4	1	9	8	7
3	2	8	7	5	1	9	4	6
3	2	9	7	4	1	8	6	5
data1 <- read.delim("clipboard")
cor1 <- cor(data1, method="kendall")

library(gplots)
cols <- greenred(100)
h0 <- heatmap.2(cor1, col=cols, scale="none", trace="none", dendrogram="row", key=TRUE)
h1 <- h0$carpet # 列は並び替える
layout(matrix(c(1, rep(2, 4)), nc=1)) # プロット領域を適当に設定する
par(mar = c(0.2, 2, 0.5, 8))
plot(h0$colDendrogram, horiz=FALSE, axes=FALSE, xaxs="i", leaflab="none", edgePar=list(col=1)) # デンドログラムを描く
par(mar = c(8, 2, 0, 8))
image(seq(nrow(h1)), seq(ncol(h1)), h1, col=cols, xlab="", ylab="", axes=FALSE) # heatmapを描く
axis(1, at=seq(colnames(h1)), label=colnames(h1), tick=0, las=2, line=-0.8, cex.axis=1.5)
axis(4, at=seq(colnames(h1)), label=colnames(h1), tick=0, las=2, line=-0.8, cex.axis=1.5)
par(mar = c(10, 0.5, 0, 1))
panel <- t(as.matrix(unique(c(h0$colorTable$low, h0$colorTable$low))))
image(y=c(panel), z=panel, col=cols, axes=FALSE, ylab="") # カラースケールを描く
axis(4, las=2, tick=0, line=-0.8)

まきちゃんとえりちーの相関が強い。彼女らはデビュー当初はあまり人気がなかったが、最近になって人気急上昇している。
副会長とにこにーの相関が強い。しかし彼女らは、デビュー当初に人気があったけど最近低迷している。
3年生どうしてこうなった…