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).
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).
fvs <- read.csv("data/FVS2011-hands.csv")
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.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.5 v dplyr 1.0.7
## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.0.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
fvs |>
select(subid, age) |>
distinct() |>
ggplot(aes(x=age)) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
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)) +
facet_wrap(~ condition) +
geom_point() +
geom_smooth(method="lm") +
labs(x="Age (months)", y="Hand Looking Time (s)") +
theme_light()
## `geom_smooth()` using formula 'y ~ x'
What do you conclude from this pattern of data?
I conclude that there is a weak relationship between age and hand looking time in the medium condition and a stronger relationship in the plus condition.
What statistical analyses would you perform here to quantify these differences?
I would compare the coefficients on age in the linear models fit to the data from each condition.
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.
fp_count <- 0
for (i in 1:10000) {
data <- rnorm(30)
p <- t.test(data)["p.value"]
if (p < 0.05) {
fp_count <- fp_count + 1
}
}
sprintf('false positive rate: %s', fp_count / 10000)
## [1] "false positive rate: 0.0569"
Next, do this using the replicate function:
fp_count <- sum(replicate(10000, t.test(rnorm(30))["p.value"] < 0.05))
sprintf('false positive rate: %s', fp_count / 10000)
## [1] "false positive rate: 0.0497"
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
This is almost identical to the intended false-positive rate. There is just a slight difference each time due to the randomness in the sampling.
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.sample <- function(upper_p) {
initial_data <- rnorm(30)
p <- t.test(initial_data)["p.value"]
if (p < 0.05) {
return(T)
} else if (p > upper_p) {
return(F)
} else {
new_data <- rnorm(30)
full_data <- c(initial_data, new_data)
p <- t.test(full_data)["p.value"]
return(p < 0.05)
}
}
Now call this function 10k times and find out what happens.
sprintf("false positive rate: %s", sum(replicate(10000, double.sample(0.25))) / 10000)
## [1] "false positive rate: 0.0727"
Is there an inflation of false positives? How bad is it?
There is an inflation of false positives. The false-positive rate is now around 0.07 rather than 0.05 where it is supposed to be. The difference is fairly small, as the FPR only goes up by about 2%, but how bad this is in practice depends on what specifically the study is and how bad a false positive could be.
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.
sprintf("FPR w/ upper p=0.5: %s", sum(replicate(100000, double.sample(0.5))) / 100000)
## [1] "FPR w/ upper p=0.5: 0.07919"
sprintf("FPR w/ upper p=0.75: %s", sum(replicate(100000, double.sample(0.75))) / 100000)
## [1] "FPR w/ upper p=0.75: 0.0831"
sprintf("FPR w/ upper p=1: %s", sum(replicate(100000, double.sample(1))) / 100000)
## [1] "FPR w/ upper p=1: 0.08338"
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
Based on this simulation, I conclude that the false-positive rate increases as the upper p bound increases, though the increase is not dramatic. I am inclined to say that this is not very bad since even when the upper p bound is 1 the false positive rate is only around 0.085, only a little bit higher than the 0.05 we get without data-dependent sampling. Still, I think it would be less than ideal to use this kind of sampling policy and conduct the analysis as if there were no data-dependence in sampling. Perhaps we would need to pick a smaller alpha value to counterbalance the increase in false positive rate if we use this approach.