Psych 251 PS4: Simulation + Analysis

Author

Mike Frank (Problem set completed by Verity Lua)

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)
library(jtools) # for theme_apa()
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 %>% 
  dplyr::distinct(subid, .keep_all = TRUE) %>%
  ggplot(aes(x = age)) +
  geom_histogram(binwidth = 1) +
  labs(x = "Age", y = "Count") +
  theme_apa()

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() +
  geom_smooth(method = "lm") +
  labs(x = "Age", y = "Time Spent Looking at Hands") + 
  theme_apa()
`geom_smooth()` using formula = 'y ~ x'

What do you conclude from this pattern of data?

In both conditions, there appears to be a positive relationship between age and time spent looking at hands. This positive relationship appears to be stronger in the Faces_Medium condition relative to the Faces_Plus condition. Nonetheless, based on the scatter plot, the relationship between time spent looking at hands and age may not be linear (e.g., it may be exponential in the Faces_Medium Condition, based on the scatter plot).

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

Linear mixed model. Since this is a within-persons experiment (where infants go throguh both conditions), the data points are not independent of each other. A linear mixed model will be able to account for the non independence of the data points. I would look at the value and significance of an interaction term between age and condition (age * condition) predicting time spend on looking at hands (i.e., hand_looking ~ age*condition + (1 | subid)).

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.

# parameters
significant_result <- 0
n_tests <- 10000

# simulation
for (i in 1:n_tests) {
  data <- rnorm(n = 30, mean = 0, sd = 1)
  p_val <- t.test(data)$p.value
  if (p_val < 0.05) {
    significant_result = significant_result + 1
  }
}

# find proportion of signfiicant results
print(paste0("The proportion of significant result is ", significant_result / 10000))
[1] "The proportion of significant result is 0.0504"

Next, do this using the replicate function:

# parameters
n_tests <- 10000

# simulation
significant_result <- 
  replicate(n_tests, {
            data <- rnorm(n = 30, mean = 0, sd = 1)
            t.test(data)$p.value < .05 
            }) %>%
  sum()
     
# find proportion of signfiicant results
print(paste0("The proportion of significant result is ", significant_result / 10000))     
[1] "The proportion of significant result is 0.0499"

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

The values are very close to .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() 
  {
  # specify how to get data
  data <- rnorm(n = initial_n, mean = 0, sd = 1)
  
  # perform initial t-test
  test_result <- t.test(data, mu = 0)
  p_value <- test_result$p.value
  
  # if significant, stop
  if (p_value < lower_bound) {
    return(1)}
  
  # if p < .25 but non-significant, collect more data
  else if (p_value <= upper_bound) {
    additional_data <- rnorm(n = additional_n, mean = 0, sd = 1)
    data <- c(data, additional_data)
    test_result <- t.test(data, mu = 0) # retest with the combined data
    p_value <- test_result$p.value
    if (p_value < lower_bound) {
    return(1) } 
    else { 
      return(0) }
  } 
  else {
    return(0)
  }
}

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

# parameters
initial_n <- 30
additional_n <- 30
lower_bound <- 0.05
upper_bound <- 0.25
n_tests <- 10000

# simulation
significant_result <- replicate(n_tests, double.sample()) %>% 
  invisible() %>% 
  unlist() %>% 
  sum() 

# find proportion of signfiicant results
print(
  paste0(
    paste0(
      paste0("The proportion of significant result is ", significant_result / 10000), 
    ". There is ", (significant_result / (.05*10000) )), " times higher than the alpha of .05."))
[1] "The proportion of significant result is 0.0654. There is 1.308 times higher than the alpha of .05."

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

Answer printed above! The false positive rate is about 1.4 times higher than the alpha level.

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.

# global parameters
  initial_n <- 30
  additional_n <- 30
  lower_bound <- 0.05
  n_tests <- 10000
  
# scenario 1 -  double sample when .05 < p < .50
  
  # specific parameter
  upper_bound <- 0.50
  
  # simulation
  significant_result <- replicate(n_tests, double.sample()) %>% 
    invisible() %>% 
    unlist() %>% 
    sum() 
  
  # find proportion of signfiicant results
  print(
    paste0(
      paste0(
        paste0("Scenario 1: The proportion of significant result is ", significant_result / 10000), 
      ". There is ", (significant_result / (.05*10000) )), " times higher than the alpha of .05."))
[1] "Scenario 1: The proportion of significant result is 0.0809. There is 1.618 times higher than the alpha of .05."
# scenario 2 -  double sample when .05 < p < .75
  
  # specific parameter
  upper_bound <- 0.75
  
  # simulation
  significant_result <- replicate(n_tests, double.sample()) %>% 
    invisible() %>% 
    unlist() %>% 
    sum() 
  
  # find proportion of signfiicant results
  print(
    paste0(
      paste0(
        paste0("Scenario 2: The proportion of significant result is ", significant_result / 10000), 
      ". There is ", (significant_result / (.05*10000) )), " times higher than the alpha of .05."))
[1] "Scenario 2: The proportion of significant result is 0.0823. There is 1.646 times higher than the alpha of .05."
  # scenario 3 -  double sample when .05 < p < 1.00
  
  # specific parameter
  upper_bound <- 1.00
  
  # simulation
  significant_result <- replicate(n_tests, double.sample()) %>% 
    invisible() %>% 
    unlist() %>% 
    sum() 
  
  # find proportion of signfiicant results
  print(
    paste0(
      paste0(
        paste0("Scenario 3: The proportion of significant result is ", significant_result / 10000), 
      ". There is ", (significant_result / (.05*10000) )), " times higher than the alpha of .05."))
[1] "Scenario 3: The proportion of significant result is 0.0808. There is 1.616 times higher than the alpha of .05."

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

Data-dependent policies like this inflate the false positive findings. The proportion of significant results you get increases as you decrease your treshold to double the sample and re-run statistical tests to find a significant result.