Correlation Analysis in R 👨‍💻

Oluwafemi Oyedele

2023-12-23

Introduction

  • The last meeting I said that correlation matrix is used to show the type of relationship that exist b/w more than two traits that are continuous.

  • We also explore different method to carry this out e.g. base R (cor function); metan package cor_coef and also correlation pacakge in R.

Base R

p <- cor(iris[,1:4])
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length    1.0000000  -0.1175698    0.8717538   0.8179411
Sepal.Width    -0.1175698   1.0000000   -0.4284401  -0.3661259
Petal.Length    0.8717538  -0.4284401    1.0000000   0.9628654
Petal.Width     0.8179411  -0.3661259    0.9628654   1.0000000

Metan Package

#install.packages('metan')

library(metan)

corrl <- corr_coef(mtcars)
p <- plot(corrl)

Correlation Package

# install.packages('correlation')

library(correlation)

Cor <- correlation(data = mtcars,redundant = F)


p1 <- summary(object = Cor,redundant = F)


#summary(correlation(data = mtcars,redundant = F))


#correlation(data = mtcars,redundant = F) %>%
# summary()
# Correlation Matrix (pearson-method)

Parameter |    carb |    gear |       am |       vs |     qsec |       wt |     drat |       hp |     disp |      cyl
---------------------------------------------------------------------------------------------------------------------
mpg       |  -0.55* |    0.48 |   0.60** |   0.66** |     0.42 | -0.87*** |  0.68*** | -0.78*** | -0.85*** | -0.85***
cyl       |   0.53* |   -0.49 |   -0.52* | -0.81*** |   -0.59* |  0.78*** | -0.70*** |  0.83*** |  0.90*** |         
disp      |    0.39 |  -0.56* |   -0.59* | -0.71*** |    -0.43 |  0.89*** | -0.71*** |  0.79*** |          |         
hp        | 0.75*** |   -0.13 |    -0.24 | -0.72*** | -0.71*** |   0.66** |    -0.45 |          |          |         
drat      |   -0.09 | 0.70*** |  0.71*** |     0.44 |     0.09 | -0.71*** |          |          |          |         
wt        |    0.43 |  -0.58* | -0.69*** |   -0.55* |    -0.17 |          |          |          |          |         
qsec      | -0.66** |   -0.21 |    -0.23 |  0.74*** |          |          |          |          |          |         
vs        |  -0.57* |    0.21 |     0.17 |          |          |          |          |          |          |         
am        |    0.06 | 0.79*** |          |          |          |          |          |          |          |         
gear      |    0.27 |         |          |          |          |          |          |          |          |         

p-value adjustment method: Holm (1979)

Correlation Plot

# install.packages('ggpmisc')

library(ggpmisc)

p <- ggplot(data = mtcars,aes(x=mpg,y = disp))+
  geom_point()+
  stat_poly_line(fullrange = T)+
  stat_poly_eq(use_label(c("eq","R2","P")))

Additional Function from Metan

library(metan)

p <- corr_coef(data = mtcars) %>%
  corr_focus(mpg,cyl,disp)
# A tibble: 8 × 4
  var      mpg    cyl   disp
  <chr>  <dbl>  <dbl>  <dbl>
1 hp    -0.776  0.832  0.791
2 drat   0.681 -0.700 -0.710
3 wt    -0.868  0.782  0.888
4 qsec   0.419 -0.591 -0.434
5 vs     0.664 -0.811 -0.710
6 am     0.600 -0.523 -0.591
7 gear   0.480 -0.493 -0.556
8 carb  -0.551  0.527  0.395

GGally for correlation

# install.packages("GGally")

library(GGally)

p <- GGally::ggpairs(iris, columns = 1:4)

Merry Christmas and Happy New Year in Advance