This is problem set #4, in which we want you to integrate your knowledge of data wrangling with some basic simulation skills. It’s a short problem set to help consolidate your ggplot2 skills and then help you get your feet wet in testing statistical concepts through “making up data” rather than consulting a textbook or doing math.

For ease of reading, please separate your answers from our text by marking our text with the > character (indicating quotes).

Part 1: ggplot practice

This part is a warmup, it should be relatively straightforward ggplot2 practice.

Load data from Frank, Vul, Saxe (2011, Infancy), a study in which we measured infants’ looking to hands in moving scenes. There were infants from 3 months all the way to about two years, and there were two movie conditions (Faces_Medium, in which kids played on a white background, and Faces_Plus, in which the backgrounds were more complex and the people in the videos were both kids and adults). An eye-tracker measured children’s attention to faces. This version of the dataset only gives two conditions and only shows the amount of looking at hands (other variables were measured as well).

library(tidyverse)
## ── Attaching packages ────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.3     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ───────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
fvs <- read_csv("data/FVS2011-hands.csv")
## Parsed with column specification:
## cols(
##   subid = col_double(),
##   age = col_double(),
##   condition = col_character(),
##   hand.look = col_double()
## )

First, use ggplot to plot a histogram of the ages of children in the study. NOTE: this is a repeated measures design, so you can’t just take a histogram of every measurement.

head(fvs)
## # A tibble: 6 x 4
##   subid   age condition    hand.look
##   <dbl> <dbl> <chr>            <dbl>
## 1     2  3.16 Faces_Medium    0.0319
## 2    93  5.03 Faces_Medium    0.119 
## 3    29  5.85 Faces_Medium    0.0921
## 4    76  5.85 Faces_Medium    0.130 
## 5    48  6.08 Faces_Medium    0.0138
## 6   101  6.15 Faces_Medium    0.0438
filtered_fvs = fvs %>% filter(!duplicated(fvs$subid))
ggplot(filtered_fvs,
       aes(x=age)) +
    geom_histogram(binwidth = 1)

Second, make a scatter plot showing hand looking as a function of age and condition. Add appropriate smoothing lines. Take the time to fix the axis labels and make the plot look nice.

ggplot(fvs,
       aes(x=age, y=hand.look, col=condition)) +
  geom_point() +
  geom_smooth(method="lm")
## `geom_smooth()` using formula 'y ~ x'

What do you conclude from this pattern of data?

It looks like in the faces plus condition, we see a stronger age effect on hand looking than the faces medium condition. It also looks like the variability is larger in the faces plus condition.

What statistical analyses would you perform here to quantify these differences?

We could use a t test to see if the likelihood that these two filts are coming from the same distribution.

Part 2: Simulation

library(tidyverse)

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).

The goal of these t-tests are to determine, based on 30 observations, whether the underlying distribution (in this case a normal distribution with mean 0 and standard deviation 1) has a mean that is different from 0. In reality, the mean is not different from 0 (we sampled it using rnorm), but sometimes the 30 observations we get in our experiment will suggest that the mean is higher or lower. In this case, we’ll get a “significant” result and incorrectly reject the null hypothesis of mean 0.

What’s the proportion of “significant” results (\(p < .05\)) that you see?

First do this using a for loop.

samp = rnorm(30)
tt = t.test(samp)
tt$p.value
## [1] 0.8943136
ps = as.double(1:10000)
for (i in 1:10000) {
  samp = rnorm(30)
  ps[i] = as.double(t.test(samp)$p.value)
}
length(ps[ps<0.05])
## [1] 493

Next, do this using the replicate function:

ps = replicate(10000, t.test(rnorm(30))$p.value)
length(ps[ps<0.05])
## [1] 516

How does this compare to the intended false-positive rate of \(\alpha=0.05\)?

It’s about what we would expect. It looks like we get a positive result about 5% of the time.

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 the true mean is different from 0. 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.while_sample <- function (p.upper) {
  samp = rnorm(30)
  p = t.test(samp)$p.value
  loop_count = 0
  while(p<p.upper & p>0.05 & loop_count < 1000) {
    samp = c(samp,rnorm(30))
    p = t.test(samp)$p.value
    loop_count = loop_count + 1
  }
  return(p)
}

double.if_sample <- function (p.upper) {
  samp = rnorm(30)
  p = t.test(samp)$p.value
  if(p<p.upper & p>0.05) {
    samp = c(samp,rnorm(30))
    p = t.test(samp)$p.value
  }
  return(p)
}

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

ps = replicate(10000, double.while_sample(0.25))
length(ps[ps<0.05])
## [1] 899
ps = replicate(10000, double.if_sample(0.25))
length(ps[ps<0.05])
## [1] 727

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

Yes there is an inflation and it’s not great. The portion increased by more than 50% in both the case where we indefinitely loop forward and close to 50% when we only do a single repeat..

Now modify this code so that you can investigate this “double the sample” rule in a bit more depth. In the previous question, the researcher doubles the sample only when they think they got “close” to a significant result, i.e. when their not-significant p is less than 0.25. What if the researcher was more optimistic? See what happens in these 3 other scenarios:

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.

ps = replicate(10000, double.if_sample(0.5))
length(ps[ps<0.05])
## [1] 796
ps = replicate(10000, double.while_sample(0.5))
length(ps[ps<0.05])
## [1] 1210
ps = replicate(10000, double.if_sample(0.75))
length(ps[ps<0.05])
## [1] 816
ps = replicate(10000, double.while_sample(0.75))
length(ps[ps<0.05])
## [1] 1815
ps = replicate(10000, double.if_sample(1))
length(ps[ps<0.05])
## [1] 885

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

If you do an algorithmic approach where you persistently add data every time you do don’t get a significant result, then you’re wayy more likely to get a significant result. If, on the other hand, you only double your data once, then you’re definitely still inflating the chance of a positive result, but it’s not quite as bad as the algorithmic approach. Either way, this is convincing enough to make me think that all repeat studies should use completely fresh data.