I have a correlation matrix that includes bivariate correlations among 14 variables. How can I append asterisks to denote statistical significance? I am using the following code:
pretty.matrix<-a %>%
correlate() %>%
shave() %>%
fashion() %>%
print()
You could use a function that prints the statistical significance. Using the colpair_map
it will be easy to make a pretty matrix. I used the mtcars dataset as example. You can use the code below:
library(corrr)
library(tidyverse)
# Function
calc_p_value <- function(vec_a, vec_b, sig_level){
test_res <- cor.test(vec_a, vec_b)
sig <- if_else(test_res$p.value < sig_level, "*", "")
paste0(round(cor.test(vec_a, vec_b)$estimate, 2), sig)
}
# Matrix with p = 0.05
colpair_map(mtcars, calc_p_value, 0.05) %>%
shave()
Output looks like this:
# A tibble: 11 × 12
term mpg cyl disp hp drat wt qsec vs am gear carb
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 mpg NA NA NA NA NA NA NA NA NA NA NA
2 cyl -0.85* NA NA NA NA NA NA NA NA NA NA
3 disp -0.85* 0.9* NA NA NA NA NA NA NA NA NA
4 hp -0.78* 0.83* 0.79* NA NA NA NA NA NA NA NA
5 drat 0.68* -0.7* -0.71* -0.45* NA NA NA NA NA NA NA
6 wt -0.87* 0.78* 0.89* 0.66* -0.71* NA NA NA NA NA NA
7 qsec 0.42* -0.59* -0.43* -0.71* 0.09 -0.17 NA NA NA NA NA
8 vs 0.66* -0.81* -0.71* -0.72* 0.44* -0.55* 0.74* NA NA NA NA
9 am 0.6* -0.52* -0.59* -0.24 0.71* -0.69* -0.23 0.17 NA NA NA
10 gear 0.48* -0.49* -0.56* -0.13 0.7* -0.58* -0.21 0.21 0.79* NA NA
11 carb -0.55* 0.53* 0.39* 0.75* -0.09 0.43* -0.66* -0.57* 0.06 0.27 NA
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.