Psych 251 PS4: Simulation + Analysis

Author

Morgan Tompkins

Published

December 31, 2024

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(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")
head(fvs)
  subid      age    condition  hand.look
1     2 3.156164 Faces_Medium 0.03187500
2    93 5.030137 Faces_Medium 0.11885333
3    29 5.852055 Faces_Medium 0.09212000
4    76 5.852055 Faces_Medium 0.12961111
5    48 6.082192 Faces_Medium 0.01378571
6   101 6.147945 Faces_Medium 0.04384706

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.

# unique dataset
fvs_unique <- fvs %>%
  distinct(subid, age)

# plot
ggplot(fvs_unique, aes(x = age)) +
  geom_histogram(binwidth = 1, color = "white") +
  labs(
    title = "Distribution of Ages in FVS (2011)",
    x = "Age (months)",
    y = "Number of Children"
  ) +
  theme_minimal()

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.

# create dataset
fvs_age_condition <- fvs %>%
  group_by(subid, age, condition) %>%
  summarise(
    avg_hands = mean(hand.look, na.rm = TRUE),
    .groups = "drop"
  )

# plot
ggplot(fvs_age_condition, aes(x = age, y = avg_hands, color = condition)) +
  geom_point(alpha = 0.6) +
  geom_smooth(se = FALSE, method = "loess") +
  labs(
    title = "Hand looking as a Function of Age and Condition",
    x = "Age (months)",
    y = "Proportion of Looking to Hands",
    color = "Condition"
  ) +
  theme_minimal(base_size = 12)
`geom_smooth()` using formula = 'y ~ x'

What do you conclude from this pattern of data?

For kids looking at faces with a white background, the attention paid to hands generally increases with age, especially after about 10 months. However, for more complex backgrounds, we see more of an upside-down U pattern after 10 months. Between 10 and 20 months, kids spend increased time looking at hands in moving scenes with complex backgrounds, but that sharply decreases after around 20 months. However, for both conditions, the ages of the participants are clustered heavily around the 10–15 month range, with only a few observations past 20 months. Therefore, the data at the right tail of the graph may be misleading.

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

I’d use a mixed-effects regression model with age, condition, and their interaction to test whether the two conditions show different developmental patterns and respects the actual structure of the data.I’d include a quadratic term for age to account for the non-linear shapes in the scatter plot.

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(123)

n_sims <- 10000
p_values <- numeric(n_sims)

for (i in 1:n_sims) {
  x <- rnorm(30, mean = 0, sd = 1)
  test <- t.test(x, mu = 0)
  p_values[i] <- test$p.value
}


mean(p_values < 0.05)
[1] 0.0465

Next, do this using the replicate function:

p_values_rep <- replicate(
  10000,
  {
    x <- rnorm(30, mean = 0, sd = 1)
    t.test(x, mu = 0)$p.value
  }
)

mean(p_values_rep < 0.05)
[1] 0.0496

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

Both simulations produce a false positive rate very close to 0.05, which is what we would expect at alpha = 0.05 because that alpha level means that we should incorrectly reject the null about five percent of the time purely by chance.

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() {
  x1 <- rnorm(30, mean = 0, sd = 1)
  p1 <- t.test(x1, mu = 0)$p.value
  if (p1 < 0.05) {
    return(p1)  
  } else if (p1 > 0.25) {
    return(p1)   
  } else {
    x2 <- rnorm(30, mean = 0, sd = 1)
    x_all <- c(x1, x2)
    p2 <- t.test(x_all, mu = 0)$p.value
    return(p2)
  }
}

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

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

mean(pvals_sniff < 0.05)
[1] 0.0755

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

Yes, the false positive rate is inflated. Instead of the expected 5 percent, the optional-sampling rule produces a false positive rate of about 7.6 percent.

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_p) {
  x1 <- rnorm(30, mean = 0, sd = 1)
  p1 <- t.test(x1, mu = 0)$p.value
  
  if (p1 < 0.05) {
    return(p1)
  } else if (p1 > upper_p) {
    return(p1)
  } else {
    x2 <- rnorm(30, mean = 0, sd = 1)
    x_all <- c(x1, x2)
    p2 <- t.test(x_all, mu = 0)$p.value
    return(p2)
  }
}

set.seed(123)

sim_fp <- function(upper_p, n_sims = 50000) {
  pvals <- replicate(n_sims, double.sample(upper_p))
  mean(pvals < 0.05)
}

library(dplyr)
library(purrr)

thresholds <- tibble(
  rule = c("p < .25", "p < .50", "p < .75", "any nonsig"),
  upper_p = c(0.25, 0.50, 0.75, 1.00)
)

results <- thresholds %>%
  mutate(false_positive = map_dbl(upper_p, sim_fp))

results
# A tibble: 4 × 3
  rule       upper_p false_positive
  <chr>        <dbl>          <dbl>
1 p < .25       0.25         0.0702
2 p < .50       0.5          0.0783
3 p < .75       0.75         0.0828
4 any nonsig    1            0.0852

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

The more p-sniffing or the more willing a researcher is to double a sample if they think they are close to a significant result, the more false positives we will see in the literature. The original rule (.05 < p < .25) already pushes the Type I error from 0.05 to about 0.07. Expanding that window to .50, .75, or literally any nonsignificant p-value just keeps inflating it even worse. Going from 0.05 to 0.085 is a 70% increase in false positives, meaning that significant results would need to be taken with much more scrutiny than if we did not engage in the practice as a field.