cor.matrix: Correlation Matrix

View source: R/cor.matrix.R

cor.matrixR Documentation

Correlation Matrix

Description

This function computes a correlation matrix based on Pearson product-moment correlation coefficient, Spearman's rank-order correlation coefficient, Kendall's Tau-b correlation coefficient, Kendall-Stuart's Tau-c correlation coefficient, tetrachoric correlation coefficient, or polychoric correlation coefficient and computes significance values (p-values) for testing the two-sided hypothesis H0: \rho = 0 for all pairs of variables. Statistically significant correlations can be highlighted by specifying the argument color.

Usage

cor.matrix(data, ...,
           method = c("pearson", "spearman", "kendall-b", "kendall-c", "tetra", "poly"),
           group = NULL, exact = FALSE, continuity = TRUE, ml = FALSE,
           print = c("all", "cor", "n", "stat", "df", "p"),
           tri = c("both", "lower", "upper"), alpha = 0.05,
           color = "default", style = c("regular", "bold", "italic"),
           p.adj = c("none", "bonferroni", "holm", "hochberg", "hommel",
                     "BH", "BY", "fdr"),
           na.omit = FALSE, digits = 2, p.digits = 3, as.na = NULL,
           write = NULL, append = TRUE, check = TRUE, output = TRUE)

Arguments

data

a data frame with numeric variables, i.e., factors and character variables are excluded from data before conducting the analysis.

...

an expression indicating the variable names in data, e.g., cor.matrix(dat, x1, x2, x3). Note that the operators +, -, ~, :, ::, and ! can also be used to select variables, see 'Details' in the df.subset function.

method

a character vector indicating which correlation coefficient is to be computed, i.e. "pearson" for Pearson product-moment correlation coefficient (default), "spearman" for Spearman's rank-order correlation coefficient, "kendall-b" for Kendall's Tau-b correlation coefficient, "kendall-c" for Kendall-Stuart's Tau-c correlation coefficient, "tetra" for tetrachoric correlation coefficient, and "poly" for polychoric correlation coefficient.

group

either a character string indicating the variable name of the grouping variable in data, or a vector representing the grouping variable. Note that the grouping variable is limited to two groups.

exact

logical: if TRUE (default), an exact p-value is computed for Spearman's rank-order correlation coefficient and Kendall's Kendall's Tau-b correlation coefficient. Note that the exact p-value is not available in the presence of ties.

continuity

logical: if TRUE (default), continuity correction is used for testing Spearman's rank-order correlation coefficient and Kendall's Tau-b correlation.

ml

logical: if FALSE (default), a two-step approximation is used to compute the tetrachoric and polychoric correlation coefficient, while the maximum-likelihood (ML) estimate is computed if TRUE. Note that ML estimation is computationally expensive, i.e., takes a lot of time.

print

a character string or character vector indicating which results to show on the console, i.e. "all" for all results, "cor" for correlation coefficients, "n" for the sample sizes, "stat" for the test statistic, "df" for the degrees of freedom, and "p" for p-values.

tri

a character string indicating which triangular of the matrix to show on the console, i.e., both for upper and lower triangular, lower (default) for the lower triangular, and upper for the upper triangular.

alpha

a numeric value between 0 and 1 indicating the significance level at which correlation coefficients are are highlighted when specifying the argument color.

color

a character string indicating the text color for highlighting statistically significant correlation coefficients , i.e., "default" (default) for the default text color without color coding and various text colors for highlighting like "red", "b.red", "green", "b.green", "blue", or "b.blue", see the help page of the chr.color function. Note that this option is not supported when using R Markdown and when writing the output into a text file (.txt).

style

a character vector indicating the font style for statistically significant correlation coefficients, i.e., "regular" (default) for regular text, "bold" for bold text, and "italic" for italic text. Note that the font style "bold" and "italic" can be combined, i.e., style = c("bold", "italic") provides a bold and italic text. Note that the argument color needs to be specified to change the style of the text, e.g. color = "black" and style = "bold" to for bold text.

p.adj

a character string indicating an adjustment method for multiple testing based on p.adjust, i.e., none , bonferroni, holm (default), hochberg, hommel, BH, BY, or fdr.

na.omit

logical: if TRUE, incomplete cases are removed before conducting the analysis (i.e., listwise deletion); if FALSE (default), pairwise deletion is used.

digits

an integer value indicating the number of decimal places to be used for displaying correlation coefficients.

p.digits

