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)
## Warning: package 'tidyverse' was built under R version 3.4.4
## -- Attaching packages -------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.0.0     v purrr   0.2.5
## v tibble  1.4.2     v dplyr   0.7.6
## v tidyr   0.8.1     v stringr 1.3.1
## v readr   1.1.1     v forcats 0.3.0
## Warning: package 'ggplot2' was built under R version 3.4.4
## Warning: package 'tibble' was built under R version 3.4.4
## Warning: package 'tidyr' was built under R version 3.4.4
## Warning: package 'readr' was built under R version 3.4.4
## Warning: package 'purrr' was built under R version 3.4.4
## Warning: package 'dplyr' was built under R version 3.4.4
## Warning: package 'stringr' was built under R version 3.4.4
## Warning: package 'forcats' was built under R version 3.4.4
## -- Conflicts ----------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x 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.

significantPs = 0

for (i in 1:10000) {
  #t.test stores values in an array. index 3 has the pvalue.
  significantPs[i] = ifelse(t.test(rnorm(30), rnorm(30))[3] < .05, 1, 0)
}

mean(significantPs)
## [1] 0.0536

Next, do this using the replicate function:

#replicate(number of times to replicate, function to replicate)
# since t.test returns a list. indexing the function will return the index.
#example t.test(x,y)[3] returns the pvalue
significantPs = replicate(10000, ifelse(t.test(rnorm(30), rnorm(30))[3] < .05, 1, 0))

mean(significantPs)
## [1] 0.0507

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 () {
  
 x = as.numeric(t.test(rnorm(30), rnorm(30))[3])
 if (x >.25) {
 return(0)
 } 
 else if (x <= .25 && x >= .05 ){
    y  =  as.numeric(t.test(rnorm(30), rnorm(30))[3])
    ifelse(y < .05, 1, 0)
 } else {
   return(1)
 } 

}

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

significantPs = replicate(10000, double.sample())

# print(significantPs)

mean(significantPs)
## [1] 0.0625

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

Yes. It increases the chances of significant results about 1%.

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.

double.sample <- function () {
 x = t.test(rnorm(30), rnorm(30))[3]
 if (x >= .05 ){
    y  =  t.test(rnorm(60), rnorm(60))[3]
    ifelse(y < .05, 1, 0)
 }
 else { 
 return(1)
 }
}

significantPs = replicate(10000, double.sample())

mean(significantPs)
## [1] 0.0957
double.sample <- function () {
 x = t.test(rnorm(30), rnorm(30))[3]
 if (x <.05) {
   return(1)
   }
 else if (x >= .05 | x < .75){
    y  =  t.test(rnorm(60), rnorm(60))[3]
    ifelse(y < .05, 1, 0)
 } 
 else{ 
 return(0)
 }
}

significantPs = replicate(10000, double.sample())

mean(significantPs)
## [1] 0.0964

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

Double sampling increases your likelihood of finding significant results by almost double. It is actually more harmful than I would have thought at first glance.