Skip to contents

Creates a pretty console correlation table (by Dominik Vogel) method : correlation method. "pearson", "spearman" and "polychoric" are currently supported the results will be displayed directly in console. There is the option to save them in html or latex format or (recommended), to transform them to a flextable and export it directly to word. labels_rows and labels_cols are character vectors for labeling rows and columns. Reference for the original code. Additionally added the option to investigate polychoric correlation For clarity to be able to calculate pearson or spearman correlation an installation of the Hmisc package is required. Run install.packages("Hmisc")

Usage

corstars(
  x,
  method = c("pearson", "spearman", "polychoric"),
  removeTriangle = c("upper", "lower", FALSE),
  rmDiag = c(TRUE, FALSE),
  rmLastCol = c(TRUE, FALSE),
  result = c("none", "html", "latex"),
  labels_rows = colnames(x),
  labels_cols = labels_rows[1:length(labels_rows)],
  sig.level = 0.05,
  nod = 2
)

Arguments

x

a matrix containing the data

method

correlation method. "pearson", "spearman" or "polychoric" are supported

removeTriangle

remove upper or lower triangle, or FALSE for not removing any triangle

rmDiag

if one triangle of the matrix is removed, should the diagonal be kept = FALSE; or removed = TRUE

rmLastCol

chose if the last column can be removed, to shorten the table if necessary, default = TRUE

result

Print result in Console ("none"), generate HTML file ("html"), generate latex file ("latex")

labels_rows

Labels for the rows (i.e., variable names). Length musst be same as number of variables

labels_cols

Labels for columns. Length musst be same as number of variables - 1

sig.level

Significance level (.1 or .05). If NA is provided, no stars marking the significance will be printed. This helps formatting the decimal places. NA is especially used by the apa_corrTable function

nod

Integer. Number of Decimals. Default is nod = 2. In case of -1 a simple convention based on sample size is applied for determination of number of decimal points. See get_number_of_decimals or ?datscience::get_number_of_decimals

Value

Correlation table in console or file

Author

Dominik Vogel (Adapted by Bjoern Buedenbender)

Examples

if (FALSE) {
# Console output
corstars(mtcars,
  method = "pearson", removeTriangle = "upper", result = "none",
  caption = "Correlations",
  sig.level = 0.1,
  labels_rows = c(
    "(1) mpg", "(2) cyl", "(3) disp", "(4) hp",
    "(5) drat", "(6) wt", "(7) qsec", "(8) vs",
    "(9) am", "(10) gear",
    "(11) carb"
  ),
  labels_cols = 1:10
)

# HTML output
corstars(mtcars,
  method = "pearson", removeTriangle = "upper", result = "html",
  caption = "Correlations", filename = "corr.html",
  sig.level = 0.1,
  labels_rows = c(
    "(1) mpg", "(2) cyl", "(3) disp", "(4) hp",
    "(5) drat", "(6) wt", "(7) qsec", "(8) vs",
    "(9) am", "(10) gear",
    "(11) carb"
  ),
  labels_cols = 1:10
)
}