corr {NCStats} R Documentation

Compute correlation coefficients.

Description

Computes the correlation coefficients between `y` and `x` if these are vectors. If `y` and `x` are matrices then the correlation between the columns of `y` and the columns of `x` are computed. This functionality is the same as `cor` in stats except that it adds the formula notation `y~x`.

Usage

```corr(x, ...)

## Default S3 method:
corr(x, y = NULL, use = "everything",
method = c("pearson", "kendall", "spearman"), digits = NULL, ...)

## S3 method for class 'formula'
corr(x, data, use = "everything", method = c("pearson",
"kendall", "spearman"), digits = getOption("digits"), ...)
```

Arguments

 `x` A numeric vector, matrix, or data.frame (see details in `cor`) or a formula (see details here) `...` Unused. `y` A numeric vector, matrix, or data.frame (see details in `cor`) `use` An optional character string that defines a method for computing the correlation in the presence of missing values. This must be one of `"everything"`, `"all.obs"`, `"complete.obs"`, `"na.or.complete"`, or `"pairwise.complete.obs"` `method` A single character string that indicates which correlation coefficient is to be computed. Must be one of `"pearson"` (default), `"kendall"`, or `"spearman"` `digits` A single numeric that indicates the number of decimals to which the result should be rounded. `data` An optional data frame that contains the variables in the formula.

Details

see details in `cor`. The formula version is only used with two variables in either the `y~x` or `~y+x` form.

Value

A single numeric if `x` and `y` are vectors or a numeric matrix if `x` and `y` are matrices.

Author(s)

Derek H. Ogle, derek@derekogle.com, but this is largely a wrapper to `cor` from stats that includes a formula version.

See `cor`.

Examples

```## example from stats::cor() ... all return equivalent
df <- data.frame(x=1:10,y=2:11)
cor(df\$x,df\$y)
corr(df\$x,df\$y)
corr(y~x,data=df)
corr(~x+y,data=df)
corr(df)

## another example from stats::cor()
cor(longley)
corr(longley)
corr(longley[1:2],longley[3:5])

## handling missing values
set.seed(3473) # to control randomness
df <- data.frame(x=c(runif(9),NA),y=c(NA,runif(9)),z=runif(10),w=runif(10))
corr(~x+y,data=df)
corr(~x+y,data=df,use="complete.obs")
corr(~x+y+z,data=df,use="complete.obs")
corr(~x+y+z,data=df,use="pairwise.complete.obs")
# no messages below (as no missing values)
corr(~w+z,data=df,use="complete.obs")
corr(~w+z,data=df,use="pairwise.complete.obs")

```

[Package NCStats version 0.4.7 Index]