Skip to content

Instantly share code, notes, and snippets.

@njudd
Created October 31, 2022 21:53
Show Gist options
  • Save njudd/a1e9db2408d8562f8d43177abd028d05 to your computer and use it in GitHub Desktop.
Save njudd/a1e9db2408d8562f8d43177abd028d05 to your computer and use it in GitHub Desktop.
Correlation Mat's and their p-vals by group
# No need to call Bill Gates!
# here is how you get correlation tables by group with their p-val matries and even plot them
library(Hmisc); library(tidyverse); library(corrplot)
result <- iris |> # base R pipe
#select() # remove col's you don't want in the correlation matrix
group_by(Species) |> # grouping col
group_map(~rcorr(as.matrix(.))) |>
setNames(unique(sort(iris$Species)))
# rcorr(as.matrix(iris[iris$Species == "virginica",1:4])) #checking that the naming of lists worked
## Let's make some plots!
setosa_plt <- corrplot(result$setosa$r, p.mat = result$setosa$P, method = 'color', diag = FALSE, type = 'upper',
sig.level = c(0.001, 0.01, 0.05), pch.cex = 0.9, tl.col = "black",
insig = 'label_sig', pch.col = 'grey20', order = 'AOE')
virginica_plt <- corrplot(result$virginica$r, p.mat = result$virginica$P, method = 'color', diag = FALSE, type = 'upper',
sig.level = c(0.001, 0.01, 0.05), pch.cex = 0.9, tl.col = "black",
insig = 'label_sig', pch.col = 'grey20', order = 'AOE')
versicolor_plt <- corrplot(result$versicolor$r, p.mat = result$versicolor$P, method = 'color', diag = FALSE, type = 'upper',
sig.level = c(0.001, 0.01, 0.05), pch.cex = 0.9, tl.col = "black",
insig = 'label_sig', pch.col = 'grey20', order = 'AOE')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment