Skip to main content

3.7) Covariance And Correlation


Some functions that naturally accompany scatterplots are cov() for calculating covariances and cor() for correlations (covariances of standardized data). If you want to test whether a correlation statistic is consistent with a null hypothesis of no association, you need cor.test(). The output from this function is a list, which you can explore using the generic function str().

If you give cor() a matrix of variables for its first argument, a whole triangle of coefficients will be calculated. If there are any NAs in your data, check the help file for options with the use= argument.

The hidden print() function

What you see on the screen when you type the name of any object in R is actually the result of applying a hidden generic function print() to that object. For simple objects, this just prints the contents of the object, but objects like the ones produced by cor.test() actually have specified print. methods that allow their contents (which may include lots of output you don’t normally want) to be summarized in a neat form. That’s why you may want to use another generic function, str(), to investigate what’s actually stored in a complex object. And it helps make R more user-friendly.