an integer value indicating the number of decimal places to be used for displaying p-values.

as.na

a numeric vector indicating user-defined missing values, i.e. these values are converted to NA before conducting the analysis.

write

a character string naming a file for writing the output into either a text file with file extension ".txt" (e.g., "Output.txt") or Excel file with file extension ".xlsx" (e.g., "Output.xlsx"). If the file name does not contain any file extension, an Excel file will be written.

append

logical: if TRUE (default), output will be appended to an existing text file with extension .txt specified in write, if FALSE existing text file will be overwritten.

check

logical: if TRUE (default), argument specification is checked.

output

logical: if TRUE (default), output is shown on the console.

Value

Returns an object of class misty.object, which is a list with following entries:

call

function call

type

type of analysis

data

data frame used for the current analysis

args

specification of function arguments

result

list with result tables, i.e., cor for the correlation matrix, n for a matrix with the sample sizes, stat for a matrix with the test statistics, df for a matrix with the degrees of freedom, and p-value for the matrix with the significance values (p-values)

Note

This function uses a modified copy of the polychor() function in the polycor package by John Fox when requesting tetrachoric or polychoric correlation coefficients.

Author(s)

Takuya Yanagida takuya.yanagida@univie.ac.at

References

Rasch, D., Kubinger, K. D., & Yanagida, T. (2011). Statistics in psychology - Using R and SPSS. John Wiley & Sons.

Revelle, W. (2018) psych: Procedures for personality and psychological research. Northwestern University, Evanston, Illinois, USA, https://CRAN.R-project.org/package=psych Version = 1.8.12.

See Also

write.result, cohens.d, effsize, multilevel.cor, multilevel.icc, na.auxiliary

Examples

## Not run: 

#————————————————————————————————————————————————————————————————————————————
# Pearson Product-Moment Correlation Coefficient

# Example 1a: Pearson product-moment correlation matrix using pairwise deletion
cor.matrix(airquality, Ozone:Wind)

# Example 1b:  Alternative specification without using the '...' argument
cor.matrix(airquality[, c("Ozone", "Solar.R", "Wind")])

# Example 2a: Highlight statistically significant result in bright red
cor.matrix(airquality, Ozone, Solar.R, Wind, color = "b.red")

# Example 2b: Highlight statistically significant result in boldface
cor.matrix(airquality, Ozone, Solar.R, Wind, color = "black", style = "bold")

# Example 3a: Print sample size, degrees of freedom, and significance values
cor.matrix(airquality, Ozone, Solar.R, Wind, print = "all")

# Example 3b: Listwise deletion
cor.matrix(airquality, Ozone, Solar.R, Wind, na.omit = TRUE)

# Example 3c: Significance values with Bonferroni correction
cor.matrix(airquality, Ozone, Solar.R, Wind, print = "all", p.adj = "bonferroni")

#————————————————————————————————————————————————————————————————————————————
# Spearman's Rank-Order Correlation Coefficient and Kendall's Tau

# Example 4a: Spearman's rank-order correlation matrix
cor.matrix(airquality, Ozone, Solar.R, Wind, method = "spearman")

# Example 4b: Kendall's Tau-c
cor.matrix(airquality, Ozone, Solar.R, Wind, method = "kendall-c")

#————————————————————————————————————————————————————————————————————————————
# Tetrachoric and Polychoric Correlation Coefficient

# Example 5a: Tetrachoric correlation matrix
cor.matrix(data.items, +ditem, method = "tetra")

# Example 5b: Polychoric correlation matrix
cor.matrix(data.items, +pitem, method = "poly")

#————————————————————————————————————————————————————————————————————————————
# Grouping Variable

# Example 6a: Results for group 'vs' = "0" and "1" separately
cor.matrix(mtcars, mpg:disp, group = "vs")

# Example 6b: Alternative specification without using the '...' argument
cor.matrix(mtcars[, c("mpg", "cyl", "disp")], group = mtcars$vs)

#————————————————————————————————————————————————————————————————————————————
# Write Results

# Example 7a: Write Results into a text file
cor.matrix(airquality, Ozone, Solar.R, Wind, print = "all", write = "Correlation.txt")

# Example 7b: Write Results into an Excel file
cor.matrix(airquality, Ozone, Solar.R, Wind, print = "all", write = "Correlation.xlsx")

## End(Not run)

misty documentation built on Aug. 2, 2026, 9:06 a.m.

Related to cor.matrix in misty...