corx: Compute and format a correlation matrix

View source: R/corx.R

corxR Documentation

Compute and format a correlation matrix

Description

Compute ordinary or partial correlations and format them for an APA-style table. Unrounded coefficients, p-values, and observation counts are also available separately.

Usage

corx(
  data,
  x = NULL,
  y = NULL,
  z = NULL,
  method = c("pearson", "spearman", "kendall"),
  stars = c(0.05, 0.01, 0.001),
  p_adjust = c("none", "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr"),
  round = 2,
  remove_lead = TRUE,
  triangle = NULL,
  caption = NULL,
  note = NULL,
  describe = FALSE,
  grey_nonsig = TRUE,
  call_only = FALSE
)

Arguments

data

A data frame or matrix with at least three rows. Selected columns must be numeric or integer. Numeric columns with value labels are accepted; character and factor columns are not.

x

Column selection for the rows of the result. The default, NULL, selects all columns. Use bare names, quoted names, or tidyselect helpers such as where(is.numeric). For a character vector stored in cols, use tidyselect::all_of(cols). An explicit selection must match a column. Use c(Label = column) to label a variable without renaming the input.

y

Column selection for the columns of the result, using the same syntax as x. The default, NULL, uses the selection in x. An explicit selection must match a column. Labels on the two axes may differ.

z

Column selection for control variables, using the same syntax as x. The default, NULL, computes ordinary correlations. Selected controls are removed from both axes of the result.

method

Correlation method: "pearson" (the default), "spearman", or "kendall".

stars

Numeric p-value thresholds, listed from largest to smallest. Each threshold a p-value falls strictly below adds one star. Use NULL or numeric(0) to omit stars.

p_adjust

Multiple-testing adjustment passed to stats::p.adjust(): "none" (the default), "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", or "fdr".

round

Number of decimal places in the formatted table. The default is 2. This does not round the stored coefficients or p-values.

remove_lead

Logical. If TRUE (the default), omit leading zeros from formatted coefficients (for example, .42 instead of 0.42).

triangle

Which part of a square matrix with the same variables in the same order on both axes to show in the formatted table: "lower", "upper", or NULL (the default) for the full table. The stored numeric matrices are not cropped.

caption

Optional character string printed above the table and used as the default plot title.

note

Optional character string printed below the table. If NULL, a note is generated from the significance thresholds in stars.

describe

Descriptive statistics to append for each row variable. Use FALSE (the default) for none, TRUE for the mean and standard deviation, or a named list of functions that each accept a column and return one value. You can also select the built-in summaries mean, sd, var, median, iqr, skewness, kurtosis, and n (the number of nonmissing values), for example c(M = mean, n). The built-in summaries remove missing values from each variable independently; custom functions handle missing values themselves.

grey_nonsig

Logical. If TRUE (the default), print coefficients without significance stars in grey when stars are enabled and describe = FALSE. This affects console output only.

call_only

Logical. If TRUE, return the matched call and its environments for debugging, without computing correlations. The default is FALSE.

Details

Ordinary Pearson correlations use the two-sided t test with n - 2 degrees of freedom. Ordinary rank correlations use the asymptotic p-values from stats::cor.test(). Partial correlations use ppcor::pcor.test().

Missing data. Each pair uses only the rows observed for both variables and all controls. The n matrix records this count, even when it is zero. Ordinary correlations based on fewer than two observations have missing coefficients and p-values. With exactly two observations, ordinary Pearson and Spearman correlations have a coefficient but no p-value. Partial tests require at least three observations beyond the number of controls (two for Kendall); otherwise both the coefficient and p-value are missing.

Multiple comparisons. The adjustment is applied to distinct unordered variable pairs in the matrix. Self-correlations are excluded and their p-values are left unchanged. If a pair appears in both orientations, both cells receive the same adjusted p-value. The display selected by triangle does not change the set of comparisons.

Value

A list of class corx with the following components:

  • call: The matched function call.

  • apa: The formatted character matrix.

  • r: The unrounded matrix of correlation coefficients.

  • p: The matrix of p-values, adjusted if requested.

  • n: The matrix of observation counts.

  • caption, note: The text displayed with the table.

With call_only = TRUE, the result is an unclassed list containing the matched call, the function's evaluation environment (env), and the calling environment (parent_env).

See Also

corx-methods, print.corx(), plot.corx(), plot_mds(), to_table(), to_clipboard().

Examples

fit <- corx(mtcars, x = c(mpg, cyl, disp), y = c(drat, qsec),
            z = wt, caption = "Controlling for weight",
            describe = list(M = function(x) mean(x, na.rm = TRUE)))
fit
coef(fit)
fit$p

corx(iris[, 1:4], describe = c(median, IQR = iqr, n), triangle = "lower")

corx documentation built on Nov. 25, 2026, 9:06 a.m.

Related to corx in corx...