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).
library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ 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 ──────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
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.
#get histogram for only one condition since it's repeated measures
fvsFacesMedium <- fvs %>%
mutate(roundAge = factor(round(age))) %>% #round the ages
filter(condition == "Faces_Plus")
ggplot(fvsFacesMedium, aes(x=roundAge))+
geom_histogram(stat="count")+
xlab("Age (months)")
## Warning: Ignoring unknown parameters: binwidth, bins, pad
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(fvs,aes(x = age, y = hand.look, color = condition))+
geom_point()+
ylab("Proportion Looking at Hands ")+xlab("Age (months)")+
geom_smooth(method = 'lm', se = F)+
scale_color_brewer(palette = "Set1")
What do you conclude from this pattern of data?
Based on this pattern of data, it looks like as infants get older (especially >12 months), they spend more time looking at hands while watching more complex videos that feature both children and adults.
What statistical analyses would you perform here to quantify these differences?
To quantify these differences, I would perform a two-way ANOVA using condition and age as independent variables. Based on the scatter plot and trend lines, I would predict to see an interaction effect between age and condition on proportion looking at hands.
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.
count = 0 #running count of significant results
for (i in 1:10000){
norm=rnorm(30)
tTest=t.test(norm)
pVal=tTest$p.value #get p value from tTest
if(pVal< .05) #if the pValue is significant, add one to the count
count <- count+1
}
count/10000 #get proportion of significant results
## [1] 0.0545
Next, do this using the replicate function:
proportion<-replicate(10000, as.numeric(t.test(rnorm(30))$p.value<.05), simplify = F) #generate list as output
sum(unlist(proportion))/10000 #sum, and find proportion
## [1] 0.0507
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
The 10,000 for-loop and ‘replicate’ generated replications have false-positive rates of between a range of 0.048 and 0.051, which is very close to the intended 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 (originalSample,upperBound) { #allows for altering the sample size
tTest=t.test(originalSample)
pVal=tTest$p.value #get p value from tTest
if(pVal>.05||pVal<upperBound){
new<-append(originalSample,rnorm(30))
newpVal<-t.test(new)$p.value
return(newpVal)
}
else{
return(pVal)
}
}
Now call this function 10k times and find out what happens.
proportion<-replicate(10000,as.numeric(double.sample(rnorm(30),.25)<.05),simplify = F)
sum(unlist(proportion))/10000 #sum list and find proportion
## [1] 0.0518
Is there an inflation of false positives? How bad is it?
There does seem to be a slight inflation of the false positives. Generally, adding 30 participants to the sample when the p value is between 0.05 and 0.25 inflates the false positive rate to a range of 0.049 to 0.053
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.
# #max p value = 0.5
proportion1<-replicate(10000,as.numeric(double.sample(rnorm(30),.5)<.05),simplify = F)
sum(unlist(proportion1))/10000 #sum list and find proportion
## [1] 0.0522
#
# #max p value = 0.75
proportion2<-replicate(10000,as.numeric(double.sample(rnorm(30),.75)<.05),simplify = F)
sum(unlist(proportion2))/10000 #sum list and find proportion
## [1] 0.0473
# #max p value = 1 no upper bound for pValue
proportion3<-replicate(10000,as.numeric(double.sample(rnorm(30),1)<.05),simplify = F)
sum(unlist(proportion3))/10000 #sum list and find proportion
## [1] 0.0506
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
It seems there is a certain point where p is so insignificant that adding more data will not bring the data any closer to significance. When the upper bound for the p-value is 0.25, the false-positive rate is inflated by quite a bit more than when the upper bound is 0.5, 0.75, or undefined (1). This suggests that this policy can have an impact on the false-postive rate when a result is on the cusp of significance but there is a point when the relationship is so insignificant that adding additional data will not affect the false positive rate.