User Tools

Site Tools


r:correlation

Correlation

cor(data.frame, method=c("pearson", "kendall", "spearman"))
cor.test(data$acol, data$bcol, method=c("pearson", "kendall", "spearman"))
data("mtcars")
my_data <- mtcars[, c(1,2,3,4,5,6,7)]
head(my_data)
                   mpg cyl disp  hp drat    wt  qsec
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02
Datsun 710        22.8   4  108  93 3.85 2.320 18.61
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02
Valiant           18.1   6  225 105 2.76 3.460 20.22
> 
install.packages("Hmisc")
library(Hmisc)
# rcorr(x, type = c("pearson","spearman"))
rcorr(as.matrix(my_data))
> rcorr(as.matrix(my_data))
       mpg   cyl  disp    hp  drat    wt  qsec
mpg   1.00 -0.85 -0.85 -0.78  0.68 -0.87  0.42
cyl  -0.85  1.00  0.90  0.83 -0.70  0.78 -0.59
disp -0.85  0.90  1.00  0.79 -0.71  0.89 -0.43
hp   -0.78  0.83  0.79  1.00 -0.45  0.66 -0.71
drat  0.68 -0.70 -0.71 -0.45  1.00 -0.71  0.09
wt   -0.87  0.78  0.89  0.66 -0.71  1.00 -0.17
qsec  0.42 -0.59 -0.43 -0.71  0.09 -0.17  1.00

n= 32 


P
     mpg    cyl    disp   hp     drat   wt     qsec  
mpg         0.0000 0.0000 0.0000 0.0000 0.0000 0.0171
cyl  0.0000        0.0000 0.0000 0.0000 0.0000 0.0004
disp 0.0000 0.0000        0.0000 0.0000 0.0000 0.0131
hp   0.0000 0.0000 0.0000        0.0100 0.0000 0.0000
drat 0.0000 0.0000 0.0000 0.0100        0.0000 0.6196
wt   0.0000 0.0000 0.0000 0.0000 0.0000        0.3389
qsec 0.0171 0.0004 0.0131 0.0000 0.6196 0.3389       
> 
r/correlation.txt · Last modified: 2018/12/26 15:19 by hkimscil