결과 통일을 위해 seed 설정
set.seed(1123)
그래프 그려서 데이터 살펴보기
x <- sample(1:20, 20) + rnorm(10, sd = 2)
y <- x + rnorm(10, sd = 3)
z <- (sample(1:20, 20)/2) + rnorm(20, sd = 5)
df <- data.frame(x, y, z)
시각화
plot(df)
x와 y 상관 계수 구하기
cor(x, y)
[1] 0.9434575
x와 z 상관 계수 구하기
cor(x, z)
[1] 0.04525752
y와 z 상관 계수 구하기
cor(y, z)
[1] -0.0606775
데이터 프레임을 이용해 한 번에 구하기
cor.mat <- cor(df)
cor.mat
x y z x 1.00000000 0.9434575 0.04525752 y 0.94345751 1.0000000 -0.06067750 z 0.04525752 -0.0606775 1.00000000
x와 y의 상관이 유의미한 상관인지 테스트하기
cor.test(x, y)
Pearson's product-moment correlation data: x and y t = 12.075, df = 18, p-value = 4.565e-10 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: 0.8599765 0.9777628 sample estimates: cor 0.9434575
x와 z의 상관이 유의미한 상관인지 테스트하기
cor.test(x, z)
Pearson's product-moment correlation data: x and z t = 0.19221, df = 18, p-value = 0.8497 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: -0.4053820 0.4782012 sample estimates: cor 0.04525752
install.packages('psych')
also installing the dependency ‘mnormt’ Updating HTML index of packages in '.Library' Making 'packages.html' ... done
library(psych)
Attaching package: ‘psych’ The following object is masked from ‘package:boot’: logit
corr.test(df, method = 'pearson', alpha = 0.05)
Call:corr.test(x = df, method = "pearson", alpha = 0.05) Correlation matrix x y z x 1.00 0.94 0.05 y 0.94 1.00 -0.06 z 0.05 -0.06 1.00 Sample Size [1] 20 Probability values (Entries above the diagonal are adjusted for multiple tests.) x y z x 0.00 0.0 1 y 0.00 0.0 1 z 0.85 0.8 0 To see confidence intervals of the correlations, print with the short=FALSE option