Covariance and Correlation in R Programming

14 Jan, 2022
Covariance and Correlation are terms used in statistics to measure relationships between two random variables. Both of these terms measure linear dependency between a pair of random variables or bivariate data. 
In this article, we are going to discuss cov(), cor() and cov2cor() functions in R which use covariance and correlation methods of statistics and probability theory.
In R programming, covariance can be measured using cov() function. Covariance is a statistical term used to measures the direction of the linear relationship between the data vectors. Mathematically, 
where, 
x represents the x data vector y represents the y data vector [Tex]\bar{x}  [/Tex]represents mean of x data vector [Tex]\bar{y}  [/Tex]represents mean of y data vector N represents total observations
Syntax: cov(x, y, method)
where, 
x and y represents the data vectors
method defines the type of method to be used to compute covariance. Default is “pearson”.
Example: 
R
# Data vectorsx <- c(1, 3, 5, 10) y <- c(2, 4, 6, 20) # Print covariance using different methodsprint(cov(x, y))print(cov(x, y, method = "pearson"))print(cov(x, y, method = "kendall"))print(cov(x, y, method = "spearman"))
Output: 
[1] 30.66667
[1] 30.66667
[1] 12
[1] 1.666667
cor() function in R programming measures the correlation coefficient value. Correlation is a relationship term in statistics that uses the covariance method to measure how strong the vectors are related. Mathematically,
where, 
x represents the x data vector y represents the y data vector [Tex]\bar{x}  [/Tex]represents mean of x data vector [Tex]\bar{y}  [/Tex]represents mean of y data vector
Syntax: cor(x, y, method)
where, 
x and y represents the data vectors
method defines the type of method to be used to compute covariance. Default is “pearson”.
Example: 
R
# Data vectorsx <- c(1, 3, 5, 10) y <- c(2, 4, 6, 20) # Print correlation using different methodsprint(cor(x, y)) print(cor(x, y, method = "pearson"))print(cor(x, y, method = "kendall"))print(cor(x, y, method = "spearman"))
Output: 
[1] 0.9724702
[1] 0.9724702
[1] 1
[1] 1
cov2cor() function in R programming converts a covariance matrix into corresponding correlation matrix.
Syntax: cov2cor(X)
where, 
X and y represents the covariance square matrix
Example: 
R
# Data vectorsx <- rnorm(2)y <- rnorm(2) # Binding into square matrixmat <- cbind(x, y) # Defining X as the covariance matrixX <- cov(mat) # Print covariance matrixprint(X) # Print correlation matrix of data# vectorprint(cor(mat)) # Using function cov2cor()# To convert covariance matrix to# correlation matrixprint(cov2cor(X))
Output: 
           x          y
x  0.0742700 -0.1268199
y -0.1268199  0.2165516

   x  y
x  1 -1
y -1  1

   x  y
x  1 -1
y -1  1
kumar_satyam
rkbhola5
R-Functions
R-Statistics
R-Vectors
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change column name of a given DataFrame in R
Filter data by multiple conditions in R using Dplyr
How to Replace specific values in column in R DataFrame ?
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Loops in R (for, while, repeat)
Adding elements in a vector in R programming - append() method
Group by function in R using Dplyr
How to change Row Names of DataFrame in R ?
Convert Factor to Numeric and Numeric to Factor in R Programming