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.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.4 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.4 ✓ stringr 1.4.0
## ✓ readr 2.0.2 ✓ 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")
## Rows: 232 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): condition
## dbl (3): subid, age, hand.look
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
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.
fvswider <- fvs %>%
pivot_wider(names_from = condition,
values_from = hand.look)
ggplot(fvswider, aes(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, col = condition)) +
geom_point() +
geom_smooth(method = lm) +
theme_classic() +
xlab("Age in Months") +
ylab("Looking at Hands") +
theme(legend.position = "top")
## `geom_smooth()` using formula 'y ~ x'
What do you conclude from this pattern of data?
I would conclude that there is a difference in looking to hands between the Faces_Medium and Faces_Plus conditions, but only in older children (12-15 months and older). Children in the Faces_Plus condition seem to have higher looking to hands than children in the Faces_Medium condition, but only when children were older than about 15 months. It’s a bit hard to say though, since the observations of children older than 20 months is a bit sparse.
What statistical analyses would you perform here to quantify these differences?
One thing you could do is actually fit linear models and then do significance testing to test whether the regression coefficients are different from one another at p < .05. You could also fit multiple regression models and test that way. Another thing you could do is to median split by age and then do an ANOVA and test for an interaction between age and 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.
df <- data.frame(0,1)
for(simulation in 1:10000) {
dist <- rnorm(30, mean = 0, sd = 1)
test <- t.test(dist, y = NULL, alternative = "two.sided")
df[nrow(df) + 1,] = c(simulation, test[3])
}
df <- df[-1,]
total <- df %>%
filter(X1 <= .05) %>%
nrow()
total/10000
## [1] 0.0452
Next, do this using the replicate function:
sum(replicate(10000, t.test(rnorm(30, mean = 0, sd = 1), y = NULL, alternative = "two.sided")[3] <= .05))/10000
## [1] 0.0488
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
This lines up exactly with our intuitions of the false-positive rate of an alpha of .05: roughly .05 of the time we got false positives.
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 () {
dist1 <- rnorm(30, mean = 0, sd = 1)
p <- t.test(dist1, y = NULL, alternative = "two.sided")[3]
if(p <= .25 & p > .05){
dist2 <- rnorm(30, 0, 1)
fulldist <- append(dist1, dist2)
t.test(fulldist, y = NULL, alternative = "two.sided")[3]
}
else {
p
}
}
Now call this function 10k times and find out what happens.
sum(replicate(10000, double.sample() <= .05))/10000
## [1] 0.0683
Is there an inflation of false positives? How bad is it?
The false positive rate is slightly inflated here from the original one of .05, here is about .07, a difference of 2 percent. While small, it’s enough to make the difference for a lot more results and inflates the false positive rate.
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 <- function (pvalue = .25) {
dist1 <- rnorm(30, mean = 0, sd = 1)
p <- t.test(dist1, y = NULL, alternative = "two.sided")[3]
if(p <= pvalue & p > .05){
dist2 <- rnorm(30, 0, 1)
fulldist <- append(dist1, dist2)
t.test(fulldist, y = NULL, alternative = "two.sided")[3]
}
else {
p
}
}
pointtwofive <- replicate(25, sum(replicate(10000, double.sample2() <= .05))/10000)
pointtwofive
## [1] 0.0736 0.0739 0.0730 0.0732 0.0732 0.0775 0.0711 0.0691 0.0693 0.0718
## [11] 0.0670 0.0712 0.0744 0.0677 0.0735 0.0679 0.0690 0.0728 0.0665 0.0713
## [21] 0.0756 0.0732 0.0698 0.0701 0.0719
mean(pointtwofive)
## [1] 0.071504
sd(pointtwofive)
## [1] 0.002760507
pointfive <- replicate(25, sum(replicate(10000, double.sample2(.5) <= .05))/10000)
pointfive
## [1] 0.0787 0.0783 0.0727 0.0772 0.0764 0.0832 0.0778 0.0777 0.0809 0.0770
## [11] 0.0733 0.0819 0.0803 0.0751 0.0818 0.0796 0.0802 0.0774 0.0802 0.0806
## [21] 0.0797 0.0797 0.0791 0.0771 0.0773
mean(pointfive)
## [1] 0.078528
sd(pointfive)
## [1] 0.002537801
pointsevenfive <- replicate(25, sum(replicate(10000, double.sample2(.75) <= .05))/10000)
pointsevenfive
## [1] 0.0859 0.0747 0.0825 0.0819 0.0803 0.0854 0.0800 0.0868 0.0817 0.0811
## [11] 0.0810 0.0830 0.0791 0.0812 0.0855 0.0834 0.0839 0.0830 0.0849 0.0775
## [21] 0.0805 0.0835 0.0816 0.0817 0.0869
mean(pointsevenfive)
## [1] 0.08228
sd(pointsevenfive)
## [1] 0.002861235
allnonsig <- replicate(25, sum(replicate(10000, double.sample2(1) <= .05))/10000)
allnonsig
## [1] 0.0838 0.0886 0.0834 0.0893 0.0850 0.0853 0.0833 0.0844 0.0813 0.0874
## [11] 0.0865 0.0854 0.0846 0.0851 0.0798 0.0864 0.0855 0.0783 0.0861 0.0803
## [21] 0.0881 0.0825 0.0839 0.0877 0.0850
mean(allnonsig)
## [1] 0.08468
sd(allnonsig)
## [1] 0.002739069
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
It seems that the more lenient you are with doubling your sample, the higher your chances of getting a false positive: up to to an increase of about 3.3% from the original alpha level if you choose to resample anytime you have a nonsignificant result. This difference appears to be marginally dimishing as you move the decision line up, and the variance appears roughly the same across significance levels. I would say this data-dependent policy is pretty bad - this is a case where we actually know there is no difference between the sampled distribution and the null, and where we only double the sample once, and we still see pretty large increases in false positive rates. Here we see an inflation of the false positive rate of about 2% even when the researcher is relatively less optimistic. In the real world, people might check their p-values more frequently, and just keep sampling until they reach significance. So this data-dependent policy is likely implemented in a much worse way in the real world, and only serves to increase the amount of false positives that we end up publishing as significant. This is why we just need to do our power analyses then pre-register and not keep sampling until significance :)