This is problem set #4, 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).

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_trials = 10000
n_samples = 30

sig_trials = 0
for (i in 1:n_trials) {
  if (t.test(rnorm(n_samples))$p.value <= .05) {
    sig_trials = sig_trials + 1
  }
}
sig_trials / n_trials
## [1] 0.0486

Next, do this using the replicate function:

pvals = replicate(n_trials,t.test(rnorm(n_samples))$p.value)
length(pvals[pvals <= .05]) / n_trials
## [1] 0.0484

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 (p_redo_max=.25) {
  n_samples = 30
  p_redo_min = .05
  
  trials = rnorm(n_samples)
  p = t.test(trials)$p.value
  
  if (p > p_redo_min & p < p_redo_max) {
    trials = c(trials, rnorm(n_samples))
    p = t.test(trials)$p.value
  }
  
  return(p)
}

Now call this function 10k times and find out what happens.

pvals = replicate(n_trials, double.sample())
all_positives = length(pvals[pvals <= .05]) / n_trials
false_pos_ratio = 1 - (.05/all_positives)

all_positives
## [1] 0.0695
false_pos_ratio
## [1] 0.2805755

Is there an inflation of false positives? How bad is it?

Yes. 28.06% of the positives are false positives.

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.

n_trials = 100000
p_redo_max = 1
pvals = replicate(n_trials, double.sample(p_redo_max))
all_positives = length(pvals[pvals <= .05]) / n_trials
false_pos_ratio = 1 - (.05/all_positives)

all_positives
## [1] 0.08445
false_pos_ratio
## [1] 0.4079337
n_trials = 100000
p_redo_max = .75
pvals = replicate(n_trials, double.sample(p_redo_max))
all_positives = length(pvals[pvals <= .05]) / n_trials
false_pos_ratio = 1 - (.05/all_positives)

all_positives
## [1] 0.08218
false_pos_ratio
## [1] 0.3915795
n_trials = 100000
p_redo_max = .5
pvals = replicate(n_trials, double.sample(p_redo_max))
all_positives = length(pvals[pvals <= .05]) / n_trials
false_pos_ratio = 1 - (.05/all_positives)

all_positives
## [1] 0.07844
false_pos_ratio
## [1] 0.3625701

What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?

Sampling based on data inflates the number of false-positives. Changing the sample-size when the p-value is close to .05 such as .05 < p < .06, the study has a 5.4% chance to yield a false positive.