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 --
## v ggplot2 3.1.0     v purrr   0.2.5
## v tibble  1.4.2     v dplyr   0.7.7
## v tidyr   0.8.2     v stringr 1.3.1
## v readr   1.1.1     v forcats 0.3.0
## -- 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.

pvals <- array()
for (i in 1:10000) {
  pvals[i] <- t.test(rnorm(30))$p.value
}

sum(pvals < 0.05)/10000
## [1] 0.049

Next, do this using the replicate function:

pvals <- replicate(10000, t.test(rnorm(30))$p.value)

sum(pvals < 0.05)/10000
## [1] 0.0537

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 () {
  
data1 <- rnorm(30)
pval <- t.test(data1)$p.value

if(.05 < pval && pval <.25 )
  {data2 <- rnorm(30)} 
else {data2 <- NULL}
  return (union(data1, data2))
} 

double.sample()
##  [1]  0.2747462  0.3801568  0.4119203  0.3831514  0.9336668  0.3279626
##  [7]  0.5566593 -0.5479319 -0.8583462 -0.1758944 -1.9471117  1.0163602
## [13]  0.5766513 -1.7430210  0.2237177  1.2167233 -1.0419948 -0.3697298
## [19] -0.8059276  2.0106940 -0.3252205  0.3063095 -0.6045983  1.0545394
## [25]  0.1520955 -0.6723691  1.5371248  0.1167917 -0.2509279  1.3934601

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

pvals <- replicate(10000, t.test(double.sample())$p.value)

sum(pvals < 0.05)/10000
## [1] 0.0719

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

There is an inflation. It is bad because it increases the absolute percentage of false positives by approximately 2%.

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.sampleall <- function () {
  
data1 <- rnorm(30)
pval <- t.test(data1)$p.value

if(.05 < pval)
  {data2 <- rnorm(30)} 
else {data2 <- NULL}
  return (union(data1, data2))
} 


pvalsall <- replicate(10000, t.test(double.sampleall())$p.value)
                      




double.sample.75 <- function () {
  
data1 <- rnorm(30)
pval <- t.test(data1)$p.value

if(.05 < pval && .75 > pval)
  {data2 <- rnorm(30)} 
else {data2 <- NULL}
  return (union(data1, data2))
} 


pvals.75 <- replicate(10000, t.test(double.sample.75())$p.value)



#Proportion wrt/ to the range [.05, .25]
                      
sum(pvals < 0.05)/10000
## [1] 0.0719
#Proportion wrt/ to the range [.05, .75]

sum(pvals.75 < 0.05)/10000
## [1] 0.081
#Proportion wrt/ to the range [.05, 1]
                      
sum(pvalsall < 0.05)/10000
## [1] 0.0848

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

Data-dependent collection policies inflate the rate of false positives in proportion to how easy it is for the initially generated data to satisfy the conditions for collecting more data. It is very, very bad because it can increase the absolute percentage of false positives by 3% or more.