library(modelsummary)
library(modelsummary)
# clean variable names (base R)
dat <- mtcars[, c("mpg", "hp")]
colnames(dat) <- c("Miles / Gallon", "Horse Power")
datasummary_correlation(dat)
Miles / Gallon | Horse Power | |
---|---|---|
Miles / Gallon | 1 | . |
Horse Power | -.78 | 1 |
# clean variable names (tidyverse)
library(tidyverse)
dat <- mtcars %>%
select(`Miles / Gallon` = mpg,
`Horse Power` = hp)
datasummary_correlation(dat)
Miles / Gallon | Horse Power | |
---|---|---|
Miles / Gallon | 1 | . |
Horse Power | -.78 | 1 |
# alternative methods
datasummary_correlation(dat, method = "pearspear")
Miles / Gallon | Horse Power | |
---|---|---|
Miles / Gallon | 1 | -.78 |
Horse Power | -.89 | 1 |
# custom function
cor_fun <- function(x) cor(x, method = "kendall")
datasummary_correlation(dat, method = cor_fun)
Miles / Gallon | Horse Power | |
---|---|---|
Miles / Gallon | 1.00 | -.74 |
Horse Power | -.74 | 1.00 |
# rename columns alphabetically and include a footnote for reference
note <- sprintf("(%s) %s", letters[1:ncol(dat)], colnames(dat))
note <- paste(note, collapse = "; ")
colnames(dat) <- sprintf("(%s)", letters[1:ncol(dat)])
datasummary_correlation(dat, notes = note)
(a) | (b) | |
---|---|---|
(a) Miles / Gallon; (b) Horse Power | ||
(a) | 1 | . |
(b) | -.78 | 1 |
# `datasummary_correlation_format`: custom function with formatting
dat <- mtcars[, c("mpg", "hp", "disp")]
cor_fun <- function(x) {
out <- cor(x, method = "kendall")
datasummary_correlation_format(
out,
fmt = 2,
upper_triangle = "x",
diagonal = ".")
}
datasummary_correlation(dat, method = cor_fun)
mpg | hp | disp | |
---|---|---|---|
mpg | . | x | x |
hp | -.74 | . | x |
disp | -.77 | .67 | . |
# use kableExtra and psych to color significant cells
library(psych)
library(kableExtra)
dat <- mtcars[, c("vs", "hp", "gear")]
cor_fun <- function(dat) {
# compute correlations and format them
correlations <- data.frame(cor(dat))
correlations <- datasummary_correlation_format(correlations, fmt = 2)
# calculate pvalues using the `psych` package
pvalues <- psych::corr.test(dat)$p
# use `kableExtra::cell_spec` to color significant cells
for (i in 1:nrow(correlations)) {
for (j in 1:ncol(correlations)) {
if (pvalues[i, j] < 0.05 && i != j) {
correlations[i, j] <- cell_spec(correlations[i, j], background = "pink")
}
}
}
return(correlations)
}
# The `escape=FALSE` is important here!
datasummary_correlation(dat, method = cor_fun, escape = FALSE)
vs | hp | gear | |
---|---|---|---|
vs | 1.00 | -.72 | .21 |
hp | -.72 | 1.00 | -.13 |
gear | .21 | -.13 | 1.00 |