correlate: Compute correlation matrix

View source: R/correlate.R

correlateR Documentation

Compute correlation matrix

Description

Compute correlation matrix for multiple variables and/or multiple groups.

Usage

correlate(
  formula,
  data,
  repetition,
  use = "everything",
  method = "pearson",
  collapse.value = ".",
  collapse.var = ".",
  na.rm
)

Arguments

formula

[formula] on the left hand side the outcome(s) and on the right hand side the grouping variables. E.g. Y1+Y2 ~ Gender will compute for each gender the correlation matrix for Y1 and the correaltion matrix for Y2.

data

[data.frame] dataset containing the observations.

repetition

[formula] Specify the structure of the data: the time/repetition variable and the grouping variable, e.g. ~ time|id.

use

[character] method for computing correlation in the presence of missing values: "everything", "all.obs", "complete.obs", "na.or.complete", or "pairwise.complete.obs". Passed to stats::cor.

method

[character] type of correlation coefficient: "pearson", "kendall", "spearman". Passed to stats::cor.

collapse.value

[character] symbol used to combine covariate values when using multiple grouping variables.

collapse.var

[character] symbol used to combine variable names to be pasted left of the covariate values when using multiple grouping variables. Can be disabled setting it to NULL or FALSE.

na.rm

[logical] not used. The user may expect this argument though so it is added to help the user with a message pointing toward the argument use.

See Also

as.array to convert the output to an array or as.matrix to convert the output to a matrix (when a single outcome and no grouping variable).
summarize for other summary statistics (e.g. mean, standard deviation, ...).

Examples

#### simulate data (wide format) ####
data(gastricbypassL, package = "LMMstar")

## compute correlations (composite time variable)
e.S <- correlate(weight ~ 1, data = gastricbypassL, repetition = ~time|id)
e.S
as.matrix(e.S)

e.S21 <- correlate(glucagonAUC + weight ~ 1, data = gastricbypassL,
                  repetition = ~time|id, use = "pairwise")
e.S21
as.array(e.S21)
as.matrix(e.S21, index = "weight")

gastricbypassL$sex <- as.numeric(gastricbypassL$id) %% 2
e.S12 <- correlate(weight ~ sex, data = gastricbypassL,
                   repetition = ~time|id, use = "pairwise")
e.S12
as.array(e.S12)

e.S22 <- correlate(glucagonAUC + weight ~ sex, data = gastricbypassL,
                   repetition = ~time|id, use = "pairwise")
e.S22
as.array(e.S22)

bozenne/repeated documentation built on July 16, 2025, 11:16 p.m.