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).
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.
library(ggplot2)library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.3 ✔ stringr 1.5.0
✔ forcats 1.0.0 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
`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.
# having weird errors upon render...# https://stackoverflow.com/a/38171694if(!require("pacman")) install.packages("pacman")
As children get older, they pay more attention to hands in scenes with (complex) background information than in scenes without background. This could mean that development drives kids to increasingly recognize the agency of others, since hands are more meaningful in environments they can interact with rather than opaque environments.
What statistical analyses would you perform here to quantify these differences?
We’d want a test to see if the slope of the lines are different. So I would use a linear mixed effects model predicting hand looking time with Age as a predictor, condition as a fixed effect, and random fixed effect(?) for participant. Then I would test if there is an interaction effect between condition and age (?).
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.
library(infer)# run samplesnum_samples <-10000allresults <-tibble()for (i in (1:num_samples)) { sample <-tibble(value=rnorm(30)) tib <-t_test(sample, response=value) allresults <-bind_rows(allresults, tib)}allresults <-bind_rows(allresults)# calculate proportionalpha =0.05allresults <- allresults |>mutate(significant = p_value < alpha)# gotta be a better way to do thisnum_sig <- allresults |>count(significant)num_sig |>group_by(significant) |>summarize(freq = n /nrow(allresults))
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
Compares about equal.
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.
alpha <-0.05upperlimit <-0.25getpvalue <-function(t) {# assumes t is a tibble with one column "obs" results <-t_test(t, response=obs) pvalue <- results$p_value[[1]]# return pvalue}pvaluesettled <-function(t) { pvalue <-getpvalue(t) settled <- pvalue < alpha || pvalue > upperlimit # return settled}double.sample <-function() { ppts =tibble(obs=rnorm(30))if (!pvaluesettled(ppts)) { ppts =bind_rows(ppts, tibble(obs=rnorm(30))) }# returngetpvalue(ppts)}
Now call this function 10k times and find out what happens.
num_samples <-10000allresults <-bind_rows(pvalue=replicate(num_samples, double.sample(), simplify="array"))# calculate proportionalpha =0.05allresults <- allresults |>mutate(significant = pvalue < alpha)# gotta be a better way to do thisnum_sig <- allresults |>count(significant)num_sig |>group_by(significant) |>summarize(freq = n /nrow(allresults))
Is there an inflation of false positives? How bad is it?
Yes, to 0.0742 . This is quite bad because it essentially violates the whole point of what a pvalue of 0.05 means. A whole “2%” of results in research would be… plain false.
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.
pvaluesettled <-function(t, upperlimit) { pvalue <-getpvalue(t) settled <- pvalue < alpha || pvalue > upperlimit # return settled}double.sample <-function(pvalueupperlimit) { ppts =tibble(obs=rnorm(30))if (!pvaluesettled(ppts, pvalueupperlimit)) { ppts =bind_rows(ppts, tibble(obs=rnorm(30))) }# returngetpvalue(ppts)}num_samples <-10000allresults <-bind_rows(pvalue=replicate(num_samples, double.sample(0.5), simplify="array"))# calculate proportionalpha =0.05allresults <- allresults |>mutate(significant = pvalue < alpha)# gotta be a better way to do thisnum_sig <- allresults |>count(significant)num_sig |>group_by(significant) |>summarize(freq = n /nrow(allresults))
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
It’s pretty bad. Even the practice of doubling your sample once (which seems pretty widespread to me) results in a dramatic increase in false positive results in science. Considering societally affecting decisions are often made on the basis of such results, it seems reasonable to expect that such “knowledge” leads people to make bad decisions.