Psych 251 PS4: Simulation + Analysis

Author

Mike Frank

Published

December 31, 2018

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).

library(readr)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.4.3
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.

fvs %>%
  distinct(subid, .keep_all = TRUE) %>%
  ggplot(aes(x = age)) +
  geom_histogram() +
  labs(
    title = "Age Distribution",
    x = "Age (months)",
    y = "Count"
  ) +
  theme_minimal()
`stat_bin()` using `bins = 30`. Pick better value `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.

fvs %>%
  ggplot(aes(x = age, y = hand.look, color = condition)) + 
  geom_point(alpha = 0.7) +
  geom_smooth(method = "lm", se = TRUE) + 
  labs(
    title = "Hand looking as a function of age and condition", 
    x = "Age (months)", 
    y = "Hand looking (seconds)",
    color = "Condition"
  ) +
  theme_minimal()
`geom_smooth()` using formula = 'y ~ x'

What do you conclude from this pattern of data?

It looks like in both conditions hand looking increases with age (older participants spend more time looking at hands during stimuli presentation regardless of condition). The line is steeper for the Faces Plus condition which suggest that as age increases, participants in this condition show a larger incrase in hand-looking compared to the faces medium condition. The younger children show very little hand looking across both conditions and older children show more variable attention to hand looking.

What statistical analyses would you perform here to quantify these differences?

To understand if there is a difference in hand looking as a function of age and condition, we could run a linear model with interaction between age and condition. Here, we’d see if the slopes are different and if age is predicting any differences in hand-looking across conditions.

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.

set.seed(2025) # for reproducibility 

n_reps <- 10000 # setting repetitions at 10,000
p_vals <- numeric(n_reps) # to store p-values 

for (i in 1:n_reps) {
  x <- rnorm(30, mean = 0, sd = 1) # sampling 30 observations from normally distributed data 
  t_output <- t.test(x, mu = 0) # one sample t test against mean 0
  p_vals[i] <- t_output$p.value
}

# finding proportion of false pos's 
mean(p_vals < 0.05)
[1] 0.0522

Next, do this using the replicate function:

set.seed(2025)

# using replicate ()
p_vals_replicate <- replicate(
  10000, # 10000 reps
  t.test(rnorm(30, mean = 0, sd = 1), mu = 0)$p.value
)

mean(p_vals_replicate < 0.05)
[1] 0.0522

How does this compare to the intended false-positive rate of \(\alpha=0.05\)?

The observed false positive rate in our simulation is veyr close to the intended false positive rate of 0.05.

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 30 participants 
  x1 <- rnorm(30, mean = 0, sd = 1)
  p1 <- t.test(x1, mu = 0)$p.value
  
  # additing the conditional stop 
  if (p1 < 0.05 || p1 > 0.25) {
    return(p1)
  }
  # otherwise, collect 30 more and retest with the hext 30
  x2 <- rnorm(30, mean = 0, sd = 1)
  x_total <- c(x1, x2)
  p2 <- t.test(x_total, mu = 0)$p.value
  
  # pring p2
  return(p2)
}

Now call this function 10k times and find out what happens.

set.seed(2025)

p_vals_sniffing <- replicate(10000, double.sample())

mean(p_vals_sniffing < 0.05)
[1] 0.0695

Is there an inflation of false positives? How bad is it?

Yes, there is an increase in false positives. We go from ~5% to ~7%.

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.

double.sample <- function(upper_cutoff = 0.25) {
  # initial 30 
  x1 <- rnorm(30, mean = 0, sd = 1)
  p1 <- t.test(x1, mu = 0)$p.value
  
  # scenario 1 (doubles the sample whenever their pvalue is not significant, but it's less than 0.5): 
  if( p1 <0.05) {
    return(p1)
  }
  
  # scenario 2 (doubles the sample whenever their pvalue is not significant, but it's less than 0.75):
  if(p1 > upper_cutoff) {
    return(p1)
  }
  
  # otherwise double 
  x2 <- rnorm(30, mean =0, sd = 1)
  x_total <- c(x1, x2)
  p2 <- t.test(x_total, mu = 0)$p.value
  
  return(p2)
}
set.seed(2025)

n_reps <- 10000 

# scenario 1
p_50 <- replicate(n_reps, double.sample(0.5))
mean(p_50 < 0.05)
[1] 0.0766
# scenario 2
p_75 <- replicate(n_reps, double.sample(0.75))
mean(p_75 < 0.05)
[1] 0.0849
# scenario 3
p_1 <- replicate(n_reps, double.sample(1.00))
mean(p_1 < 0.05)
[1] 0.0855
set.seed(2025)

n_reps <- 15000 

# scenario 1
p_50 <- replicate(n_reps, double.sample(0.5))
mean(p_50 < 0.05)
[1] 0.07813333
# scenario 2
p_75 <- replicate(n_reps, double.sample(0.75))
mean(p_75 < 0.05)
[1] 0.0842
# scenario 3
p_1 <- replicate(n_reps, double.sample(1.00))
mean(p_1 < 0.05)
[1] 0.08173333

What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?

This simulation shows that optional stopping in research increases the likelihood of false positives even in mild versions (like some of the scenarios we were testing). Such data-dependent sampling rules can distort published findings and it’s an easy rule to bend because researchers often check their data as they can and can find themsleves adjusting their sample sizes without adjusting their inference proceures.