1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| diff_mat <- outer(vars_row, vars_col, Vectorize(function(r, c){ x <- df[[r]]; y <- df[[c]] mean(x - y, na.rm = TRUE) }))
rownames(diff_mat) <- vars_row colnames(diff_mat) <- vars_col
p_mat <- outer(vars_row, vars_col, Vectorize(function(r, c){ x <- df[[r]]; y <- df[[c]] ok <- complete.cases(x, y) if(sum(ok) < 2) return(NA_real_) t.test(x[ok], y[ok], paired = TRUE)$p.value }))
diag(p_mat) <- NA p_adj <- matrix(p.adjust(as.vector(p_mat), method = "BH"), nrow = nrow(p_mat), dimnames = dimnames(p_mat))
alpha <- 0.05 diag(diff_mat) <- NA mark_x <- ifelse(!is.na(p_adj) & p_adj < alpha, formatC(diff_mat, digits = 0, format = "f"), "×") diag(mark_x) <- ""
options(repr.plot.width=6, repr.plot.height=6) max_abs <- max(abs(diff_mat), na.rm = TRUE) pheatmap( diff_mat, cluster_rows = FALSE, cluster_cols = FALSE, main = "Mean difference (row - col)", color = colorRampPalette(rev(brewer.pal(11, "Spectral")))(101), breaks = seq(-max_abs, max_abs, length.out = 101), display_numbers = mark_x, number_color = "black", na_col = "transparent", border_color = NA, angle_col = 315 )
|