16. 상관분석

Statistics
교재 9장
Author

Kim Jae Sook

Published

April 23, 2024

두개의 변량에 대해 서로 상관되는 인자항목들이 어떤 관련성이 있고, 그 관련성이 어느 정도인지를 수치적으로 분석하는 것을 상관분석(correlation analysis)이라 하며, 실제 관측된 값과 모형의 결과를 서로 비교할 때 상관분석을 통하여 모형의 적합성 정도를 표현하기도 한다. 상관계수는 pearson, spearman, kendall의 3가지 상관계수가 있으며, \(-1 \sim 1\)의 값을 가지는데 두 변수가 음의 상관성을 가질수록 \(-1\), 양의 상관성을 가질수록 \(1\), 상관성을 가지지 않을수록 \(0\)과 가깝게 나타난다.

상관분석: 모수적 상관분석

Pearson 상관분석

  • 두 변수가 모두 수치형 변수인 경우 일반적으로 pearson의 상관계수를 계산한다.
  • pearson 상관계수는 대표본이거나 각 변수의 모집단분포가 정규분포인 경우, \((x_i ,\ y_i)\)와 같은 순서쌍으로 주어진다. 모집단의 상관계수는 모수로서 일반적으로 알려져 있지 않다.
  • 모집단으로부터 랜덤하게 \(n\)개를 표본으로 뽑은 경우, \((x_1 ,\ y_1)\), \(\cdots\) , \((x_n ,\ y_n)\)의 표본의 순서쌍에서 표본상관계수를 계산할 수 있다.(\(S_{x}\): \(X\)의 표준편차, \(S_{y}\): \(Y\)의 표준편차, \(S_{xy}\): \(X\)\(Y\)의 공분산)

\[ r=\dfrac{S_{xy}}{S_{x}S_{y}} \]

  • 표본상관계수 \(r\)을 가지고 모상관계수 \(\rho\)에 대한 가설검정을 할 수 있다.

  • 귀무가설 \(~ ~H_0 : \rho=0\) (모집단에서는 두 변수 간에 상관관계가 없다.)

  • 대립가설

\(~ ~ ~ ~ ~ ~ ~H_1 : \rho\neq0\) (모집단에서는 두 변수 간에 상관관계가 있다.)[양측검정]

\(~ ~ ~ ~ ~ ~ ~H_1 : \rho>0\) (모집단에서는 두 변수 간에 양의 상관관계가 있다.)[단측검정]

\(~ ~ ~ ~ ~ ~ ~H_1 : \rho<0\) (모집단에서는 두 변수 간에 음의 상관관계가 있다.)[단측검정]

  • 가설검정을 위한 검정통계량은 다음과 같다.

\[ T=\dfrac{r}{\sqrt{\dfrac{1-r^2}{n-2}}} \sim t(n-2) \]

  • 자유도가 \(n-2\)\(t\)분포를 따르므로 표본상관계수에 대해서 \(t\)검정을 한다.

  • \(t\)분포를 이용하여 유의확률 \(p\)를 계산했을 대 유의수준 \(\alpha\)보다 작으면 귀무가설을 기각한다.

상관분석: 비모수적 상관분석

Spearman의 순위상관계수

  • 비모수적 상관계수이다.

  • 소표본이며 변수의 분포가 정규분포라고 할 수 없을 때, 각 변수의 순위를 가지고 순위에 대한 pearson 상관계수를 구하는 공식으로 계산한다.

  • 유의성 검정은 pearson의 상관과는 달리 비모수적 검정에 의해 실시한다.

Kendall’s Tau(켄달의 순위상관계수)

  • spearman 순위상관계수처럼 두 변수 사이의 연관성을 나타내는 비모수적 방법이다.

  • \((x_1 ,\ y_1)\), \(\cdots\) , \((x_n ,\ y_n)\)의 순서쌍에서 모든 \(i<j\)에 대해 \(P=(x_i - x_j)(y_i - y_j)>0\)인 쌍의 개수, \(Q=(x_i - x_j)(y_i - y_j)<0\)인 쌍의 개수일 때, 켄달의 타우는 다음 식과 같다.

