Learn R Programming

matrixCorr

matrixCorr computes correlation and related association matrices from small to high-dimensional data using simple, consistent functions and sensible defaults. It includes shrinkage and robust options for noisy or p ≥ n settings, plus convenient print/plot methods. Performance-critical paths are implemented in C++ with BLAS/OpenMP and memory-aware symmetric updates. The API accepts base matrices and data frames and returns standard R objects via a consistent S3 interface.

Supported measures include Pearson, Spearman, Kendall, distance correlation, partial correlation, and robust biweight mid-correlation; agreement tools cover Bland–Altman (two-method and repeated-measures) and Lin’s concordance correlation coefficient (including repeated-measures LMM/REML extensions).

Features

  • High-performance C++ backend using Rcpp
  • General correlations such as pearson_corr(), spearman_rho(), kendall_tau()
  • Robust correlation metrics (biweight_mid_corr())
  • Distance correlation (distance_corr())
  • Partial correlation (partial_correlation())
  • Shrinkage for $p >> n$ (schafer_corr())
  • Agreement metrics
    • Bland–Altman (two-method bland_altman() and repeated-measures bland_altman_repeated()),
    • Lin’s concordance correlation coefficient (pairwise ccc(), repeated-measures LMM/REML ccc_lmm_reml() and non-parametric ccc_pairwise_u_stat())

Installation

# Install from GitHub
# install.packages("devtools")
devtools::install_github("Prof-ThiagoOliveira/matrixCorr")

Example

Correlation matrices (Pearson, Spearman, Kendall)

library(matrixCorr)

set.seed(1)
X <- as.data.frame(matrix(rnorm(300 * 6), ncol = 6))
names(X) <- paste0("V", 1:6)

R_pear <- pearson_corr(X)
R_spr  <- spearman_rho(X)
R_ken  <- kendall_tau(X)

print(R_pear, digits = 2)
plot(R_spr)   # heatmap

Robust correlation (biweight mid-correlation)

set.seed(2)
Y <- X
# inject outliers
Y$V1[sample.int(nrow(Y), 8)] <- Y$V1[sample.int(nrow(Y), 8)] + 8

R_bicor <- biweight_mid_corr(Y)
print(R_bicor, digits = 2)

High-dimensional shrinkage correlation ($p >> n$)

set.seed(3)
n <- 60; p <- 200
Xd <- matrix(rnorm(n * p), n, p)
colnames(Xd) <- paste0("G", seq_len(p))

R_shr <- schafer_corr(Xd)
print(R_shr, digits = 2, max_rows = 6, max_cols = 6)

Partial correlation matrix

R_part <- partial_correlation(X)
print(R_part, digits = 2)

Distance correlation matrix

R_dcor <- distance_corr(X)
print(R_dcor, digits = 2)

Agreement analyses

Two-method Bland–Altman

set.seed(4)
x <- rnorm(120, 100, 10)
y <- x + 0.5 + rnorm(120, 0, 8)

ba <- bland_altman(x, y)
print(ba)
plot(ba)

Repeated-measures Bland–Altman (pairwise matrix)

set.seed(5)
S <- 20; Tm <- 6
subj  <- rep(seq_len(S), each = Tm)
time  <- rep(seq_len(Tm), times = S)

true  <- rnorm(S, 50, 6)[subj] + (time - mean(time)) * 0.4
mA    <- true + rnorm(length(true), 0, 2)
mB    <- true + 1.0 + rnorm(length(true), 0, 2.2)
mC    <- 0.95 * true + rnorm(length(true), 0, 2.5)

dat <- rbind(
  data.frame(y = mA, subject = subj, method = "A", time = time),
  data.frame(y = mB, subject = subj, method = "B", time = time),
  data.frame(y = mC, subject = subj, method = "C", time = time)
)
dat$method <- factor(dat$method, levels = c("A","B","C"))

ba_rep <- bland_altman_repeated(
  data = dat, response = "y", subject = "subject",
  method = "method", time = "time",
  include_slope = FALSE, use_ar1 = FALSE
)
summary(ba_rep)
# plot(ba_rep)  # faceted BA scatter by pair

