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("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.3     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ 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
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.
view(fvs)

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.

distinct(fvs |> select(subid, age)) |> # Remove duplicates
ggplot(aes(x=age)) + geom_histogram(bins = 28) + # 28 bins, i.e. one bin per month, seems like a decent choice.
  scale_x_continuous(name = "age (months)") +
  scale_y_continuous(name = "number of participants")

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() +
  scale_x_continuous(name = "age (months)") +
  scale_y_continuous(name = "looking at hands (%)") +
  scale_color_brewer(palette="Paired") +
  geom_smooth(method = 'lm', se = TRUE)
`geom_smooth()` using formula = 'y ~ x'

What do you conclude from this pattern of data?

Children in the Faces_Plus condition looked more at hands than children of the same age in the Faces_Medium condition, and in both conditions older children looked more at hands than younger children did.

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

We could conduct an independent t-test, because we’re comparing two groups (Faces_Medium and Faces_Plus) to test for a difference (average % looking at hands), and we assume the groups are approximately normally distributed.

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.

n <- 10000
num_significant <- 0

for (x in 1:n) {
  if (t.test(rnorm(30,mean=0,sd=1), y = NULL, mu = 0)$p.value < .05) {
    num_significant = num_significant + 1
  }
}

print((num_significant / n) * 100)
[1] 4.66

Next, do this using the replicate function:

vec <- replicate(n, t.test(rnorm(30,mean=0,sd=1), y = NULL, mu = 0)$p.value < .05)
print((length(vec[vec == TRUE]) / n)*100)
[1] 5.01

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

The false-positive rate we obtain using either method is very close to the intended false-positive rate of \(\alpha=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.

I find the scenario somewhat ambiguous. Does this mean that (Interpretation 1) the experimenter adds 30 additional participants at most once (i.e., either collects 30 or 60 observations) or (Interpretation 2) do they repeat the procedure indefinitely (i.e., always adding 30 more participants) until they’ve hit a significant result or else a p value > .25?

First, write a function that implements this sampling regime.

Interpretation 1

double.sample <- function(samples,upperpvalue) {
  count = 0
  data = rnorm(samples,mean=0,sd=1)
  pval = t.test(data)$p.value
  if (pval > 0.05 & pval < upperpvalue) {
    data = c(data, rnorm(samples))
    pval = t.test(data)$p.value
  }
  if (pval <= 0.05) {
    # Success!
    count = 1
  }
  return (count)
}

Interpretation 2

double.sample2 <- function(samples,upperpvalue) {
  count = 0
  data = rnorm(samples,mean=0,sd=1)
  pval = t.test(data)$p.value
  while (pval > 0.05) {
    if (pval > upperpvalue) {
      break
    }
    data = c(data, rnorm(samples))
    pval = t.test(data)$p.value
  }
  if (pval <= 0.05) {
    # Success!
    count = 1
  }
  return (count)
}

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

n <- 10000
samplesize <- 30
upperpvalue <- 0.25

proportion <- (sum(replicate(n,double.sample2(samplesize,upperpvalue))) / n) * 100
print(proportion)
[1] 8.58

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

Interpretation 1: Yes, there is a slight inflation of false positives, roughly 1.5-2.5 percentage points more (around 6.5-7.5%).

Interpretation 2: Yes, there is a slight inflation of false positives, roughly 3.5-4 percentage points more (around 8.5-9%).

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 researcher doubles their sample whenever they get ANY pvalue that is not significant.

How do these choices affect the false positive rate?

Interpretation 1: For upperpvalue = 0.5: around 8%, For upperpvalue = 0.75: around 8.3%, For upperpvalue = 1: around 8.6%

Interpretation 2: For upperpvalue = 0.5: around 12.5%, For upperpvalue = 0.75: around 18.5%; For upperpvalue = 1: (code runs potentially forever)

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.

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

Interpretation 1: The data-dependent policy seems somewhat bad, but the false-positive rate is still relatively small.

Interpretation 2: The data-dependent policy seems pretty bad, as we may get a false-positive every fifth experiment or so.