Learn R Programming

lineup (version 0.34-1)

corbetw2mat: Calculate correlations between columns of two matrices

Description

For matrices x and y, calculate the correlation between columns of x and columns of y.

Usage

corbetw2mat(x, y, what=c("paired", "bestright", "bestpairs", "all"),
            corthresh=0.9)

Arguments

x
A numeric matrix.
y
A numeric matrix with the same number of rows as x.
what
Indicates which correlations to calculate and return. See value, below.
corthresh
Threshold on correlations if what="bestpairs".

Value

  • If what="paired", the return value is a vector of correlations, between columns of x and the corresponding column of y. x and y must have the same number of columns.

    If what="bestright", we return a data frame of size ncol(x) by 3, with the $i$th row being the maximum correlation between column $i$ of x and a column of y, and then the y-column index and y-column name with that correlation. (In case of ties, we give the first one.)

    If what="bestpairs", we return a data frame with five columns, containing all pairs of columns (with one in x and one in y) with correlation $\ge$ corthresh. Each row corresponds to a column pair, and contains the correlation and then the x- and y-column indices followed by the x- and y-column names.

    If what="all", the output is a matrix of size ncol(x) by ncol(y), with all correlations between columns of x and columns of y.

Details

Missing values (NA) are ignored, and we calculate the correlation using all complete pairs, as in cor with use="pairwise.complete.obs".

See Also

distee, findCommonID

Examples

Run this code
# a variance matrix
V <- diag(rep(0.5, 5)) + 0.5
D <- chol(V)

# simulate two correlated matrices
x <- matrix(rnorm(100), ncol=5)y <- matrix(rnorm(100), ncol=5)
# create shuffled version of the second matrix
u <- sample(1:ncol(y))
z <- y[,u]

# correlations with paired columns
corbetw2mat(x, y)

# the same with y columns shuffled
corbetw2mat(x, z)

# for each column x, find column of y with max correlation
corbetw2mat(x, y, what="bestright")

# the same with y columns shuffled 
corbetw2mat(x, z, what="bestright")


# all pairs of columns with correlation >= 0.6
corbetw2mat(x, y, what="bestpairs", corthresh=0.6)

# the same with y columns shuffled 
corbetw2mat(x, z, what="bestpairs", corthresh=0.6)

# all correlations
corbetw2mat(x, y, what="all")

Run the code above in your browser using DataLab