corr_coef: Linear and partial correlation coefficients

View source: R/corr_coef.R

corr_coefR Documentation

Linear and partial correlation coefficients

Description

Computes Pearson's linear correlation or partial correlation with p-values

Usage

corr_coef(
  data,
  ...,
  type = c("linear", "partial"),
  method = c("pearson", "kendall", "spearman"),
  use = c("pairwise.complete.obs", "everything", "complete.obs"),
  by = NULL,
  verbose = TRUE
)

Arguments

data

The data set. It understand grouped data passed from dplyr::group_by().

...

Variables to use in the correlation. If no variable is informed all the numeric variables from data are used.

type

The type of correlation to be computed. Defaults to "linear". Use type = "partial" to compute partial correlation.

method

a character string indicating which partial correlation coefficient is to be computed. One of "pearson" (default), "kendall", or "spearman"

use

an optional character string giving a method for computing covariances in the presence of missing values. See stats::cor for more details

by

One variable (factor) to compute the function by. It is a shortcut to dplyr::group_by().This is especially useful, for example, to compute correlation matrices by levels of a factor.

verbose

Logical argument. If verbose = FALSE the code is run silently.

Details

The partial correlation coefficient is a technique based on matrix operations that allow us to identify the association between two variables by removing the effects of the other set of variables present (Anderson 2003) A generalized way to estimate the partial correlation coefficient between two variables (i and j ) is through the simple correlation matrix that involves these two variables and m other variables from which we want to remove the effects. The estimate of the partial correlation coefficient between i and j excluding the effect of m other variables is given by: \loadmathjax \mjsdeqnr_ij.m = \frac- a_ij\sqrt a_iia_jj

Where \mjseqnr_ij.m is the partial correlation coefficient between variables i and j, without the effect of the other m variables; \mjseqna_ij is the ij-order element of the inverse of the linear correlation matrix; \mjseqna_ii, and \mjseqna_jj are the elements of orders ii and jj, respectively, of the inverse of the simple correlation matrix.

Value

A list with the correlation coefficients and p-values

Author(s)

Tiago Olivoto tiagoolivoto@gmail.com

References

Anderson, T. W. 2003. An introduction to multivariate statistical analysis. 3rd ed. Wiley-Interscience.

Examples


library(metan)

# All numeric variables
all <- corr_coef(data_ge2)


# Select variable
sel <-
  corr_coef(data_ge2,
            EP, EL, CD, CL)
sel$cor

# Select variables, partial correlation
sel <-
  corr_coef(data_ge2,
            EP, EL, CD, CL,
            type = "partial")
sel$cor




TiagoOlivoto/WAASB documentation built on March 20, 2024, 4:18 p.m.