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

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.4     ✔ 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

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

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.

ggplot(data=fvs %>% distinct(subid, age), aes(x=age)) + geom_histogram() + ggtitle("Histogram of the ages of participants in Frank, Vul, Saxe (2011, Infancy)") + xlab("Age (Months)") + ylab("Count")
`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.

ggplot(data=fvs, aes(x=age, y=hand.look, color=condition)) + geom_point() + geom_smooth(method="lm") + ylab("Hand looking time") + xlab("Age (Months)") +ggtitle("Hand looking as a function of age and condition in Frank, Vul, Saxe (2011, Infancy)") +
labs(color="Condition") + scale_color_manual(labels = c("Faces Medium", "Faces Plus"), values = c("blue", "red")) 
`geom_smooth()` using formula = 'y ~ x'

What do you conclude from this pattern of data?

As age increases, looking time toward hands increases, with the slope of the effect appearing to be positive in both conditions but steeper in the “Faces Plus” group.

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

One possibility for quantifying these differences could be a multiple linear model, with age, condition, and the interaction between age and condition as predictors and hand looking time as the response variable.

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.

num_significant = 0
for (i in 1:10000){
  experiment = rnorm(n=30)
  test = t.test(experiment)
  if (test$p.value < 0.05){
    num_significant = num_significant +  1
  }
}

prop_significant = num_significant / 10000
print(prop_significant)
[1] 0.0436

Next, do this using the replicate function:

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

prop_significant <- mean(experiments)
print(prop_significant)
[1] 0.049

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

These results are very similar 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() {
  initial_sample <- rnorm(30)
  initial_pvalue = t.test(initial_sample)$p.value
  if (initial_pvalue < 0.05 || initial_pvalue > 0.25){
    return(initial_pvalue)
  }
  else {
    additional_sample <- rnorm(30)
    total_sample = c(initial_sample, additional_sample)
    return (t.test(total_sample)$p.value)
  }
}

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

experiments = replicate(10000, as.numeric(double.sample() < 0.05))

prop_significant <- mean(experiments)
print(prop_significant)
[1] 0.0734

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

There is quite a bad inflation of false positives of almost 50%.

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_revised <- function(sample_size, p_to_double){
  initial_sample <- rnorm(sample_size)
  initial_pvalue = t.test(initial_sample)$p.value
  if (initial_pvalue < 0.05 || initial_pvalue > p_to_double){
    return(initial_pvalue)
  }
  else {
    additional_sample <- rnorm(sample_size)
    total_sample = c(initial_sample, additional_sample)
    return (t.test(total_sample)$p.value)
  }
}
sample30_p_to_double_0.5 <- mean(replicate(50000, as.numeric(double.sample_revised(30, 0.5) < 0.05)))
print(sample30_p_to_double_0.5)
[1] 0.0783
sample30_p_to_double_0.75 <-mean(replicate(50000, as.numeric(double.sample_revised(30, 0.75) < 0.05)))
print(sample30_p_to_double_0.75)
[1] 0.08386
sample30_p_to_double_any <-mean(replicate(50000, as.numeric(double.sample_revised(30, 1) < 0.05)))
print(sample30_p_to_double_any)
[1] 0.08428

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

This data-dependent policy is very bad. It produces inflations in false positives that are above 50%, and while increasing the p-cutoff based on which you double the sample makes the inflation worse, inflation of almost 50% occurs even when using p=0.25 as the cutoff for doubling the sample size.