How to Create a Covariance Matrix in R

[This article was first published on Methods – finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Covariance Matrix in R, Covariance is a measure of the degree to which two variables are linearly associated. We can measure how changes in one variable are associated with another variable.

A covariance matrix indicates the covariance between different variables. It’s mainly used to understand how different variables are related.

This article describes how to create a covariance matrix in R.

Covariance Matrix in R

Kendall’s Rank Correlation in R-Correlation Test »

How to Create a Covariance Matrix in R

Step 1: Load the data frame.

Let’s create a data frame that contains different parameter’s scores of 10 different products

data <- data.frame(Appearance = c(8, 8, 8, 9, 7, 9, 9, 7, 8, 9),
                   Thickness = c(8, 8, 7, 7, 7, 8, 9, 8, 7, 9),
                   Spredability= c(7, 9, 9, 9, 8, 8, 7, 8, 6, 7))
   Appearance Thickness Spreadability
1           8         8            7
2           8         8            9
3           8         7            9
4           9         7            9
5           7         7            8
6           9         8            8
7           9         9            7
8           7         8            8
9           8         7            6
10          9         9            7

Step 2: Create the covariance matrix.

Now let’s create the covariance matrix using the cov() function:

Intraclass Correlation Coefficient in R-Quick Guide »

           Appearance  Thickness Spreadability
Appearance    0.62222222  0.2666667  -0.06666667
Thickness     0.26666667  0.6222222  -0.26666667
Spreadability -0.06666667 -0.2666667   1.06666667

Step 3: Inference.

The values along the diagonals of the matrix are simply the variances of each product.

The variance of the appearance scores is 0.62

The variance of the thickness scores is 0.62

The variance of the spreadability scores is 1.06

How to Calculate Partial Correlation coefficient in R-Quick Guide

The other values in the matrix represent the covariances between the various products

The covariance between the appearance and thickness scores is 0.26

The covariance between the appearance and spreadability scores is -0.066

The covariance between the thickness and spreadability scores is –0.266

A positive number for covariance indicates that two variables tend to increase or decrease simultaneously.

For example, appearance and thickness have a positive covariance (0.26), which indicates that products that score high on appearance also tend to score high on thickness.

Point Biserial Correlation in R-Quick Guide »

A negative number for covariance indicates that as one variable increases, a second variable tends to decrease.

For example, appearance and spreadability have a negative covariance (-0.066), which indicates that product which scores high on appearance tend to score low on spreadability.


Covariance can vary between -∞ and +∞

Covariance is affected by the changes in scale.

Covariance is zero indicates if one variable move and the other doesn’t.

PSC Mock Test » Online Platform for all Exams » PSC Mock Test

Subscribe to the Newsletter and COMMENT below!

The post How to Create a Covariance Matrix in R appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)