Learn R Programming

misty (version 0.5.0)

cor.matrix: 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, or Kendall-Stuart's Tau-c correlation coefficient and computes significance values (p-values) for testing the hypothesis H0: \(\rho\) = 0 for all pairs of variables.

Usage

cor.matrix(x, method = c("pearson", "spearman", "kendall-b", "kendall-c"),
           na.omit = FALSE, group = NULL, sig = FALSE, alpha = 0.05,
           print = c("all", "cor", "n", "stat", "df", "p"),
           tri = c("both", "lower", "upper"),
           p.adj = c("none", "bonferroni", "holm", "hochberg", "hommel",
                     "BH", "BY", "fdr"), continuity = TRUE,
           digits = 2, p.digits = 3, as.na = NULL,
           write = NULL, check = TRUE, output = TRUE)

Value

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

call

function call

type

type of analysis

data

matrix or data frame specified in x

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)

Arguments

x

a matrix or data frame.

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 or kendall-c for Kendall-Stuart's Tau-c correlation coefficient.

na.omit

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

group

a numeric vector, character vector of factor as grouping variable to show results for each group separately, i.e., upper triangular for one group and lower triangular for another group. Note that the grouping variable is limited to two groups.

sig

logical: if TRUE, statistically significant correlation coefficients are shown in boldface on the console.

alpha

a numeric value between 0 and 1 indicating the significance level at which correlation coefficients are printed boldface when sig = TRUE.

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.

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.

continuity

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

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 for writing the results into a Excel file naming a file with or without file extension '.xlsx', e.g., "Results.xlsx" or "Results".

check

logical: if TRUE, argument specification is checked.

output

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

Author

Takuya Yanagida takuya.yanagida@univie.ac.at

Details

Note that unlike the cor.test function, this function does not compute an exact p-value for Spearman's rank-order correlation coefficient or Kendall's Tau-b correlation coefficient, but uses the asymptotic t approximation.

Statistically significant correlation coefficients can be shown in boldface on the console when specifying sig = TRUE. However, this option is not supported when using R Markdown, i.e., the argument sig will switch to FALSE.

References

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

See Also

write.result, cohens.d, cor.cont, cor.cramer, multilevel.icc, cor.phi, na.auxiliary, size.cor.

Examples

Run this code
dat <- data.frame(group = c("a", "a", "a", "a", "a",
                            "b", "b", "b", "b", "b"),
                  x = c(5, NA, 6, 4, 6, 7, 9, 5, 8, 7),
                  y = c(3, 3, 5, 6, 7, 4, 7, NA, NA, 8),
                  z = c(1, 3, 1, NA, 2, 4, 6, 5, 9, 6))

# Pearson product-moment correlation coefficient
cor.matrix(dat[, c("x", "y")])

# Pearson product-moment correlation coefficient matrix using pairwise deletion
cor.matrix(dat[, c("x", "y", "z")])

# Spearman's rank-order correlation matrix using pairwise deletion
cor.matrix(dat[, c("x", "y", "z")], method = "spearman")

# Kendall's Tau-b correlation matrix using pairwise deletion
cor.matrix(dat[, c("x", "y", "z")], method = "kendall-b")

# Kendall-Stuart's Tau-c correlation matrix using pairwise deletion
cor.matrix(dat[, c("x", "y", "z")], method = "kendall-c")

# Pearson product-moment correlation coefficient matrix using pairwise deletion
# highlight statistically significant result at alpha = 0.05
cor.matrix(dat[, c("x", "y", "z")], sig = TRUE)

# Pearson product-moment correlation coefficient matrix using pairwise deletion
# highlight statistically significant result at alpha = 0.05
cor.matrix(dat[, c("x", "y", "z")], sig = TRUE, alpha = 0.10)

# Pearson product-moment correlation coefficient matrix using pairwise deletion,
# print sample size and significance values
cor.matrix(dat[, c("x", "y", "z")], print = "all")

# Pearson product-moment correlation coefficient matrix using listwise deletion,
# print sample size and significance values
cor.matrix(dat[, c("x", "y", "z")], na.omit = TRUE, print = "all")

# Pearson product-moment correlation coefficient matrix using listwise deletion,
# print sample size and significance values with Bonferroni correction
cor.matrix(dat[, c("x", "y", "z")], na.omit = TRUE, print = "all", p.adj = "bonferroni")

# Pearson product-moment correlation coefficient using pairwise deletion,
# results for group "a" and "b" separately
cor.matrix(dat[, c("x", "y")], group = dat$group)

# Pearson product-moment correlation coefficient matrix using pairwise deletion,
# results for group "a" and "b" separately
cor.matrix(dat[, c("x", "y", "z")], group = dat$group, print = "all")

if (FALSE) {
# Write Results into a Excel file
cor.matrix(dat[, c("x", "y", "z")], print = "all", write = "Correlation.xlsx")

result <- cor.matrix(dat[, c("x", "y", "z")], print = "all", output = FALSE)
write.result(result, "Correlation.xlsx")
}

Run the code above in your browser using DataLab