-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselection_bias_demonstration.R
72 lines (49 loc) · 2.19 KB
/
selection_bias_demonstration.R
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
#### Simulate distribution of correlation after selection ####
getRejectedDistribution <- function (resels,
n.subjects,
rho,
error.rate,
error.type='BH') {
rho.sd<- 1 / sqrt(n.subjects-3)
rho.z<- atanh(rho)
correlations<- tanh(rnorm(resels, mean=rho.z, sd=rho.sd ))
pvals<- 2 * pnorm(abs(atanh(correlations)), sd=rho.sd, lower.tail=FALSE)
adjusted.pvals<- p.adjust(pvals, method=error.type)
rejection.indicator<- adjusted.pvals < error.rate
result<- correlations[rejection.indicator]
return(result)
}
## Testing:
# .testing<- getRejectedDistribution(resels=111, n.subjects=16, rho=0.2, error.rate=0.5)
# mean(.testing)
# Minimal number of resels was computed from Tom et al's data
# Maximal number of resels equal number of voxels
configurations<- expand.grid(
resels=round(c(5e3, 1e4, 3e5),-1),
n.subjects=floor(seq(12, 100, length.out=6)),
rho=seq(0,1, length.out=10),
error.rate=c(0.05, 0.2),
error.type='bonf', stringsAsFactors=FALSE)
## Run simulation
selected.correlations<- list()
for(i in 1: nrow(configurations)){
selected.correlations[[i]]<- do.call(getRejectedDistribution, as.list(configurations[i,]))
}
## Compute means:
selection.bias<- cbind(configurations,
mean=sapply(selected.correlations, mean, na.rm=TRUE),
median=sapply(selected.correlations, median, na.rm=TRUE),
sd=sapply(selected.correlations, sd, na.rm=TRUE),
n=sapply(selected.correlations, function(x) sum(!is.na(x))))
selection.bias$arm<- with(selection.bias, sd/sqrt(n)*2)
# To report SEs in paper:
max(selection.bias$arm, na.rm=TRUE)
## Vizualize results:
library(ggplot2)
error.rates<- unique(selection.bias$error.rate)
.subset<- subset(selection.bias, error.rate==error.rates[1])
base.plot<- ggplot(data=.subset) + xlab("True Correlation") + ylab("Mean of Selected Correlations")+
geom_point(aes(y=mean , x=rho)) +
facet_grid(resels~ n.subjects , labeller = label_both) +
geom_abline(intercept = 0, slope = 1, lty=1, col='grey')
base.plot