A correlation matrix with asterisks and the option to mix Pearson and Spearman correlations in R
corstars
is an R function that computes a correlation matrix for a given dataset, supporting both Pearson and Spearman correlations. Specific indices can be used to denote different correlation methods.
corstars(x, ordinal_vars=NULL, removeTriangle=c("upper", "lower"),
result=c("none", "html", "latex"), indecies=c(pearson="", spearman="(s)"))
x
: Your data matrix.ordinal_vars
: Vector of column indices for ordinal variables that require Spearman correlation.removeTriangle
: Specifies whether to remove the upper or lower triangle of the correlation matrix. Options include"upper"
or"lower"
.result
: Specifies the output format. Options include"none"
,"html"
, and"latex"
."none"
returns the result as a data frame, whereas"html"
and"latex"
print the result in the corresponding formats.indecies
: Named list containing specific indices for Pearson and Spearman correlations, e.g.c(pearson="", spearman="(s)")
.
Here's an example of how to use the function with a dataset that contains both continuous and ordinal variables:
# Simulate some data with multiple continuous and ordinal variables
set.seed(123)
data <- data.frame(
continuous_var1 = rnorm(100),
continuous_var2 = rnorm(100),
continuous_var3 = rnorm(100),
ordinal_var1 = sample(1:5, 100, replace = TRUE),
ordinal_var2 = sample(1:5, 100, replace = TRUE)
)
# Use the corstars function, specifying that the fourth and fifth columns are ordinal
result <- corstars(data, ordinal_vars = c(4, 5), removeTriangle = "upper", result = "none")
# Print the result
print(result)