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("readr")
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.
library("ggplot2")
library("tidyverse")
fvs_unique <- distinct(fvs, subid, .keep_all = TRUE)
ggplot(data = fvs_unique,
mapping = aes(x = age)) +
stat_bin(binwidth = 2,
color = "black",
fill = "lightsteelblue1") +
labs(x = "Age (months)",
y = "Count")
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(data = fvs,
mapping = aes(x = age,
y = hand.look,
color = condition)) +
geom_point() +
geom_smooth(method = "lm",
formula = y ~ x) +
labs(x = "Age (months)",
y = "Hand looking proportion",
color = "Condition")
What do you conclude from this pattern of data?
Children had increased hand looking with increasing age, and the rate of increase for the
Faces_Plus(i.e. complex) condition was greater than that for theFaces_Medium(i.e. simple) condition. Thus, children learn to focus on hands more rapidly in complex contexts than in simple contexts.
What statistical analyses would you perform here to quantify these differences?
A mixed-effects regression model on hand looking with one within-subjects factor (condition) and one between-subjects factor (age) can quantify the amount to which condition affects the increase in hand looking with age.
library(rstatix)
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.
set.seed(9999)
for_df <- data.frame()
for (i in 1:10000) {
normal_for <- tibble(samples = rnorm(n = 30, mean = 0, sd = 1))
t_for <- t_test(normal_for,
formula = samples ~ 1)
for_df <- rbind(for_df, t_for)
}
for_df %>%
mutate(significant = p < .05) %>%
summarize(prob = sum(significant)/n())
## # A tibble: 1 x 1
## prob
## <dbl>
## 1 0.0542
Next, do this using the replicate function:
set.seed(9999)
t_on_sample <- function(n, mean, sd) {
normal_rep <- tibble(samples = rnorm(n = n, mean = mean, sd = sd))
return(t_test(normal_rep,
formula = samples ~ 1)$p)
}
rep_df <- tibble(p = replicate(n = 10000,
t_on_sample(n = 30, mean = 0, sd = 1)))
rep_df %>%
mutate(significant = p < .05) %>%
summarize(prob = sum(significant)/n())
## # A tibble: 1 x 1
## prob
## <dbl>
## 1 0.0542
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
The probabilities are approximately equal to the alpha value (within 0.005).
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 (max_p) {
data_set <- tibble(samples = rnorm(n = 30, mean = 0, sd = 1))
data_p <- t_test(data_set, formula = samples ~ 1)$p
if (data_p >= .05 && data_p < max_p) {
data_set <- rbind(data_set, tibble(samples = rnorm(n = 30, mean = 0, sd = 1)))
data_p <- t_test(data_set, formula = samples ~ 1)$p
}
return(data_p)
}
Now call this function 10k times and find out what happens.
set.seed(9999)
double_df <- tibble(p = replicate(n = 10000, double.sample(0.25)))
double_df %>%
mutate(significant = p < .05) %>%
summarize(prob = sum(significant)/n())
## # A tibble: 1 x 1
## prob
## <dbl>
## 1 0.0738
Is there an inflation of false positives? How bad is it?
Yes! The probability increases to 0.074, which is about 0.02 more than before.
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.
set.seed(9999)
double_list <- list()
double_list[[1]] <- tibble(p = replicate(n = 20000, double.sample(0.5)))
double_list[[2]] <- tibble(p = replicate(n = 20000, double.sample(0.75)))
double_list[[3]] <- tibble(p = replicate(n = 20000, double.sample(1)))
double_tbl <- data.frame(row.names = c(0.5, 0.75, 1))
for (i in 1:3) {
double_tbl[i,1] <- double_list[[i]] %>%
mutate(significant = p < .05) %>%
summarize(prob = sum(significant)/n()) %>%
.[1]
}
print(double_tbl)
## prob
## 0.5 0.0787
## 0.75 0.0800
## 1 0.0817
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
The greater the permissibility on doubling the sample size, the greater the deviation from the expected false positive rate. This is bad in terms of having scientific accountability and accuracy of reporting, and an additional insidious aspect is that this “methodology” is rarely reported, and thus it is impossible to adjust for such post-hoc data collection changes.