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).
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.3 ✓ purrr 0.3.4
## ✓ tibble 3.1.4 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.4 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
fvs <- read_csv("data/FVS2011-hands.csv")
##
## ── 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.
histogram <- fvs %>%
filter(condition == "Faces_Medium") %>%
ggplot(aes(x = age)) +
geom_histogram()
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.
scatter <- ggplot(fvs, aes(x = age,
y = hand.look,
color = condition)) +
geom_point(stat = "identity") +
geom_smooth(method = lm) +
expand_limits(x = c(0, 30)) +
ggtitle("Hand Looking by Age and Condition") +
xlab("Age (months)") +
ylab("Hand Looking Time (ms)")
scatter
## `geom_smooth()` using formula 'y ~ x'
What do you conclude from this pattern of data?
As infants get older, they spend more time looking at hands especially if they are in conditions with more complex backgrounds.
What statistical analyses would you perform here to quantify these differences?
Linear mixed effects model to examine the effects of age and condition on hand looking time.
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.
p_values <- length(10000)
for (i in 1:10000){
sample <- rnorm(n=30, mean = 0, sd = 1)
t_test <- t.test(sample)
p_values[i] <- t_test$p.value
}
hist(p_values) # confirming it works
(sum(p_values < .05) / 10000) # 0.0518 !
## [1] 0.0477
Next, do this using the replicate function:
replicated_p_values <- replicate(10000,
t.test(rnorm(30,0,1))$p.value)
hist(replicated_p_values)
(sum(replicated_p_values < .05) / 10000) # 0.0502 !
## [1] 0.0501
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
It appears to be… working? It seems that we got p < .05 values 5% of the time as anticipated? But not sure if that’s what you’re referring to…
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 (x, lower_bound, upper_bound) {
sample1 <- rnorm(x)
t_test1 <- t.test(sample1)
sample1.p_value <- as.numeric(t_test1$p.value)
if (sample1.p_value > lower_bound & sample1.p_value < upper_bound) {
sample2 <- c(sample1, rnorm(x))
t_test2 <- t.test(sample2)
sample2.p_value <- as.numeric(t_test2$p.value)
return(sample2.p_value)
} else {
return(sample1.p_value)
}
}
# shoutout to Julie who helped me finish writing this code!
Now call this function 10k times and find out what happens.
double_sample <- replicate(10000, double.sample(30, .05, .25))
hist(double_sample)
(sum(double_sample < .05) / 10000) # ~0.0754 !
## [1] 0.0747
Is there an inflation of false positives? How bad is it?
Yes! I do not know how to quantify how “bad” it is — but the fact that it is over .05 should be pretty alarming (especially considering that, in theory, it should not go above that)
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.
double_sample2 <- replicate(10000, double.sample(30, .05, .5))
hist(double_sample2)
double_sample3 <- replicate(10000, double.sample(30, .05, .75))
hist(double_sample3)
double_sample4 <- replicate(10000, double.sample(30, .05, 1))
hist(double_sample4)
(sum(double_sample2 < .05) / 10000) # ~0.0774 !
## [1] 0.0765
(sum(double_sample3 < .05) / 10000) # ~0.0816 !
## [1] 0.0851
(sum(double_sample4 < .05) / 10000) # 0.0862 !
## [1] 0.0822
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
Yeah it’s pretty bad. It appears that the proportion of false positives seems to increase as the policy increases… which is… not good!