Correlation matrix is a type of matrix that shows the correlation coefficient of the set of variables and helps in examining the relationship between the variables. Correlation matrix is useful to study the association between the variables. In R programming, cor() function is present in stats package. This package is built-in package in R programming. In this article, let us discuss the correlation matrix with the help of examples in R programming.
Applications of correlation matrix :
- To find the relationships between the variables of the data
- To analyse the data to predict the behaviour of the variables
Syntax :
cor(x, y = NULL, method = c(“pearson”, “kendall”, “spearman”))
Parameters:
x: represents numeric vector, matrix or data frame
y: represents numeric vector, matrix or data frame with same dimensions as x
method: represents method to compute correlation between variables
Example 1 :
In this example, let’s use mtcars dataset present in R programming and create a correlation matrix.
Implementation :
# Load the dataset in a variable
df <- mtcars[, c(1, 2, 3, 4, 5, 6)]
# Print the original data frame
cat("Original data frame:\n")
print(df)
# Create correlation matrix
cor_mt <- cor(df)
# Print correlation matrix
cat("The correlation matrix:\n")
print(cor_mt)
Output :
Original data frame:
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160.0 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875
Datsun 710 22.8 4 108.0 93 3.85 2.320
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440
Valiant 18.1 6 225.0 105 2.76 3.460
Duster 360 14.3 8 360.0 245 3.21 3.570
Merc 240D 24.4 4 146.7 62 3.69 3.190
Merc 230 22.8 4 140.8 95 3.92 3.150
Merc 280 19.2 6 167.6 123 3.92 3.440
Merc 280C 17.8 6 167.6 123 3.92 3.440
Merc 450SE 16.4 8 275.8 180 3.07 4.070
Merc 450SL 17.3 8 275.8 180 3.07 3.730
Merc 450SLC 15.2 8 275.8 180 3.07 3.780
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250
Lincoln Continental 10.4 8 460.0 215 3.00 5.424
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345
Fiat 128 32.4 4 78.7 66 4.08 2.200
Honda Civic 30.4 4 75.7 52 4.93 1.615
Toyota Corolla 33.9 4 71.1 65 4.22 1.835
Toyota Corona 21.5 4 120.1 97 3.70 2.465
Dodge Challenger 15.5 8 318.0 150 2.76 3.520
AMC Javelin 15.2 8 304.0 150 3.15 3.435
Camaro Z28 13.3 8 350.0 245 3.73 3.840
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845
Fiat X1-9 27.3 4 79.0 66 4.08 1.935
Porsche 914-2 26.0 4 120.3 91 4.43 2.140
Lotus Europa 30.4 4 95.1 113 3.77 1.513
Ford Pantera L 15.8 8 351.0 264 4.22 3.170
Ferrari Dino 19.7 6 145.0 175 3.62 2.770
Maserati Bora 15.0 8 301.0 335 3.54 3.570
Volvo 142E 21.4 4 121.0 109 4.11 2.780
The correlation matrix:
mpg cyl disp hp drat wt
mpg 1.0000000 -0.8521620 -0.8475514 -0.7761684 0.6811719 -0.8676594
cyl -0.8521620 1.0000000 0.9020329 0.8324475 -0.6999381 0.7824958
disp -0.8475514 0.9020329 1.0000000 0.7909486 -0.7102139 0.8879799
hp -0.7761684 0.8324475 0.7909486 1.0000000 -0.4487591 0.6587479
drat 0.6811719 -0.6999381 -0.7102139 -0.4487591 1.0000000 -0.7124406
wt -0.8676594 0.7824958 0.8879799 0.6587479 -0.7124406 1.0000000
Example 2 :
In this example, let’s take trees dataset and create correlation matrix
# Load the dataset in a variable
df <- trees
# Print the original data frame
cat("Original data frame:\n")
print(df)
# Create correlation matrix
cor_mt <- cor(df)
# Print correlation matrix
cat("The correlation matrix:\n")
print(cor_mt)
Output :
Original data frame:
Girth Height Volume
1 8.3 70 10.3
2 8.6 65 10.3
3 8.8 63 10.2
4 10.5 72 16.4
5 10.7 81 18.8
6 10.8 83 19.7
7 11.0 66 15.6
8 11.0 75 18.2
9 11.1 80 22.6
10 11.2 75 19.9
11 11.3 79 24.2
12 11.4 76 21.0
13 11.4 76 21.4
14 11.7 69 21.3
15 12.0 75 19.1
16 12.9 74 22.2
17 12.9 85 33.8
18 13.3 86 27.4
19 13.7 71 25.7
20 13.8 64 24.9
21 14.0 78 34.5
22 14.2 80 31.7
23 14.5 74 36.3
24 16.0 72 38.3
25 16.3 77 42.6
26 17.3 81 55.4
27 17.5 82 55.7
28 17.9 80 58.3
29 18.0 80 51.5
30 18.0 80 51.0
31 20.6 87 77.0
The correlation matrix:
Girth Height Volume
Girth 1.0000000 0.5192801 0.9671194
Height 0.5192801 1.0000000 0.5982497
Volume 0.9671194 0.5982497 1.0000000