Learn R Programming

scrime (version 1.3.5)

rowCors: Rowwise Correlation with a Vector

Description

Computes Pearson's correlation coefficient of a vector with each row of a matrix.

Usage

rowCors(X, y, trendStat = FALSE, use.n = NULL)

Arguments

X

a numeric matrix in which each row represents a variable and each column an observation.

y

a numeric vector of length ncol(X).

trendStat

instead of the correlation coefficients should the values of the statistic for a test of linear trend based on this coefficient be returned? If TRUE, then it is assumed that all variables in X and the variable represented by y are ordinal, and the values in X and y represent scores for the different levels.

use.n

should the squared values of the correlation coefficient be multiplied by ncol(X)? Ignored if trendStat = FALSE. If FALSE, the squared values are multiplied by ncol(X) - 1. By default, the squared values are multiplied by ncol(X) if y shows two levels, leading to the Cochran-Armitage test of trend. Otherwise, they are multiplied by ncol(X) - 1.

Value

A vector containing the rowwise values of Pearson's correlation coefficient (if trendStat = FALSE or the rowwise values of the trend statistics (if trendStat = TRUE.

References

Agresti, A.\ (2002). Categorical Data Analysis. Wiley, Hoboken, NJ. 2nd Edition.

See Also

rowTrendStats, rowCATTs, rowMsquares

Examples

Run this code
# NOT RUN {
# Generate a random matrix containing 10 continuous variables
# and a vector representing a continuous variable.

mat <- matrix(runif(200, 0, 20), 10)
y <- sample(runif(20, 0, 20))

# The correlations between y and each of row of mat are
# computed by

rowCors(mat, y)

# Generate a random binary vector and a matrix consisting
# of 10 ordinal variables with levels 0, 1, 2, where these 
# values can be interpreted as scores for the differ 
# categories.

mat <- matrix(sample(0:2, 500, TRUE), 10)
y <- sample(0:1, 50, TRUE)

# The values of the Cochran-Armitage trend statistic are
# computed by

rowCors(mat, y, trendStat = TRUE)

# If the values of the general test of linear trend described
# on page 87 of Agresti (2002) should be computed, then call

rowCors(mat, y, trendStat = TRUE, use.n = FALSE) 

# }

Run the code above in your browser using DataLab