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).
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")
library(tidyverse)
## ── Attaching packages ────────
## ✔ ggplot2 3.2.1 ✔ purrr 0.3.3
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 1.0.0 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ─────────────────
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(hrbrthemes)
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
## Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
## if Arial Narrow is not on your system, please see http://bit.ly/arialnarrow
library(ggthemes)
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.
a <- fvs %>%
filter(condition == "Faces_Medium") #isolate one of the two repeated measures
p <- ggplot(a, aes(x=age)) + #plot
geom_histogram( binwidth=1, fill="#69b3a2", color="#e9ecef", alpha=0.9) +
ggtitle("Participant Ages") +
theme_ipsum() + theme(plot.title = element_text(size=15)) +
scale_x_continuous(limits=c(0,30),breaks=seq(0,30,5))
p
## Warning: Removed 2 rows containing missing values (geom_bar).
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.
p1 <- ggplot(fvs, aes(x=age,y=hand.look,color=condition)) +
geom_point(alpha=0.6) + xlab("Age") + ylab("Hand Looking") +
geom_smooth(method="lm", se=TRUE) +
ggtitle("Participant Ages") +
theme_gdocs() + theme(plot.title = element_text(size=15)) +
scale_x_continuous(limits=c(0,30),breaks=seq(0,30,5))
p1
What do you conclude from this pattern of data?
Infant visual tracking appears to be positively associated with age for both movie conditions. There is more hand-looking overall as an infant ages. There also is more hand-looking in the complex stimuli (with colorful backgrounds and adults) compared to the basic one (with kids playing in front of a white background).
What statistical analyses would you perform here to quantify these differences?
One can use a two-sample test to compare the differences between hand-looking behavior while looking at the basic or complex movie stimuli.
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.
pValues <- 0
for (i in 1:1000){
randDist = rnorm(30, mean=0, sd=1)
t_result = t.test(randDist)
pValues[i] <- t_result$p.value
}
significant_proportion = sum(pValues<.05)/length(pValues)
significant_proportion
## [1] 0.055
Next, do this using the replicate function:
sim_pvalue <- replicate(1000, t.test(rnorm(30, mean=0, sd=1))$p.value)
sim_pvalue_proportion = sum(sim_pvalue<.05)/length(sim_pvalue)
sim_pvalue_proportion
## [1] 0.049
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
The simulations show a similar p = .05 false positive rate.
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 () {
firstSet = rnorm(30, mean=0, sd=1)
secondSet = rnorm(30, mean=0, sd=1)
firstAttempt = t.test(firstSet)$p.value
if (between(firstAttempt, .05, .25)) {
doubleList = c(firstSet,secondSet)
result = t.test(doubleList)$p.value
}
else if ( firstAttempt < .05 ) {
result = firstAttempt
}
else if ( firstAttempt > .25) {
result = firstAttempt
}
return(result)
}
Now call this function 10k times and find out what happens.
tenk_sim <- replicate(10000, double.sample());
sniffing_pvalue_proportion = sum(tenk_sim<.05)/length(tenk_sim)
sniffing_pvalue_proportion
## [1] 0.0742
Is there an inflation of false positives? How bad is it?
There is a slight inflation (about 0.1 increase) in the false positive rate.
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:
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.
sampleN=1000
double.sample1 <- function (sampleN) {
firstSet = rnorm(sampleN, mean=0, sd=1)
secondSet = rnorm(sampleN, mean=0, sd=1)
firstAttempt = t.test(firstSet)$p.value
if ( firstAttempt > .05 ) {
doubleList = c(firstSet,secondSet)
result = t.test(doubleList)$p.value
}
else if ( firstAttempt < .5) {
result = firstAttempt
}
return(result)
}
double.sample2 <- function (sampleN) {
firstSet = rnorm(sampleN, mean=0, sd=1)
secondSet = rnorm(sampleN, mean=0, sd=1)
firstAttempt = t.test(firstSet)$p.value
if ( firstAttempt > .05 ) {
doubleList = c(firstSet,secondSet)
result = t.test(doubleList)$p.value
}
else if ( firstAttempt < .75) {
result = firstAttempt
}
return(result)
}
double.sample3 <- function (sampleN) {
firstSet = rnorm(sampleN, mean=0, sd=1)
secondSet = rnorm(sampleN, mean=0, sd=1)
firstAttempt = t.test(firstSet)$p.value
if ( firstAttempt > .05 ) {
doubleList = c(firstSet,secondSet)
result = t.test(doubleList)$p.value
}
else if ( firstAttempt < .05) {
result = firstAttempt
#sampleN = sampleN * 2
}
return(result)
}
tenk_sim1 <- replicate(10000, double.sample1(sampleN));
tenk_pvalue_proportion1 = sum(tenk_sim1<.05)/length(tenk_sim1)
tenk_pvalue_proportion1
## [1] 0.0853
tenk_sim2 <- replicate(10000, double.sample2(sampleN));
tenk_pvalue_proportion2 = sum(tenk_sim2<.05)/length(tenk_sim2)
tenk_pvalue_proportion2
## [1] 0.0856
tenk_sim3 <- replicate(10000, double.sample3(sampleN));
tenk_pvalue_proportion3 = sum(tenk_sim3<.05)/length(tenk_sim3)
tenk_pvalue_proportion3
## [1] 0.0823
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
There is a false positive rate inflation as the p-value threshold rises from 0.5 to 0.75 (to roughly 0.08). Doubling the sample size also increased the rate to about 0.08.