Skip to main content

Compute correlations: matrix, cross or single pair, simple or partial

Description

cor_compute() is the single computing function of corrkit.

  • y = NULL: every pair of the variables x (correlation matrix);

  • y given: every pair x x y (e.g. biomarkers x scores);

  • one x and one y: a single pair;

  • covariates: partial correlations (any of the above).

Usage

cor_compute(
  data,
  x,
  y = NULL,
  covariates = NULL,
  method = "spearman",
  p_adjust = "none",
  adjust_by = c("y", "x", "all"),
  ci = 0.95,
  engine = c("auto", "psych", "correlation", "stats", "ppcor", "base"),
  labels = NULL,
  bin_mark = " [0/1]"
)

Arguments

data data frame
x variables (rows of the outputs)
y variables crossed with x (columns); NULL = every pair of x
covariates covariates of a partial correlation (NULL = none); factors allowed (dummy coded)
method “spearman” (default), “pearson”, “kendall”; without covariates also the methods of the correlation package: “biweight”, “percentage”, “blomqvist”, “hoeffding”, “gaussian”, “shepherd”, “distance”
p_adjust method of stats::p.adjust(): “none” (default, raw p-values) or e.g. “BH”, “holm”, “bonferroni”
adjust_by family of the adjustment of a cross analysis: “y” (the predictors of each column, default), “x” (the columns of each predictor) or “all”; a matrix is always adjusted over all its pairs
ci confidence level of the intervals (NULL = none)
engine “auto” (default), “psych”, “correlation”, “stats” (matrix without covariates), “ppcor” or “base” (partial correlations)
labels named vector variable -> display label, used by the outputs
bin_mark marker appended to the labels of binary variables

Details

Engines (an existing function for every computation)

p-values are raw by default (p_adjust = “none”). Otherwise they are adjusted with stats::p.adjust() over the k(k - 1)/2 pairs (matrix) or over the family set by adjust_by (cross).

Binary variables are coded 0/1, the upper level being 1 (see as_cor_numeric()), and flagged in the result; see cor_binary_note() and cor_binary_effects().

Value

object of class cor_result: a list with

  • table: one row per pair, with X, Y, x_type, y_type, r (the coefficient, whatever the method), ci_low, ci_high, p, p_adj, n, k (number of covariate terms), engine;

  • meta: settings of the analysis (mode, method, adjustment, family, covariates, variables and their types, labels…);

  • data: the columns used, for plot_cor_pair() and cor_binary_effects().

Methods: print(), plot(), autoplot(), tbl_cor(), as.matrix(), as.data.frame().

Examples

library("corrkit")

d   <- simulate_cor_data(150)
lab <- attr(d, "labels")

# every pair of a set of variables
cm <- cor_compute(d, x = c("CRP", "IL6", "BDNF", "NfL", "BIN_CRP"), labels = lab)
cm
<cor_result> Spearman correlations: 5 variables, 10 pairs (1 binary variable)
  engine: psych::corr.test() {psych} | N = 142–146
  Raw p (no multiple-testing adjustment): * < 0.05, ** < 0.01, *** < 0.001; no star = p ≥ 0.05.

        CRP     IL6     BDNF  NfL  BIN_CRP
CRP     1                                 
IL6     0.41*** 1                         
BDNF    -0.14   -0.23** 1                 
NfL     0.12    0.08    -0.05 1           
BIN_CRP 0.87*** 0.30*** -0.10 0.12 1      
               CRP         IL6        BDNF         NfL     BIN_CRP
CRP      1.0000000  0.41066876 -0.13887643  0.12301507  0.86575902
IL6      0.4106688  1.00000000 -0.22553259  0.07996328  0.30023797
BDNF    -0.1388764 -0.22553259  1.00000000 -0.04666500 -0.09951434
NfL      0.1230151  0.07996328 -0.04666500  1.00000000  0.12285297
BIN_CRP  0.8657590  0.30023797 -0.09951434  0.12285297  1.00000000
# biomarkers x scores, adjusted for age and sex, BH over the predictors of each score
cc <- cor_compute(d, x = c("CRP", "IL6", "NfL", "BIN_CRP"), y = c("depression", "cognition"),
                  covariates = c("age", "sex"), p_adjust = "BH", labels = lab)
cc
<cor_result> partial Spearman correlations: 4 x 2 variables, 8 pairs (1 binary variable)
  adjusted for: age, sex 
  engine: ppcor::pcor.test() {ppcor} | N = 142–143
  Adjusted p, Benjamini-Hochberg (FDR) over the 4 predictors of each column: * < 0.05, ** < 0.01, *** < 0.001; no star = adjusted p ≥ 0.05.

        depression cognition
CRP     0.22*      -0.01    
IL6     0.34***    0.00     
NfL     0.02       -0.25*   
BIN_CRP 0.18*      0.02     
Back to top