library(readr)
library(dplyr)
library(ggplot2)
fvs <- read_csv("data/FVS2011-hands.csv")Psych 251 PS4: Simulation + Analysis
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).
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.
unique_subjects <- fvs |> distinct(subid, .keep_all = TRUE)
ggplot(unique_subjects, 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(age, hand.look, colour = condition)) +
geom_point() +
geom_smooth() +
labs(x = "Age of Children", y = "Amount of Hand Looking", title = "Hand Looking as a Function of Age and Condition", colour = "Environment") +
scale_color_discrete(labels = c("White Background", "Complex Background")) +
theme_classic()What do you conclude from this pattern of data?
As age increases, hand looking tends to increase more in settings with more complexity, but variance also increases.
What statistical analyses would you perform here to quantify these differences?
I would perform a linear regression of the Age x Condition interaction to see if the p-values showed significance. I would also try a nonlinear regression since the line for the Complex Background condition is curvilinear.
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.
count <- 0
for (i in 1:10000) {
d <- rnorm(30, 0, 1)
if (t.test(d)$p.value < .05) {
count <- count + 1
}
}
print(count / 10000)[1] 0.049
Next, do this using the replicate function:
p_values <- replicate(10000, {
d <- rnorm(30, 0, 1)
t.test(d)$p.value
})
mean(p_values < 0.05)[1] 0.0471
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
The two are both almost exactly 0.05, each within 0.005 of the expected rate. However, this amount varies in magnitude depending on the runtime instance.
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_value) {
d <- rnorm(30, 0, 1)
p <- t.test(d)$p.value
if (p < upper_p_value && p > 0.05) {
d <- c(d, rnorm(30, 0, 1))
p <- t.test(d)$p.value
}
return(p < 0.05)
}Now call this function 10k times and find out what happens.
cat("Upper p value: 0.25")Upper p value: 0.25
sum(replicate(10000, double.sample(0.25))) / 10000[1] 0.0671
Is there an inflation of false positives? How bad is it?
Yes, the false positive rate increased from 0.05 to 0.07. This is pretty bad: this means that researchers using this optional stopping rule would incorrectly reject the null hypothesis 7% of the time instead of the intended 5%, which is 40% more than they should 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:
- The researcher doubles the sample whenever their pvalue is not significant, but it’s less than 0.5.
- The researcher doubles the sample whenever their pvalue is not significant, but it’s less than 0.75.
- The research doubles their sample whenever they get ANY pvalue that is not significant.
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.
cat("Upper p value: 0.5\n")Upper p value: 0.5
sum(replicate(100000, double.sample(0.5))) / 100000[1] 0.07959
cat("Upper p value: 0.75\n")Upper p value: 0.75
sum(replicate(100000, double.sample(0.75))) / 100000[1] 0.08031
cat("Upper p value: unbounded\n")Upper p value: unbounded
sum(replicate(100000, double.sample(Inf))) / 100000[1] 0.08247
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
From this simulation, we can see that increasing this upper bound continues to increase the rate at which we would incorrectly reject the null hypothesis, now up to over a 60% increase in such instances. It seems this rate increases logarithmically but monotonically, suggesting that even small bounds are still quite harmful.