LoginSignup
1
5

More than 1 year has passed since last update.

【R】相関係数を求める方法

Last updated at Posted at 2019-02-02

以前に書いた【R】散布図を描く方法では、2つのデータ(二変量)の相関関係を図示しましたが、今回は2つのデータの相関係数を求めたいと思います。

解析に使うデータ

  • 以下のデータは、ある山の東側(east)と西側(west)の様々な標高で測定した樹高(m)です。
    • direction列:斜面の方角(east/west)
    • altitude列:測定地点の標高
    • tree_height列:測定された樹高
tree_height.csv
direction,altitude,tree_height
east,260,12.1
east,320,12.3
east,390,8.2
east,430,9.8
east,470,7.9
east,500,6
west,280,15.3
west,330,11.9
west,380,11.2
west,410,9.3
west,440,7.9
west,500,6.8

相関係数を求める

  • 相関係数の算出には、cor関数、もしくはcor.test関数を使います。
  • 相関係数を算出する手法については、methodパラメーターで指定します。
    • ピアソンの積率相関係数:method="pearson"
    • スピアマンの順位相関係数:method="spearman"
    • ケンドールの順位相関係数:method="kendall"

ピアソンの積率相関係数の算出

相関係数の算出+無相関検定

  • 下記の結果を見ると、相関係数(cor)が-0.915107となっているので、標高と樹高との間に「強い負の相関」があることが分かります。
    • つまり、「標高が上がるほど、樹高が低下する」ということ。
  • 相関係数の値は、一般的には以下のような区分がされているようです。
    • 0.0~±0.2:ほとんど相関がない
    • ±0.2~±0.4:弱い相関がある
    • ±0.4~±0.7:相関がある
    • ±0.7~±0.9:強い相関がある
    • ±0.9~±1.0:非常に強い相関がある
  • p値(3.007e-05)は無相関検定の結果なので、標高と樹高との間に有意な相関関係があることが分かります。
Rのコンソール
> tree_height <- read.table("C:\\tree_height.csv", header=TRUE, encoding="CP932", sep=",")
> cor.test(tree_height$altitude, tree_height$tree_height, method="pearson")

        Pearson's product-moment correlation

data:  tree_height$altitude and tree_height$tree_height
t = -7.1769, df = 10, p-value = 3.007e-05
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.9762830 -0.7186016
sample estimates:
      cor 
-0.915107 

相関係数の算出のみ

  • 相関係数しか必要としない場合は、cor関数を使うと相関係数のみのシンプルな結果を得られます。
Rのコンソール
> cor(tree_height$altitude, tree_height$tree_height, method="pearson")
[1] -0.915107
1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5