Psych 251 PS4: Simulation + Analysis

Author

Sebastian Kane

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(tidyverse)
fvs <- read_csv("data/FVS2011-hands.csv")

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 |> 
  group_by(subid) |> 
  slice(1) |> 
  ungroup() |> 
  ggplot(aes(x = age)) +
  geom_histogram() +
  labs(x = 'Age (Months)', y = 'Frequency', title = "Histogram of Children's Age") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_y_continuous(breaks = seq(0, 13, by = 2))
`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() + 
  labs(x = 'Age (Months)', y = '% Time Looking At Hands', 
       title = "Hand Looking By Age and Condition", 
       color = 'Condition') + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) + 
  scale_color_discrete(labels = c("Faces Medium", "Faces Plus")) + 
  geom_smooth(method = "lm")
`geom_smooth()` using formula = 'y ~ x'

What do you conclude from this pattern of data?

Across both conditions, there appears to be a moderate positive association between a child’s age and the percentage of time they spend looking at hands. Children in the Faces Medium condition tend to spend less time looking at hands than those in the Faces Plus condition. The relationship between age and hand looking also appears more positive for children in the Faces Plus condition.

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

First, I would test the correlation between age and hand looking for each of the groups. Then, I would want to conduct a paired t-test to see if childen in the Faces Plus and Faces Medium conditions on average look at hands different amounts. Next, I would run a regression model with hand looking as the dependent variable to test an interaction between condition and age. With this method, I could test if the slopes for the two conditions are truely different.

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.

sig_count_list <- c()

for (i in 1:10000) {
  
  samp <- rnorm(30)
  p_val <- t.test(samp)$p.value
  is_sig <- p_val < 0.05
  sig_count_list = c(sig_count_list, is_sig)
}

cat('Proportion of Significant Results:', mean(sig_count_list))
Proportion of Significant Results: 0.0451

Next, do this using the replicate function:

sig_count_list_replicate <- replicate(
  n = 10000, 
  t.test(rnorm(30))$p.value < 0.05
  ) 

cat('Proportion of Significant Results:', mean(sig_count_list_replicate))
Proportion of Significant Results: 0.049

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

Both results are fairly close to the intended false-positive rate of \(\alpha=0.05\). Currently, the results are just below (0.0492) and just above (0.0513) the value, though the numbers will likely change after this page is re-run when rendered.

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(upper_p) {
  
  samp <- rnorm(30)
  p_val <- t.test(samp)$p.value
  
  if (p_val < 0.05) {
    return(TRUE)
  } else if (p_val > upper_p) {
    return(FALSE)
  } else {
    samp2 <- c(samp, rnorm(30))
    p_val2 <- t.test(samp2)$p.value
    return(p_val2 < 0.05)
  }
  
}

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

sig_count_list_double <- replicate(10000, double.sample(upper_p = 0.25))

cat('Proportion of Significant Results:', mean(sig_count_list_double))
Proportion of Significant Results: 0.0754

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

Yes, there appears to be a greater than expected number of false positives. The false positive rate was 7.54%, over 2% greater than want is expected using a standard sampling procedure.

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.

# The researcher doubles the sample whenever their pvalue is not significant, but it's less than 0.5.
sig_count_list_double_50 <- replicate(10000, double.sample(upper_p = 0.50))
cat('Proportion of Significant Results:', mean(sig_count_list_double_50))
Proportion of Significant Results: 0.0787
# The researcher doubles the sample whenever their pvalue is not significant, but it's less than 0.75.
sig_count_list_double_75 <- replicate(10000, double.sample(upper_p = 0.75))
cat('Proportion of Significant Results:', mean(sig_count_list_double_75))
Proportion of Significant Results: 0.0795
# The research doubles their sample whenever they get ANY pvalue that is not significant.
sig_count_list_double_100 <- replicate(10000, double.sample(upper_p = 1))
cat('Proportion of Significant Results:', mean(sig_count_list_double_100))
Proportion of Significant Results: 0.0831

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

This procedure fairly problematic because it signifiantly inflates the rate of false positives. As you increase the p-value theshold for doubling the sample, the proportion of “significant” results increases from 0.0754 to 0.0831. Thus, using the most extreme method, “significant” results are over 50% more common than would be expected.