-
Notifications
You must be signed in to change notification settings - Fork 3
/
permutation_testing_ev.Rmd
196 lines (177 loc) · 5.35 KB
/
permutation_testing_ev.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
---
title: "Supplementary Analysis in Early Visual Cortex Probabilistic ROIs"
output: html_notebook
---
```{r}
require(plyr)
require(dplyr)
require(stringr)
require(ggplot2)
df <-
ldply(list.files('../../results/ev-roi-class_newmask/', pattern='csv',
full.names=T),
read.csv)
df
```
```{r}
# mean the folds
df <-
df %>%
group_by(subid, roi, permutation) %>%
summarise(acc=mean(acc))
# bootstrap one permutation for each subid and roi
bstrap_perms_ <- function(df) {
df %>%
group_by(subid, roi) %>%
sample_n(1, replace=T)
}
bstrap_perms <- function(df) {
df %>%
bstrap_perms_() %>%
group_by(roi) %>%
summarise(acc=mean(acc))
}
# change this if needed
which_df <- df
generate_df_permutations <- function(df, nboots=1000, seed=343) {
df_perms <- data.frame()
set.seed(seed)
for (idx in 1:nboots) {
tmp <- bstrap_perms(df)
df_perms <- rbind(df_perms, tmp)
}
return(df_perms)
}
##### TEST --- UNCOMMENT TO RUN
# Generate a fake dataset
# require(assertthat)
# nsubjs <- 10
# nperms <- 30
# nrois <- 3
#
# null_data_roi1 <- rnorm(nsubjs * nperms, 0, 10)
# null_data_roi2 <- rnorm(nsubjs * nperms, 20, 10)
# null_data_roi3 <- rnorm(nsubjs * nperms, 30, 10)
#
# null_df <-
# data.frame(expand.grid(subid=1:nsubjs,
# permutation = 0:(nperms-1),
# roi=1:nrois),
# acc=c(null_data_roi1, null_data_roi2, null_data_roi3))
#
# # check we got the right order
# null_df %>%
# group_by(roi) %>%
# summarise(mean(acc), sd(acc))
#
# df_null_perms <- generate_df_permutations(null_df, nboots=10000)
#
# ggplot(df_null_perms) +
# geom_histogram(aes(acc), binwidth=1) +
# facet_wrap(~roi)
#
# # check that the bootstrapping works in the way it should
# bs_df <- bstrap_perms_(null_df)
# assert_that(nrow(bs_df) == nrow(null_df) / nperms)
#### #############
# get permuted dataset
if (!file.exists('df_perms.Rdata')) {
df_perms <- generate_df_permutations(which_df, nboots=10000)
save(df_perms, file='df_perms.Rdata', compress='gzip')
} else {
load('df_perms.Rdata')
}
# get original values
df_orig <-
which_df %>%
filter(permutation == 0) %>%
group_by(roi) %>%
summarise(acc=mean(acc))
unique_rois <- unique(df_orig$roi)
pval_roi <- data.frame()
for (roi_ in unique_rois) {
df_perms_roi <- df_perms %>%
filter(roi == roi_)
df_orig_roi <- df_orig %>%
filter(roi == roi_)
pval <- 1 - (sum(df_perms_roi$acc <= df_orig_roi$acc) + 1)/(nrow(df_perms_roi) + 1)
tmp <- data.frame(roi=roi_, pval=pval)
pval_roi <- rbind(pval_roi, tmp)
}
# sort plots
df_perms$roi <-
factor(df_perms$roi,
levels=c('V1v', 'V1d', 'V1',
'V2v', 'V2d', 'V2',
'V3v', 'V3d', 'V3',
'V1+V2', 'V1+V2+V3'))
df_orig$roi <-
factor(df_orig$roi,
levels=c('V1v', 'V1d', 'V1',
'V2v', 'V2d', 'V2',
'V3v', 'V3d', 'V3',
'V1+V2', 'V1+V2+V3'))
pval_roi$roi <-
factor(pval_roi$roi,
levels=c('V1v', 'V1d', 'V1',
'V2v', 'V2d', 'V2',
'V3v', 'V3d', 'V3',
'V1+V2', 'V1+V2+V3'))
df_perms$roi_lbl <- factor(str_to_title(df_perms$roi))
pval_roi$roi_lbl <- factor(str_to_title(pval_roi$roi))
df_orig$roi_lbl <- factor(str_to_title(df_orig$roi))
df_perms$roi_lbl <-
factor(df_perms$roi_lbl,
levels=str_to_title(
c('V1v', 'V1d', 'V1',
'V2v', 'V2d', 'V2',
'V3v', 'V3d', 'V3',
'V1+V2', 'V1+V2+V3')))
df_orig$roi_lbl <-
factor(df_orig$roi_lbl,
levels=str_to_title(
c('V1v', 'V1d', 'V1',
'V2v', 'V2d', 'V2',
'V3v', 'V3d', 'V3',
'V1+V2', 'V1+V2+V3')))
pval_roi$roi_lbl <-
factor(pval_roi$roi_lbl,
levels=str_to_title(
c('V1v', 'V1d', 'V1',
'V2v', 'V2d', 'V2',
'V3v', 'V3d', 'V3',
'V1+V2', 'V1+V2+V3')))
pval_roi$color_sig <-
ifelse(pval_roi$pval < 0.05, 'sig', 'ns')
pval_roi$color_sig_bf <-
ifelse(pval_roi$pval < 0.05/11, 'sig', 'ns')
```
```{r, fig.height=8, fig.width=12}
# get max xvalues
xmax <- max(df_perms$acc, df_orig$acc) - .5
plot_ev_perms <-
ggplot() +
geom_histogram(data=df_perms, aes(acc), binwidth=0.001) +
geom_vline(xintercept=.5, linetype='dashed', color='black') +
facet_wrap(~roi_lbl, ncol=3, scales='free') +
geom_vline(data=df_orig, aes(xintercept=acc),
linetype='dashed', color='blue') +
geom_text(data=df_orig, x=.45, y=400,
aes(label=paste('M =', round(acc, 4))),
hjust='left') +
geom_text(data=pval_roi, x=.45, y=350,
aes(label=paste('p =', round(pval, 4)),
color=color_sig_bf),
hjust='left') +
scale_color_manual(values=c('black', 'red')) +
guides(color=FALSE) +
theme_bw() +
labs(x='Classifier Accuracy', y='Histogram Count') +
coord_cartesian(xlim=c(.5 - xmax, .5 + xmax),
ylim=c(0, 450))
plot_ev_perms
#pdf(file='evroi-acc-perms.pdf', onefile=F, title='',
#paper='special', width=12, height=8, bg='white')
#plot_ev_perms
#dev.off()
```