This is problem set #3, in which we want you to integrate your knowledge of data wrangling with some basic simulation skills and some linear modeling.
For ease of reading, please separate your answers from our text by marking our text with the > character (indicating quotes).
library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.0.0 ✔ purrr 0.2.5
## ✔ tibble 1.4.2 ✔ dplyr 0.7.7
## ✔ tidyr 0.8.1 ✔ stringr 1.3.1
## ✔ readr 1.1.1 ✔ forcats 0.3.0
## ── Conflicts ──────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
Let’s start by convincing ourselves that t-tests have the appropriate false positive rate. Run 10,000 t-tests with standard, normally-distributed data from a made up 30-person, single-measurement experiment (the command for sampling from a normal distribution is rnorm). What’s the mean number of “significant” results?
First do this using a for loop.
N <- 30
test_count <- 10000
significant <- 0
for (val in 1:test_count) {
t <- t.test(rnorm(N))
if (t$p.value < 0.05) {
significant <- significant + 1
}
}
significant / test_count
## [1] 0.0512
Next, do this using the replicate function:
simulate_ttest <- function () {
t <- t.test(rnorm(N))
return (t$p.value < 0.05)
}
sigtest_results <- replicate(test_count, simulate_ttest())
sum(sigtest_results) / test_count
## [1] 0.0481
Ok, that was a bit boring. Let’s try something more interesting - let’s implement a p-value sniffing simulation, in the style of Simons, Nelson, & Simonsohn (2011).
Consider this scenario: you have done an experiment, again with 30 participants (one observation each, just for simplicity). The question is whether their performance is above chance. You aren’t going to check the p-value every trial, but let’s say you run 30 - then if the p-value is within the range p < .25 and p > .05, you optionally run 30 more and add those data, then test again. But if the original p value is < .05, you call it a day, and if the original is > .25, you also stop.
First, write a function that implements this sampling regime.
double_sample <- function (cutoff=0.25) {
data <- rnorm(N)
p <- t.test(data)$p.value
if (p < 0.05) {
return(TRUE)
} else if (p > cutoff) {
return(FALSE)
} else {
data <- c(data, rnorm(30))
p <- t.test(data)$p.value
return(p < 0.05)
}
}
Now call this function 10k times and find out what happens.
sum(replicate(test_count, double_sample())) / test_count
## [1] 0.0683
Is there an inflation of false positives? How bad is it?
There is an inflation of false positives; it goes from from 5% to about 7%.
Now modify this code so that you can investigate this “double the sample” rule in a bit more depth. Let’s see what happens when you double the sample ANY time p > .05 (not just when p < .25), or when you do it only if p < .5 or < .75. How do these choices affect the false positive rate?
HINT: Try to do this by making the function double.sample take the upper p value as an argument, so that you can pass this through dplyr.
HINT 2: You may need more samples. Find out by looking at how the results change from run to run.
test_count <- 100000
many_double_samples <- function (cutoff) {
sum(replicate(test_count, double_sample(cutoff)))
}
cutoffs <- seq(0.05, 1.0, by=.05)
false_positives <- lapply(cutoffs, many_double_samples)
fp_over_doublecheck <- tibble(Cutoffs=cutoffs, FalsePositives=false_positives) %>%
mutate(
EmpiricalFPR = unlist(FalsePositives) / test_count,
NominalFPR = 0.05
) %>%
gather(Type, FPR, ends_with('FPR'))
fp_over_doublecheck %>%
ggplot(aes(x=Cutoffs, y=FPR, group=Type, color=Type)) +
geom_line(aes(linetype=Type)) +
scale_linetype_manual(values = c(1, 2)) +
geom_point(aes(shape=Type)) +
scale_shape_manual(values = c(16, 32)) +
ylim(0, 0.1)
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
This policy certainly increases false positive rate, even when the doubling only happens close to the significance level. To be honest, this policy didn’t increase false-positive rate as much as I thought it would, but this seems a relatively tame version of data-dependent analysis. In this policy, the test is only repeated once, rather than many times, and another 30 people are run before peeking at the data again. If smaller batches were used - 10, 5, or 1 person - before peeking, or if this test was run again in three or four times with a new batch each time, then the effect would be larger.