Lin’s concordance correlation coefficient (repeated-measures LMM/REML)

set.seed(6)
S <- 30; Tm <- 8
id     <- factor(rep(seq_len(S), each = 2 * Tm))
method <- factor(rep(rep(c("A","B"), each = Tm), times = S))
time   <- rep(rep(seq_len(Tm), times = 2), times = S)

u  <- rnorm(S, 0, 0.8)[as.integer(id)]
g  <- rnorm(S * Tm, 0, 0.5)
g  <- g[ (as.integer(id) - 1L) * Tm + as.integer(time) ]
y  <- (method == "B") * 0.3 + u + g + rnorm(length(id), 0, 0.7)

dat_ccc <- data.frame(y, id, method, time)
fit_ccc <- ccc_lmm_reml(dat_ccc, response = "y", rind = "id",
                        method = "method", time = "time")
summary(fit_ccc)  # overall CCC, variance components, SEs/CI

Contributing

Issues and pull requests are welcome. Please see CONTRIBUTING.md for guidelines and cran-comments.md/DESCRIPTION for package metadata.

License

MIT Thiago de Paula Oliveira

See inst/LICENSE for the full MIT license text.

Copy Link

Version

Install

install.packages('matrixCorr')

Monthly Downloads

245

Version

0.8.3

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Thiago de Paula Oliveira

Last Published

December 22nd, 2025

Functions in matrixCorr (0.8.3)

build_LDZ

build_LDZ
ccc_lmm_reml

Concordance Correlation via REML (Linear Mixed-Effects Model)
ccc

Pairwise Lin's concordance correlation coefficient
bland_altman_repeated

Bland-Altman for repeated measurements
ccc_pairwise_u_stat

Repeated-Measures Lin's Concordance Correlation Coefficient (CCC)
abort_bad_arg

Abort with a standardised argument error
biweight_mid_corr

Biweight mid-correlation (bicor)
bland_altman

Bland-Altman statistics with confidence intervals
check_scalar_int_pos

Check strictly positive scalar integer
check_matrix_dims

Check matrix dimensions
check_scalar_nonneg

Check a non-negative scalar (>= 0)
ccc_lmm_reml_pairwise

ccc_lmm_reml_pairwise
check_required_cols

Check that a data frame contains required columns
check_inherits

Check object class
check_prob_scalar

Check probability in unit interval
abort_internal

Abort for internal errors (should not happen)
check_scalar_character

Check scalar character (non-empty)
estimate_rho

estimate_rho
compute_ci_from_se

compute_ci_from_se
check_weights

Check weights vector (non-negative, finite, correct length)
check_same_length

Check that two vectors have the same length
matrixCorr-internal

matrixCorr: Collection of Correlation and Association Estimators
check_scalar_numeric

Check scalar numeric (with optional bounds)
check_ar1_rho

Check AR(1) correlation parameter
check_bool

Check a single logical flag
check_symmetric_matrix

Check matrix symmetry
.ba_rep_two_methods

two-method helper
.vc_message

.vc_message
pearson_corr

Pairwise Pearson correlation
print.ccc_ci

S3 print for legacy class ccc_ci
num_or_na

num_or_na
inform_if_verbose

Inform only when verbose
print.matrixCorr_ccc

Print method for matrixCorr CCC objects
print.matrixCorr_ccc_ci

Print method for matrixCorr CCC objects with CIs
view_corr_shiny

Interactive Shiny viewer for matrixCorr objects
.align_weights_to_levels

Align (optional named) weights to a subset of time levels
distance_corr

Pairwise Distance Correlation (dCor)
num_or_na_vec

num_or_na_vec
spearman_rho

Pairwise Spearman's rank correlation
match_arg

Match argument to allowed values
kendall_tau

Pairwise (or Two-Vector) Kendall's Tau Rank Correlation
summary.ccc_lmm_reml

Summary Method for ccc_lmm_reml Objects
partial_correlation

Partial correlation matrix (sample / ridge / OAS)
schafer_corr

Schafer-Strimmer shrinkage correlation
run_cpp

run_cpp