/
fig02.Rmd
executable file
·385 lines (303 loc) · 13 KB
/
fig02.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
---
title: 'A guide to robust statistical methods in neuroscience: Figure 2'
author: "Rand R. Wilcox & Guillaume A. Rousselet"
date: "`r Sys.Date()`"
output:
pdf_document:
fig_caption: no
number_sections: no
toc: yes
toc_depth: 2
---
We perform a simulation of false positives and true positives when sampling from normal and lognormal distributions. The false positive results are illustrated in panel A; the true positive (power) results are illustrated in panel B.
# Dependencies
```{r}
library(ggplot2)
library(cowplot)
library(tidyr)
library(tibble)
# library(beepr) # for simulation only
source("./code/Rallfun-v40.txt")
source("./code/theme_gar.txt")
source("./code/xtrafun.R")
```
## Counter function
Function used to print console updates during the simulation.
```{r, eval=FALSE}
sim.counter <- function(S, nsim, inc){
if(S == 1){
# print(paste(nsim,"iterations:",S))
cat(nsim,"iterations:",S)
beep(2)
}
if(S %% inc == 0){
# print(paste("iteration",S,"/",nsim))
cat(" /",S)
beep(2)
}
}
```
# Simulation: false positives
Before you commit to 20,000 simulations, try with 1,000, because it might take a long time to compute. The code will output an iteration update every 500 simulations. The simulation includes:
- multiple sample sizes;
- sampling from normal and lognormal distributions;
- t-tests using means and 20% trimmed means;
- a parametric test of medians.
## Define parameters
```{r}
nsim <- 20000 # number of simulation iterations
nvec <- c(seq(10,150,10), seq(200,400,50), seq(500,800,100)) # vector of sample sizes to test
max.size <- max(nvec)
ES <- 0 # effect size
```
## Run simulation
No need to run this chunk; the results are loaded in the next chunk for convenience. If you run your own simulation, change the name of the file at the end of the chunk, otherwise the results used in the article will be overwritten.
```{r eval = FALSE}
set.seed(45) # set random number generator for reproducibility
# population values
logn.m <- exp(0.5) # mean of standard lognormal distribution
logn.md <- exp(0) # median of standard lognormal distribution
logn.tm <- ghtrim(g=1) + logn.md # trimmed mean of lognormal distribution
# 6 conditions: normal / skewed x mean / trimmed mean / median
simres <- array(0, dim = c(6, length(nvec), nsim))
simsteps <- 500 # get a console update every simsteps iterations
for(S in 1:nsim){ # simulation iterations
sim.counter(S, nsim, simsteps)
large.norm.sample <- rnorm(max.size) # normal sample
large.lnorm.sample <- rlnorm(max.size) # lognormal sample
for(N in 1:length(nvec)){ # sample sizes
# sub-sample + shift by effect size
norm.sample <- large.norm.sample[1:nvec[N]] + ES
lnorm.sample <- large.lnorm.sample[1:nvec[N]] + ES
# normal population
# t-test on mean
simres[1,N,S] <- trimci(norm.sample, tr=0, pr=FALSE)$p.value
# t-test on 20% trimmed mean
simres[2,N,S] <- trimci(norm.sample, tr=0.2, pr=FALSE)$p.value
# median test
simres[3,N,S] <- sintv2(norm.sample, pr=FALSE)$p.value
# lognormal population
# t-test on mean
# subtract mu so the mean is zero on average
simres[4,N,S] <- trimci(lnorm.sample - logn.m, tr=0, pr=FALSE)$p.value
# t-test on 20% trimmed mean
# subtract population tm so the tm is zero on average
simres[5,N,S] <- trimci(lnorm.sample - logn.tm, tr=0.2, pr=FALSE)$p.value
# median test
# subtract population md so the md is zero on average
simres[6,N,S] <- sintv2(lnorm.sample - logn.md, pr=FALSE)$p.value
}
}
beep(sound = "mario")
save(simres, file = "./data/simres_fp.RData")
```
# Plot simulation results
## Get results
We get the type I error rate for each combination of test and sample size.
```{r}
load("./data/simres_fp.RData")
res <- apply(simres <= 0.05, c(1,2), mean)
```
## Figure using base R
```{r}
plot(nvec, seq(0, 0.2, length.out=length(nvec)), xlab='Sample size', ylab='Type I error rate', type='n')
lines(nvec, res[1,], lty=1, col="orange") # normal: t-test on mean
lines(nvec, res[2,], lty=1, col="green") # normal: t-test on 20% trimmed mean
lines(nvec, res[3,], lty=1, col="blue") # normal: median test
lines(nvec, res[4,], lty=2, col="orange") # lognormal: t-test on mean
lines(nvec, res[5,], lty=2, col="green") # lognormal: t-test on 20% trimmed mean
lines(nvec, res[6,], lty=2, col="blue") # lognormal: median test
abline(0.075, 0, lty=1)
legend(300, 0.2,
c("N: mean", "N: tm", "N: md", "LN: mean", "LN: tm", "LN: md"),
lty = c(1,1,1,2,2,2),
col = c("orange","green","blue","orange","green","blue"))
```
## Figure using `ggplot2`
```{r, warning=FALSE}
nvec.labs <- c("10","","","","50","","","","","100","","","","","150","200","250","300","350","400","500","600","700","800")
# create data frame
y <- c(res[1,],res[2,],res[3,],
res[4,],res[5,],res[6,])
Distribution <- c(rep('Normal',length(nvec)*3), rep('Lognormal',length(nvec)*3))
Estimator <- c(rep('Mean',length(nvec)), rep('Trimmed mean',length(nvec)), rep('Median',length(nvec)),
rep('Mean',length(nvec)), rep('Trimmed mean',length(nvec)), rep('Median',length(nvec)))
df <- tibble(x = rep(nvec, 6), # sample size
y = y, # `Type I error probability
Distribution = as.factor(Distribution),
Estimator = as.factor(Estimator))
df$Estimator <- keeporder(df$Estimator)
df$Distribution <- keeporder(df$Distribution)
# make plot
pA <- ggplot(df, aes(x, y)) + theme_gar +
# Bradley's (1978) satisfactory range
geom_ribbon(aes(ymin = 0.025, ymax = 0.075), fill = "grey80") +
# Bradley's (1978) ideal range
geom_ribbon(aes(ymin = 0.045, ymax = 0.055), fill = "grey90") +
geom_abline(intercept = 0.05, slope = 0) + # 0.05 reference line
geom_line(aes(linetype=Distribution, colour=Estimator), linewidth=0.75) +
scale_colour_manual(values = c("#56B4E9", "#D55E00", "black")) + ##009E73
theme(axis.title.x = element_text(size = 18),
axis.text.x = element_text(size = 14),
axis.text.y = element_text(size = 16),
axis.title.y = element_text(size = 18),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
legend.position = c(0.55, 0.8),
legend.box = "horizontal") +
scale_x_continuous(breaks = nvec, labels = nvec.labs) +
scale_y_continuous(limits = c(0,0.16),
breaks = seq(0, 0.15, 0.025)) +
coord_cartesian(ylim = c(0.02, 0.130)) +
labs(x = "Sample size", y = "Type I error probability")
pA
```
## Get values reported in the text
```{r, warning=FALSE}
res1 <- round(approx(x=res[4,], y=nvec, xout=0.075)$y)
res2 <- round(approx(x=res[4,], y=nvec, xout=0.055)$y)
```
### T-test on means
False positive probabilities when sampling from a lognormal population:
- For n=30: `r round(res[4,nvec==30], digits=3)`;
- For n=100: `r round(res[4,nvec==100], digits=3)`;
- To reach 0.075, n should be at least: `r res1`;
- To reach 0.055, n should be at least: `r res2`;
- For n=600: `r round(res[4,nvec==600], digits=3)`;
- For n=700: `r round(res[4,nvec==700], digits=3)`.
### Inference on 20% trimmed means
False positive probabilities when sampling from a lognormal population:
- For n=30: `r round(res[5,nvec==30], digits=3)`;
- For n=100: `r round(res[5,nvec==100], digits=3)`.
False positive probabilities when sampling from a normal population:
- For n=10: `r round(res[2,nvec==10], digits=3)`;
- For n=20: `r round(res[2,nvec==20], digits=3)`.
### Inference on medians
False positive probabilities when sampling from a lognormal population:
- For n=10: `r round(res[6,nvec==10], digits=3)`;
- For n=30: `r round(res[6,nvec==30], digits=3)`;
- For n=100: `r round(res[6,nvec==100], digits=3)`.
False positive probabilities when sampling from a normal population:
- For n=10: `r round(res[3,nvec==10], digits=3)`;
- For n=20: `r round(res[3,nvec==20], digits=3)`.
# Simulation: true positives
## Define parameters
```{r}
nsim <- 20000 # number of simulation iterations
nvec <- c(seq(10,100,10), seq(125,250,25), 300) # vector of sample sizes to test
max.size <- max(nvec)
ES <- 0.5 # effect size
```
## Run simulation
No need to run this chunk; the results are loaded in the next chunk for convenience. If you run your own simulation, change the name of the file at the end of the chunk, otherwise the results used in the article will be overwritten.
```{r eval=FALSE}
set.seed(45) # set random number generator for reproducibility
# population values
logn.m <- exp(0.5) # mean of standard lognormal distribution
logn.md <- exp(0) # median of standard lognormal distribution
logn.tm <- ghtrim(g=1) + logn.md # trimmed mean of lognormal distribution
# 6 conditions: normal / skewed x mean / trimmed mean / median
simres <- array(0, dim = c(6, length(nvec), nsim))
simsteps <- 500 # get a console update every simsteps iterations
for(S in 1:nsim){ # simulation iterations
sim.counter(S, nsim, simsteps)
large.norm.sample <- rnorm(max.size) # normal sample
large.lnorm.sample <- rlnorm(max.size) # lognormal sample
for(N in 1:length(nvec)){ # sample sizes
# sub-sample + shift by effect size
norm.sample <- large.norm.sample[1:nvec[N]] + ES
lnorm.sample <- large.lnorm.sample[1:nvec[N]] + ES
# normal population
# t-test on mean
simres[1,N,S] <- trimci(norm.sample, tr=0, pr=FALSE)$p.value
# t-test on 20% trimmed mean
simres[2,N,S] <- trimci(norm.sample, tr=0.2, pr=FALSE)$p.value
# median test
simres[3,N,S] <- sintv2(norm.sample, pr=FALSE)$p.value
# lognormal population
# t-test on mean
# subtract mu so the mean is zero on average
simres[4,N,S] <- trimci(lnorm.sample - logn.m, tr=0, pr=FALSE)$p.value
# t-test on 20% trimmed mean
# subtract population tm so the tm is zero on average
simres[5,N,S] <- trimci(lnorm.sample - logn.tm, tr=0.2, pr=FALSE)$p.value
# median test
# subtract population md so the md is zero on average
simres[6,N,S] <- sintv2(lnorm.sample - logn.md, pr=FALSE)$p.value
}
}
beep(sound = "mario")
save(simres, file = "./data/simres_tp.RData")
```
# Plot simulation results
## Get results
We get the power for each combination of test and sample size.
```{r}
load("./data/simres_tp.RData")
res <- apply(simres <= 0.05, c(1,2), mean)
```
## Base R figure
```{r}
plot(nvec, seq(0, 1, length.out=length(nvec)), xlab='Sample size', ylab='Power', type='n')
lines(nvec, res[1,], lty=1, col="orange") # normal: t-test on mean
lines(nvec, res[2,], lty=1, col="green") # normal: t-test on 20% trimmed mean
lines(nvec, res[3,], lty=1, col="blue") # normal: median test
lines(nvec, res[4,], lty=2, col="orange") # lognormal: t-test on mean
lines(nvec, res[5,], lty=2, col="green") # lognormal: t-test on 20% trimmed mean
lines(nvec, res[6,], lty=2, col="blue") # lognormal: median test
abline(0.8, 0, lty=2)
legend(200, 0.5,
c("N: mean", "N: tm", "N: md", "LN: mean", "LN: tm", "LN: md"),
lty = c(1,1,1,2,2,2),
col = c("orange","green","blue","orange","green","blue"))
```
# `ggplot2` figure
```{r}
nvec.labs <- c("10","","","","50","","","","","100","","150","","200","","250","300")
# create data frame
y <- c(res[1,],res[2,],res[3,],
res[4,],res[5,],res[6,])
Distribution <- c(rep('Normal',length(nvec)*3), rep('Lognormal',length(nvec)*3))
Estimator <- c(rep('Mean',length(nvec)), rep('Trimmed Mean',length(nvec)), rep('Median',length(nvec)),
rep('Mean',length(nvec)), rep('Trimmed Mean',length(nvec)), rep('Median',length(nvec)))
df <- tibble(x = rep(nvec, 6), # sample size
y = y, # `Type I error probability
Distribution = as.factor(Distribution),
Estimator = as.factor(Estimator))
df$Estimator <- keeporder(df$Estimator)
df$Distribution <- keeporder(df$Distribution)
# make plot
pB <- ggplot(df, aes(x, y)) + theme_gar +
geom_abline(intercept = 0.80, slope = 0, linewidth=0.25) + # 0.80 reference line
geom_abline(intercept = 0.90, slope = 0, linewidth=0.25) + # 0.90 reference line
geom_line(aes(linetype=Distribution, colour=Estimator), linewidth=0.75, show.legend = FALSE) +
scale_colour_manual(values = c("#56B4E9", "#D55E00", "black")) + #009E73
theme(axis.title.x = element_text(size = 18),
axis.text = element_text(size = 16),
axis.title.y = element_text(size = 18),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
legend.position = c(0.65, 0.2),
legend.box = "horizontal") +
scale_y_continuous(limits = c(0,1),
breaks = seq(0, 1, 0.2),
minor_breaks = seq(0.1,0.9,0.2)) +
scale_x_continuous(limits = c(0, 300),
breaks = nvec, labels = nvec.labs) +
labs(x = "Sample size", y = "Power (true positive probability)")
pB
```
## combine panels into one figure
```{r, eval=FALSE}
cowplot::plot_grid(pA, pB,
labels=c("A", "B"),
ncol = 1,
nrow = 2,
rel_widths = c(1, 1),
label_size = 20,
hjust = -0.5,
scale=.95,
align = "h")
# save figure
ggsave(filename='./figures/figure2.pdf',width=7,height=10)
```