Created
September 10, 2018 19:07
-
-
Save drsimonj/9225815822a761de7ade0e3c03a96ad0 to your computer and use it in GitHub Desktop.
Putting spearman and pearson correlations in upper/lower triangles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(corrr) | |
d <- mtcars | |
stretch_triangle <- function(cordf, upper) { | |
cordf %>% shave(upper = upper) %>% stretch(na.rm = TRUE) | |
} | |
spearman <- correlate(d, method = "spearman") %>% stretch_triangle(upper = FALSE) | |
#> | |
#> Correlation method: 'spearman' | |
#> Missing treated using: 'pairwise.complete.obs' | |
pearson <- correlate(d, method = "pearson") %>% stretch_triangle(upper = TRUE) | |
#> | |
#> Correlation method: 'pearson' | |
#> Missing treated using: 'pairwise.complete.obs' | |
pearson %>% bind_rows(spearman) %>% spread(y, r) %>% select(-x) %>% as_cordf() | |
#> # A tibble: 11 x 12 | |
#> rowname am carb cyl disp drat gear hp | |
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> | |
#> 1 am NA 0.0575 -0.522 -0.624 0.687 0.794 -0.362 | |
#> 2 carb -0.0644 NA 0.580 0.540 -0.125 0.115 0.733 | |
#> 3 cyl -0.523 0.527 NA 0.902 -0.700 -0.493 0.832 | |
#> 4 disp -0.591 0.395 0.928 NA -0.710 -0.556 0.791 | |
#> 5 drat 0.713 -0.0908 -0.679 -0.684 NA 0.700 -0.520 | |
#> 6 gear 0.808 0.274 -0.564 -0.594 0.745 NA -0.331 | |
#> 7 hp -0.243 0.750 0.902 0.851 -0.449 -0.126 NA | |
#> 8 mpg 0.600 -0.551 -0.852 -0.848 0.681 0.480 -0.776 | |
#> 9 qsec -0.230 -0.656 -0.572 -0.460 0.0919 -0.213 -0.667 | |
#> 10 vs 0.168 -0.570 -0.814 -0.724 0.447 0.206 -0.752 | |
#> 11 wt -0.692 0.428 0.858 0.898 -0.750 -0.583 0.775 | |
#> # ... with 4 more variables: mpg <dbl>, qsec <dbl>, vs <dbl>, wt <dbl> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment