Repository navigation
Expand file tree
/
Copy pathp-hacking-demo.qmd
More file actions
98 lines (73 loc) · 2.33 KB
/
Copy pathp-hacking-demo.qmd
File metadata and controls
98 lines (73 loc) · 2.33 KB
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
---
title: "p-hacking demo"
author: "Maya Flannery"
format:
html:
embed-resources: true
---
```{r}
#| output: false
library(tidyverse)
set.seed(999)
```
## Simulation
### Part I
Imagine if we ran the [demo experiment](https://www.jspsych.org/7.3/tutorials/rt-task/)
and found the average response time for orange and blue stimuli for 30 participants.
This code simulates the data, note: **the null hypothesis is TRUE** (we can do
this in a simulation), $\mu = 120$ for both orange and blue stimuli.
```{r}
N <- 30
mu <- 120
sd <- 30
# rnorm() produces random samples from a normal distribution
orangeStimulus <- rnorm(N, mu, sd)
blueStimulus <- rnorm(N, mu, sd)
```
A t-test can test if there is a significant effect of colour.
```{r}
pValues <- c(t.test(orangeStimulus, blueStimulus, paired=TRUE)$p.value)
pValues
```
Since $p = 0.27$, we would correctly fail to reject the null hypothesis.
### Part II
What happens if we add participants, 1 by 1, and run a t-test.
This code 'runs' 220 more participants (and records each p-value):
```{r}
nAdded <- N
for (i in 1:(250-N)) {
orangeStimulus <- append(orangeStimulus, rnorm(1, mu, sd))
blueStimulus <- append(blueStimulus, rnorm(1, mu, sd))
pValues <- append(pValues, t.test(orangeStimulus, blueStimulus, paired=TRUE)$p.value)
nAdded <- nAdded+1
}
```
What is the p-value with 100 participants?
```{r}
pValues[100-N]
```
$p = 0.04$, this test **incorrectly rejects** the null hypothesis!
What about 125 participants?
```{r}
pValues[125-N]
```
How do you interpret this p-value?
## Analysis
Here are the distributions of responses for 250 participants.
```{r}
ggplot() +
geom_density(aes(orangeStimulus), color = "orange") +
geom_density(aes(blueStimulus), color = "blue") +
geom_vline(xintercept = mean(orangeStimulus), color="orange", linetype=2) +
geom_vline(xintercept = mean(blueStimulus), color="blue", linetype=2) +
ggtitle("Distribution of response times for Orange and Blue stimuli") +
xlab("Response time (ms)")
```
Here are all of the p-values plotted for 30--250 participants.
```{r}
ggplot() + geom_point(aes(x=N:nAdded, y=pValues)) +
geom_hline(yintercept=0.05, color = "red") +
ggtitle(paste("Simulated p-values for", nAdded-N, "two-sample t-tests (H0 is TRUE!)")) +
xlab("Number of participants") + scale_x_continuous(limits=c(N,nAdded)) +
ylab("p-value")
```