\[ Kendall's \ \tau =\dfrac{P-Q}{P+Q} \]

[R기초] 실습하기

선형적 자료에 대한 상관분석

## correlation analaysis

## 1. linear data 
x=c(1:50) ; x
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
set.seed(1)  # 초기치 지정하여 동일한 값 유지
y = x + rnorm(50) ; y
 [1]  0.3735462  2.1836433  2.1643714  5.5952808  5.3295078  5.1795316
 [7]  7.4874291  8.7383247  9.5757814  9.6946116 12.5117812 12.3898432
[13] 12.3787594 11.7853001 16.1249309 15.9550664 16.9838097 18.9438362
[19] 19.8212212 20.5939013 21.9189774 22.7821363 23.0745650 22.0106483
[25] 25.6198257 25.9438713 26.8442045 26.5292476 28.5218499 30.4179416
[31] 32.3586796 31.8972123 33.3876716 33.9461950 33.6229404 35.5850054
[37] 36.6057100 37.9406866 40.1000254 40.7631757 40.8354764 41.7466383
[43] 43.6969634 44.5566632 44.3112443 45.2925048 47.3645820 48.7685329
[49] 48.8876538 50.8811077
plot(x, y)

###1) pearson correlation coefficient
cor(x, y)  #상관계수 구하기
[1] 0.9983757
## significant test(유의성 검정)
cor.test(x, y)  # HO: x and y are independent, 검정결과 귀무가설 기각

    Pearson's product-moment correlation

data:  x and y
t = 121.41, df = 48, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.9971245 0.9990827
sample estimates:
      cor 
0.9983757 
###2) spearman correlation coefficient
cor(x, y, method = 'spearman')  #상관계수 구하기
[1] 0.9977911
cor.test(x, y, method = 'spearman')  #유의성 검정

    Spearman's rank correlation rho

data:  x and y
S = 46, p-value < 2.2e-16
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho 
0.9977911 
###3) kendall correlation coefficient
cor(x, y, method = 'kendall')  #상관계수 구하기
[1] 0.9722449
cor.test(x,y, method = 'kendall')  #유의성 검정

    Kendall's rank correlation tau

data:  x and y
z = 9.9625, p-value < 2.2e-16
alternative hypothesis: true tau is not equal to 0
sample estimates:
      tau 
0.9722449 

비선형적 자료에 대한 상관분석

##2 non-linear data
x = c(1:50)
y = x**2  # x를 제곱함

x
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
y
 [1]    1    4    9   16   25   36   49   64   81  100  121  144  169  196  225
[16]  256  289  324  361  400  441  484  529  576  625  676  729  784  841  900
[31]  961 1024 1089 1156 1225 1296 1369 1444 1521 1600 1681 1764 1849 1936 2025
[46] 2116 2209 2304 2401 2500
 plot(x,y)

cor.test(x,y)  # 검정결과 귀무가설 기각, 즉 독립이 아님

    Pearson's product-moment correlation

data:  x and y
t = 27.391, df = 48, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.9465474 0.9826497
sample estimates:
      cor 
0.9694696 
cor.test(x,y, method='spearman') 

    Spearman's rank correlation rho

data:  x and y
S = 0, p-value < 2.2e-16
alternative hypothesis: true rho is not equal to 0
sample estimates:
rho 
  1 
cor.test(x,y, method='kendall')

    Kendall's rank correlation tau

data:  x and y
z = 10.247, p-value < 2.2e-16
alternative hypothesis: true tau is not equal to 0
sample estimates:
tau 
  1 

서로 독립인 두 변수의 상관분석

## 3) independent data
set.seed(1)
sample0 = rnorm(1000)
sample1 = rnorm(1000)
plot(sample0, sample1)

cor.test(sample0, sample1)

    Pearson's product-moment correlation

data:  sample0 and sample1
t = 0.20223, df = 998, p-value = 0.8398
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.05561394  0.06836716
sample estimates:
        cor 
0.006401211 
cor.test(sample0, sample1, method='spearman')

    Spearman's rank correlation rho

data:  sample0 and sample1
S = 168382440, p-value = 0.745
alternative hypothesis: true rho is not equal to 0
sample estimates:
        rho 
-0.01029565 
cor.test(sample0, sample1, method='kendall')

    Kendall's rank correlation tau

data:  sample0 and sample1
z = -0.32648, p-value = 0.7441
alternative hypothesis: true tau is not equal to 0
sample estimates:
         tau 
-0.006894895 

Cars93 데이터셋에서 차량중량과 가격 간의 상관관계 검정

## 4) MASS's Cars93

library(MASS)
plot(Cars93$Weight, Cars93$Price)

pairs(~Price+Weight+MPG.city + Length, data=Cars93) #가격, 차량 중량, 연비, 차량 길이 비교, 산점도 행렬 그리기

# pearson correlation of Weight and Price
cor.test(Cars93$Weight, Cars93$Price) #검정결과 두 변수는 양의 상관관계(직선관계)가 있다.

    Pearson's product-moment correlation

data:  Cars93$Weight and Cars93$Price
t = 8.0984, df = 91, p-value = 2.395e-12
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.5108143 0.7517763
sample estimates:
     cor 
0.647179 
# spearman corr~
cor.test(Cars93$Weight, Cars93$Price, method='spearman')
Warning in cor.test.default(Cars93$Weight, Cars93$Price, method = "spearman"):
Cannot compute exact p-value with ties

    Spearman's rank correlation rho

data:  Cars93$Weight and Cars93$Price
S = 29343, p-value < 2.2e-16
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho 
0.7810954 
# kendall corr~
cor.test(Cars93$Weight, Cars93$Price, method='kendall')

    Kendall's rank correlation tau

data:  Cars93$Weight and Cars93$Price
z = 8.3879, p-value < 2.2e-16
alternative hypothesis: true tau is not equal to 0
sample estimates:
      tau 
0.5924276