Created
September 2, 2025 16:07
-
-
Save abikoushi/749a305326fa9609ec90c32b2a959a7a to your computer and use it in GitHub Desktop.
Plot p-value function with multiple comparisons
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(ggplot2) | |
| library(tidyr) | |
| library(dplyr) | |
| X = matrix(0,10,10) | |
| set.seed(1234) | |
| X[,1:5] = rnorm(50, 0) | |
| X[,6:10] = rnorm(50, 2) | |
| colwise_ttest <- function(X, mu, method=NULL){ | |
| pv = apply(X, 2, function(x)t.test(x, mu = mu, conf.int=FALSE)$p.value) | |
| p.adjust(pv, method = method) | |
| } | |
| z = seq(-4, 4, length.out=201) | |
| res_pv = sapply(z, function(mu0){colwise_ttest(X, mu0)}) | |
| df_pv = pivot_longer(data.frame(t(res_pv), mu=z), cols = 1:10, | |
| names_prefix = "X") %>% | |
| mutate(name=factor(as.integer(name))) | |
| p = ggplot(df_pv, aes(x=mu,y=value,group=name))+ | |
| geom_line()+ | |
| facet_grid(row = vars(name))+ | |
| theme_bw(18) | |
| ggsave("pv_m.png", plot = p, width = 8, height = 12) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment