learners
learners

Reputation: 235

P-values heatmap - Representation of all pairwise comparisons/p-values with R (e.g. like a colored correlation matrix)

I have a lot of pairwise comparisons; it is very difficult to visualize or add all the p values ​​in a report. I was wondering if there was a function in R that could represent well the pairwise comparisons/all the p values.

Something like this :

enter image description here

But instead of having the values of the correlation in the small squares we would have the p values?

Thank you

******* EDITED/SOLUTION *********

Solution (thanks to @Park) when using emmeans package for pairwise comparisons :

p.val.test<-pwpm(emmeans(your_model, "your_factor"),means = FALSE, flip = TRUE,reverse = TRUE) # p-values presented compactly in matrix form
p.val.test<-sub("[<>]", "", p.val.test)
p.matx<-matrix(as.numeric((p.val.test)),nrow = length(p.val.test[,1]),ncol = length(p.val.test[,1])) #if your factor has 5 levels ncol and nrow=5
rownames(p.matx) <- colnames(p.matx) <-colnames(p.val.test)
p.matx[upper.tri(p.matx, diag=FALSE)] <- NA
melt(p.matx) %>%
  ggplot(aes(Var1, Var2, fill = value)) + geom_tile() +
  geom_text(aes(label = value))

Upvotes: 1

Views: 4477

Answers (1)

Kra.P
Kra.P

Reputation: 15123

Well, I made matrix formed pairwise simple linear regression.

dummy <- data.frame(
  x1 = c(1,2,3,4),
  x2 = c(1,0,3,4),
  x3 = c(1,0,7,4),
  x4 = c(1,0,7,-1),
  x5 = c(8,0,7,-1)
)
colNames <- names(dummy)
mat <- matrix(NA, nrow = 5, ncol = 5)
mat[lower.tri(mat)] <- combn(colNames, 2, function(x) summary(lm(dummy[x][,1] ~ dummy[x][,2]))$coefficients[2,4])

mat <- round(mat,2)
rownames(mat) = colnames(mat) = colnames(dummy)
mat

     x1   x2   x3   x4 x5
x1   NA   NA   NA   NA NA
x2 0.15   NA   NA   NA NA
x3 0.35 0.19   NA   NA NA
x4 0.96 0.80 0.27   NA NA
x5 0.45 0.88 0.75 0.33 NA

Then, use melt and geom_tile()(or geom_rect , etc) will return correlation matrix plot style p-value matrix plot

melt(mat) %>%
  ggplot(aes(Var1, Var2, fill = value)) + geom_tile() +
  geom_text(aes(label = value))

pval?

Upvotes: 4

Related Questions