Psych 251 PS4: Simulation + Analysis

Author

Dorothy Zhao

Published

January 1, 2025

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

df_unique_age <- fvs %>%
  group_by(subid) %>%
  summarize(age = unique(age))

ggplot(df_unique_age, aes(x = age)) +
  geom_histogram(bins = 10) +
  labs(x = "Age (months)", y="Count") +
  theme_classic()

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)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(x = "Age (months)", y="Percentage Hand Looking") +
  theme_classic() + 
  scale_color_discrete(labels = c("Faces Medium", "Faces Plus"))
`geom_smooth()` using formula = 'y ~ x'

What do you conclude from this pattern of data?

Infants when shown the “Faces Plus” condition (i.e., looking at more complex backgrounds with kids and adults) spend a a greater percentage of time looking at the hands compared to when they are shown the “Faces Medium” condition (i.e., looking at a simple background with only a kid). This difference is relatively more pronounced for older infants.

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

I would first perform a paired-sample t-test to compare the percentage of hand-looking between the Faces Medium and Faces Plus condition to see whether infants do look at hands more in the faces plus condition. Then, I would also run a linear mixed-effect model with percentage hand-looking as the dependent variable, an interaction effect for age and condition, and a random effect for subject.

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(1)
count <- 0
total <- 10000
for (i in 1:total) {
  sample_a <- rnorm(30)
  t_test <- t.test(sample_a, mu = 0, alternative = "two.sided")
  pvalue <- t_test$p.value
  if (pvalue < 0.05) {
    count <- count + 1
  }
}

print(count / total)
[1] 0.0502

Next, do this using the replicate function:

pvals <- replicate(1000, {
  sample_a <- rnorm(30)
  t.test(sample_a, mu = 0)$p.value
})

sum(pvals < 0.05)    
[1] 44
mean(pvals < 0.05)   
[1] 0.044

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

In both cases, the simulations are approximately similar to the false positive rate of \(\alpha=0.05\) — 0.050 for the for-loop simulation and 0.044 for the replicate function.

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() {
  sample_1 <- rnorm(30)
  pval <- t.test(sample_1, mu = 0)$p.value
  if (pval < 0.05 || pval > 0.25) {
    pval
  } else {
    sample_2 <- rnorm(30)
    combined <- c(sample_1, sample_2)
    p2 <- t.test(combined, mu = 0)$p.value
    p2
  }
}

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

pvals <- replicate(10000, {
  double.sample()
})

sum(pvals < 0.05)  
[1] 713
mean(pvals < 0.05)
[1] 0.0713

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

Yes, there is an inflation of false positives. Compared to the expected false positive rate of \(\alpha=0.05\), we see that this sampling strategy leads to significant p-values in 7.1% of samples. This is an absolute increase of 2.1% or a 42.6% percent increase.

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_mod <- function(p_thresh) {
  sample_1 <- rnorm(30)
  pval <- t.test(sample_1, mu = 0)$p.value
  if (pval < 0.05 || pval > p_thresh) {
    pval
  } else {
    sample_2 <- rnorm(30)
    combined <- c(sample_1, sample_2)
    p2 <- t.test(combined, mu = 0)$p.value
    p2
  }
}

pvals_cond1 <- replicate(50000, {
  double.sample_mod(p_thresh=0.5)
})
sum(pvals_cond1 < 0.05) 
[1] 3988
mean(pvals_cond1 < 0.05)
[1] 0.07976
pvals_cond1 <- replicate(50000, {
  double.sample_mod(p_thresh=0.75)
})
sum(pvals_cond1 < 0.05) 
[1] 4073
mean(pvals_cond1 < 0.05)
[1] 0.08146
pvals_cond1 <- replicate(50000, {
  double.sample_mod(p_thresh=1)
})
sum(pvals_cond1 < 0.05) 
[1] 4197
mean(pvals_cond1 < 0.05)
[1] 0.08394

These choices impact false positive rate. As the researcher becomes more “optimistic” or more lenient as to when to resample data (by increasing the upper bound of pvalues they are willing to resample for), we see the false positive rataes also increases, ranging from 7.9% when the upper bound is 0.5 to 8.5% .

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

Based on this simulation, it is clear that this data-dependent policy will lead to an inflation of false positives. Moreover, when we take a lenient approach of continuing to sample after not getting a significant result (and not setting and upper-bound threshold), the percentage of significant results is 8.5%, which is approximately a 70% increase over the expected false positive rate of 0.05.