Covariance is the simplest and widely used measure of correlation. We can find the covariance between two variables in R using the cov function. Covariance measures the linear relationship between two variables in a dataset. A positive covariance value indicates a positive linear relationship between the variables, and a negative value represents the negative linear relationship. In a previous post, I have explained calculating covariance in a spreadsheet.
Steps to calculate Covariance in R
1. To illustrate how to calculate covariance in R. I use in-built women data. This data consists of two variables i.e. Average Heights and Weights of American Women. Load the inbuilt data using the following command
> data("women")
2. Let’s find the covariance between the heights and weights in the dataset
> cov(women$height,women$weight) [1] 69
The covariance result is 69. The result is a positive number, which denotes a positive relationship between the two variables. Remember the order you use in cov command doesn’t matter cov(women$height,women$weight) and cov(women$weight,women$height) both these will give the same result.