-
Notifications
You must be signed in to change notification settings - Fork 1
/
appendix_figures.Rmd
172 lines (155 loc) · 5.63 KB
/
appendix_figures.Rmd
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
---
title: "R Notebook"
output: html_notebook
editor_options:
chunk_output_type: inline
---
```{r}
library(ggpubr)
library(ggplot2)
theme_set(theme_minimal())
library(dplyr)
library(purrr)
library(tidyr)
library(forcats)
source("funs/plot.R")
source("funs/pvalue.R")
DIR_FIGURES = "figures"
DIR_RESULTS = "data"
if (!dir.exists(DIR_FIGURES)) dir.create(DIR_FIGURES, recursive = TRUE)
```
# Within-view correction
```{r}
#' Plot examples of correlated test statistics
#'
#' @param n
#' @param l
#' @param outlier_value
#'
#' @return ggplot
plot_example = function(n, l, outlier_value) {
n_null = 10
sample_test_stat = make_sample_test_stat(n, l)
t0 = sample_test_stat()
t0[1] = outlier_value
t_null = sample_test_stat(n_null)
df = data.frame(i=1:n,
x=t0)
df_surrogates = t_null %>%
as.data.frame() %>%
mutate(i=1:n) %>%
pivot_longer(-i)
gg_surrogates = geom_line(data=df_surrogates, aes(i, value, group=name), col="grey", alpha=0.5)
ggplot(df) +
gg_surrogates +
geom_line(aes(i, x), lwd=0.5) +
theme_classic() +
theme(axis.line.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
labs(y="", x="")
}
#' Wrapper for making a Gaussian process prior
#'
#' @param n
#' @param l
#'
#' @return function
make_sample_test_stat <- function(n, l) {
gp = train_gp(1:n, kern = function(x,y) kernel_sqexp(x,y,l=l))
function(n=1) gp$prior(n)
}
exp_correlated = readRDS(file.path(DIR_RESULTS, "exp_correlated.rds"))
fig_correlated_examples = ggarrange(plot_example(1000, 1, 3),
plot_example(1000, 10, 3),
plot_example(1000, 100, 3), ncol=1)
df_exp_correlated = exp_correlated %>%
pivot_longer(-c(1,2)) %>%
filter(n==1000) %>%
mutate(name = gsub("pv\\.", "", name),
name = fct_reorder(name, -value, median))
fig_correlated = ggplot(df_exp_correlated) + theme_minimal()+
geom_line(aes(l, value, color=name, linetype=name), alpha=0.8) +
geom_point(aes(l, value, color=name, shape=name), alpha=0.6) +
scale_color_manual(values = c("minp"="black", "raw"="grey","bonf"="#1b9e77", "BH"="#7570b3", "BY"="#d95f02"),
labels = c("minp"="minP", "raw"="None","bonf"="Bonferroni", "BH"="BH", "BY"="BY")) +
scale_shape_manual(values = c("minp"=1, "raw"=4, "bonf"=3, "BH"=2, "BY"=5),
labels = c("minp"="minP", "raw"="None","bonf"="Bonferroni", "BH"="BH", "BY"="BY")) +
scale_linetype_manual(values = c("minp"=1, "raw"=2, "bonf"=2, "BH"=2, "BY"=2),
labels = c("minp"="minP", "raw"="None","bonf"="Bonferroni", "BH"="BH", "BY"="BY")) +
scale_x_log10() +
scale_y_log10() +
expand_limits(y=1)+
labs(x="GP length scale", y="", color="",shape="",linetype="") +
theme(legend.position = "bottom")
fig_correlated_both = ggarrange(fig_correlated + labs(tag = "(a)") + theme(plot.tag.position = "bottom"),
fig_correlated_examples + labs(tag = "(b)") + theme(plot.tag.position = "bottom", plot.tag = element_text(face="plain")),
nrow=1)
fig_correlated_both
ggsave(file.path(DIR_FIGURES, "exp_correlated_both.pdf"), fig_correlated_both, width = 8, height = 4)
```
# FWER investing
```{r}
get_alpha_df = function(p, alpha0) {
get_alpha_bt = function(a, a_b, R) (a_b - a*(1-R)) / (1-a*(1-R))
get_alpha_t = function(a_b) a_b/2
t = length(p)
alpha_t = numeric(t)
alpha_bt = numeric(t+1)
alpha_bt[1] = alpha0
for (i in 1:t) {
alpha_t[i] = get_alpha_t(alpha_bt[i])
alpha_bt[i+1] = get_alpha_bt(alpha_t[i], alpha_bt[i], p[i] <= alpha_t[i])
}
data.frame(t=1:t, p_t = p, alpha_t=alpha_t, alpha_bt=alpha_bt[1:t])
}
plot_alphas = function(p, alpha0=.1) {
alpha_bonf = alpha0/length(p)
df = get_alpha_df(p, alpha=alpha0)
ggplot(df) +
geom_point(aes(t, alpha_bt))+
geom_line(aes(t, alpha_bt)) +
geom_hline(aes(yintercept=alpha_bonf), lty=3) +
scale_y_continuous(limits=c(0, alpha0))
}
plot_alphas_list = function(P, alpha0=.1) {
t = length(P[[1]])
alpha_bonf = alpha0/t
dfs = lapply(1:length(P), function(i) cbind(case=names(P)[[i]], get_alpha_df(P[[i]], alpha=alpha0)))
df_all = do.call(rbind, dfs)
ggplot(df_all) +
geom_point(aes(t, alpha_bt, col=case, shape=case))+
geom_line(aes(t, alpha_bt, col=case)) +
geom_hline(aes(yintercept=alpha_bonf), lty=3) +
scale_y_continuous(limits=c(0, alpha0)) +
scale_x_continuous(breaks=1:t) +
theme(legend.position = "bottom",
legend.direction = "vertical") +
labs(x="Iteration (t)", y="alpha", color="", shape="")
}
```
```{r}
t = 8
p1 = rep(0, t)
p2 = rep(1, t)
p3 = c(rep(0, floor(t/2)), rep(1, ceiling(t/2)))
p4 = c(rep(1, floor(t/2)), rep(0, ceiling(t/2)))
P1 = list(`Only discoveries`=p1,
`No discoveries`=p2)
P2 = list(`Only discoveries, then no discoveries`=p3,
`No discoveries, then only discoveries`=p4)
fig1 = plot_alphas_list(P1) +
scale_color_manual(values=c("black", "grey"),
limits=c(names(P1)[1], names(P1)[2])) +
scale_shape_manual(values=c(1, 4),
limits=c(names(P1)[1], names(P1)[2]))
fig2 = plot_alphas_list(P2)+
scale_color_manual(values=c("black", "grey"),
limits=c(names(P2)[1], names(P2)[2])) +
scale_shape_manual(values=c(1, 4),
limits=c(names(P2)[1], names(P2)[2]))
fig_fwer = ggarrange(fig1 + labs(tag="(a)") + theme(plot.tag.position = "bottom"),
fig2 + labs(tag="(b)") + theme(plot.tag.position = "bottom"))
fig_fwer
ggsave(file.path(DIR_FIGURES, "fwer.pdf"), fig_fwer, height=4, width=7)
```