Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 280

8.3.1 8.3.1 The Pearson Correlation

Оглавление

An important metric for linear relationship is the Pearson correlation coefficient ρ.

correlation – Pearson

Definition: Pearson Correlation Coefficient


cor(mtcars$hp,mtcars$wt) ## [1] 0.6587479

cor()

Of course, we also have functions that provide the covariance matrix and functions that convert the one in the other.

d <- data.frame(mpg = mtcars$mpg, wt = mtcars$wt, hp = mtcars$hp) # Note that we can feed a whole data-frame in the functions. var(d) ## mpg wt hp ## mpg 36.324103 -5.116685 -320.73206 ## wt -5.116685 0. 957379 44.19266 ## hp -320.732056 44.192661 4700.86694 cov(d) ## mpg wt hp ## mpg 36.324103 -5.116685 -320.73206 ## wt -5.116685 0.957379 44.19266 ## hp -320.732056 44.192661 4700.86694 cor(d) ## mpg wt hp ## mpg 1.0000000 -0.8676594 -0.7761684 ## wt -0.8676594 1.0000000 0.6587479 ## hp -0.7761684 0.6587479 1.0000000

var()

cov()

cor()

cov2cor(cov(d)) ## mpg wt hp ## mpg 1.0000000 -0.8676594 -0.7761684 ## wt -0.8676594 1.0000000 0.6587479 ## hp -0.7761684 0.6587479 1.0000000

cov2cor()

The Big R-Book

Подняться наверх