1. Reading the data

2. Pearson’s correlation coefficient

Pearson correlation (r), which measures a linear dependence between two variables (x and y). It’s also known as a parametric correlation test because it depends to the distribution of the data. It can be used only when x and y are from normal distribution.

Pearson correlation coefficient between mpg and wt in the dataframe mtcars

## [1] -0.8676594

Pearson correlation coefficient and significance value between between mpg and wt in the dataframe mtcars

## 
##  Pearson's product-moment correlation
## 
## data:  mpg and wt
## t = -9.559, df = 30, p-value = 1.294e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.9338264 -0.7440872
## sample estimates:
##        cor 
## -0.8676594

Here, the p-value of the test is 1.294e-10, which is less than the significance level alpha = 0.05. We can conclude that mpg and wt are significantly correlated with a correlation coefficient of -0.8676594 and p-value of 1.294e-10 and the negative value of correlation coefficient indicates to the negative correlation.

3. Spearman Rank correlation

Spearman is a nonparametric measure of rank correlation (statistical dependence between the rankings of two variables). It assesses how well the relationship between two variables can be described using a monotonic function.

Checking Whether mpg and wt are normally distributed or not?

## 
##  Shapiro-Wilk normality test
## 
## data:  mtcars$mpg
## W = 0.94756, p-value = 0.1229
## 
##  Shapiro-Wilk normality test
## 
## data:  mtcars$wt
## W = 0.94326, p-value = 0.09265

The p-value is greater than 0.05, hence we can assume mpg and wt are normally distributed.

Spearman Rank correlation coefficient between mpg and wt in the dataframe mtcars

## [1] -0.886422

Spearman Rank correlation coefficient and significance value between between mpg and wt in the dataframe mtcars

## Warning in cor.test.default(mpg, wt, method = "spearman"): Cannot compute exact
## p-value with ties
## 
##  Spearman's rank correlation rho
## 
## data:  mpg and wt
## S = 10292, p-value = 1.488e-11
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##       rho 
## -0.886422

Here, the p-value of the test is 1.488e-11, which is less than the significance level alpha = 0.05. We can conclude that mpg and wt are significantly correlated with a correlation coefficient of -0.8676594 and p-value of 1.488e-11 and the negative value of correlation coefficient indicates to the negative correlation.

Correlation matrix for all the continuous variables of mtcars dataset

##         mpg     wt     hp   drat
## mpg   1.000 -0.868 -0.776  0.681
## wt   -0.868  1.000  0.659 -0.712
## hp   -0.776  0.659  1.000 -0.449
## drat  0.681 -0.712 -0.449  1.000

Correlation matrix using rcorr() Fuction From Package Hmisc

##        mpg    wt    hp  drat
## mpg   1.00 -0.87 -0.78  0.68
## wt   -0.87  1.00  0.66 -0.71
## hp   -0.78  0.66  1.00 -0.45
## drat  0.68 -0.71 -0.45  1.00
## 
## n= 32 
## 
## 
## P
##      mpg  wt   hp   drat
## mpg       0.00 0.00 0.00
## wt   0.00      0.00 0.00
## hp   0.00 0.00      0.01
## drat 0.00 0.00 0.01

4. Visualizing correlation using scatter plots

Visualise scatterplot matrix for all the continuous variables of mtcars dataset

5. Correlogram

CORRELOGRAM #1

Correlogram with correlation coefficient

## Warning: package 'corrplot' was built under R version 4.0.3
## corrplot 0.84 loaded

CORRELOGRAM #2

## Warning: package 'corrgram' was built under R version 4.0.5
## 
## Attaching package: 'corrgram'
## The following object is masked from 'package:lattice':
## 
##     panel.fill

CORRELOGRAM #4