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).
library(readr)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.5 v dplyr 1.0.2
## v tibble 3.0.4 v stringr 1.4.0
## v tidyr 1.1.2 v forcats 0.5.0
## v purrr 0.3.4
## Warning: package 'ggplot2' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
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")
##
## -- 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.
Assume age for each participant reamins the same in each measurement.
fvs_age <- fvs %>%
group_by(subid) %>%
summarize(age = mean(age))
## `summarise()` ungrouping output (override with `.groups` argument)
ggplot(data = fvs_age, aes(x = age)) +
geom_histogram(stat = "bin") +
labs(title = "Age distribution (in months)")
## `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, color = condition, shape = condition)) +
geom_point() +
geom_smooth(method = "lm") +
theme_classic() +
labs(title = "Looking time to different movie conditions with respect to age",
y = "Looking time at hands (in proportion?)",
x = "Age (in months)") +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))
## `geom_smooth()` using formula 'y ~ x'
What do you conclude from this pattern of data?
As age increases, babies spent more time looking at hands.
According to the smoothing lines, there is an interaction between movie condition and age, such that older children tend to perform best at the Faces_Plus condition. There are also a few possible outliers that could bias the predictions (e.g. two zeros for older children in the Faces_Plus condition).
What statistical analyses would you perform here to quantify these differences?
Multiple regression. However, the assumptions have to be checked. For example, the homoscedasticity assumption might not be satisfied.
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_list <- c()
set.seed(252)
for (i in 1:10000) {
sample_d <- rnorm(30)
test_result <- t.test(sample_d, mu = 0)
p_value <- test_result$p.value
p_list <- append(p_list, p_value)
}
mean(p_list < 0.05)
## [1] 0.052
The proportion of significant results is 0.052.
Next, do this using the replicate function:
find_p <- function(){
sample_d <- rnorm(30)
test_result <- t.test(sample_d, mu = 0)
p_value <- test_result$p.value
return (p_value)
}
set.seed(252)
p_list_2 <- replicate(10000, find_p(), simplify = F)
mean(p_list_2 < 0.05)
## [1] 0.052
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
The rate is now 0.052, which is greater than the intended threshold.
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 () {
first_sample <- rnorm(30)
first_t_test_result <- t.test(first_sample, mu = 0)
if (first_t_test_result$p.value < 0.25 & first_t_test_result$p.value > 0.05) {
second_sample <- append(first_sample, rnorm(30))
second_t_test_result <- t.test(second_sample, mu = 0)
p_list_3 <- append(p_list_3, second_t_test_result$p.value)
}
else {
p_list_3 <- append(p_list_3, first_t_test_result$p.value)
}
}
Now call this function 10k times and find out what happens.
p_list_3 <- c()
set.seed(252)
p_list_3 <- replicate(10000, double.sample())
Is there an inflation of false positives? How bad is it?
mean(p_list_3 < 0.05)
## [1] 0.0747
Yes, the false positive rate rises from ~0.05 to 0.0747.
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.sample_2 <- function (upper_value) {
first_sample <- rnorm(30)
first_t_test_result <- t.test(first_sample, mu = 0)
if (first_t_test_result$p.value < upper_value & first_t_test_result$p.value > 0.05) {
second_sample <- append(first_sample, rnorm(30))
second_t_test_result <- t.test(second_sample, mu = 0)
return (second_t_test_result$p.value)
}
else {
return(first_t_test_result$p.value)
}
}
upper_value_list <- seq(0.05, 1, 0.05)
p_df <- data.frame(upper_value = numeric(),
fp = numeric())
set.seed(251)
for (i in upper_value_list){
p_list_4 <- c()
for (j in 1:50000){
p_list_4 <- append(p_list_4, double.sample_2(i))
}
p_df <- p_df %>%
add_row(upper_value = i, fp = mean(p_list_4 < 0.05))
}
plot(p_df$fp ~ p_df$upper_value, type = "b",
main = "False positive rate with respect to upper p value",
xlab = "Upper p value",
ylab = "False positive rate")
p_df$fp[which(round(p_df$upper_value, 2) == 0.5)]
## [1] 0.0796
p_df$fp[which(round(p_df$upper_value, 2) == 0.75)]
## [1] 0.08336
p_df$fp[which(round(p_df$upper_value, 2) == 1)]
## [1] 0.08338
The false positive rate becomes 0.0796, 0.08336 and 0.08338 when the threshold is raised to p < 0.5, p < 0.75, and p < 1, respectively.
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
Overall, false positive rate inflates more if the researcher resamples when seeing a higher upper p value. This method is bad because the probability that the researcher will reject the null hypothesis falsely (Type I error) increases.