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")
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.
#load in packages
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
##get each subjects age by grouping by subject id
Ages<-fvs %>%
group_by(subid) %>%
dplyr::summarize(age=mean(age))
ggplot(Ages)+
geom_histogram(aes(x=age),binwidth = 1,fill='blue')+
labs(title="Age Distribution",
x ="Age in Months", y = "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.
ggplot(fvs)+
geom_point(aes(x=age,y=hand.look,col=condition))+
geom_smooth(aes(x=age,y=hand.look,col=condition),span = .85)+
labs(title="Hand looking by Condition and Age",
x ="Age", y = "Hand Looking",color='Condition')+
scale_color_manual(labels = c("White Background (Faces Medium)", "Complex Movie (Faces Plus)"), values = c("blue", "red"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
What do you conclude from this pattern of data?
From this pattern of data we see that as age increases that the measure of hand looking increases, and it seems like it may be moderated by the condition. That is in the more comples movie condition (Faces Plus) kids that are older (about 20 months) appear to look at hands more.
What statistical analyses would you perform here to quantify these differences?
I would perform a regression or anova with an interaction between age and condition to determine and quanitify a difference to see if older kids in the more complex (faces plus) move condition look at hands more.
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.
#get simulation data
#initialize empty vector of p values
Pvals <- c() # an empty vector to store all estimated ATEs across randomizations
for (i in 1:10000){
sample <- rnorm(30,0,1)
sampleTest <- t.test(sample)
Pvals[i] <- sampleTest$p.value
}
#this is how many p values are less than 0.05
mean(Pvals<0.05)
## [1] 0.0534
Next, do this using the replicate function:
#get the simulations
sims<-replicate(10000,rnorm(n=30,mean=0,sd=1))
#define a function to extract p values from the t test
tTestPValue <- function(x) t.test(x)$p.value
#apply function to all rows
tTestResults <- apply(sims, 2,tTestPValue )
#get proportion of results with a p value less than 0.05
mean(tTestResults<0.05)
## [1] 0.0462
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
These results are roughly the same as the 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 () {
#initial sample
currentSample <-rnorm(n=30,mean=0,sd=1)
pval<-t.test(currentSample,alternative = "two.sided")$p.value
#end if pvalue greater than 0.25 or less thand .05
if(pval<0.05 || pval>.25){
return(currentSample)
}
else{
currentSample <-c(currentSample,rnorm(n=30,mean=0,sd=1))
return(currentSample)
}
}
Now call this function 10k times and find out what happens.
##grab a sample
MySampling<-replicate(10000,double.sample())
#convert to dataframe
MySamplingDF<-as.data.frame(MySampling)
#run t tests
tTestResults <- apply(MySamplingDF, 2,tTestPValue )
# extract fraction significant
mean(tTestResults<0.05)
## [1] 0.0711
Is there an inflation of false positives? How bad is it?
Yes there is an inflation of false positives. It is somewhat bad, it is nearly 0.07 that are now significant.
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.
double.sample.modified <- function (upper_p_value) {
#initial sample
currentSample <-rnorm(n=30,mean=0,sd=1)
pval<-t.test(currentSample,alternative = "two.sided")$p.value
#end if pvalue greater than 0.25 or less thand .05
if(pval<0.05 || pval>upper_p_value){
return(currentSample)
}
else{
currentSample <-c(currentSample,rnorm(n=30,mean=0,sd=1))
pval<-t.test(currentSample,alternative = "two.sided")$p.value
return(currentSample)
}
}
###with p=0.5
##grab a sample
MySampling.5<-replicate(10000,double.sample.modified(.5))
#convert to dataframe
MySamplingDF.5<-as.data.frame(MySampling.5)
#run t tests
tTestResults.5 <- apply(MySamplingDF.5, 2,tTestPValue )
# extract fraction significant
mean(tTestResults.5<0.05)
## [1] 0.0776
###with p=0.75
##grab a sample
MySampling.75<-replicate(10000,double.sample.modified(.75))
#convert to dataframe
MySamplingDF.75<-as.data.frame(MySampling.75)
#run t tests
tTestResults.75 <- apply(MySamplingDF.75, 2,tTestPValue )
# extract fraction significant
mean(tTestResults.75<0.05)
## [1] 0.0841
###with p=1
##grab a sample
MySampling.1<-replicate(10000,double.sample.modified(1))
#convert to dataframe
MySamplingDF.1<-as.data.frame(MySampling.1)
#run t tests
tTestResults.1 <- apply(MySamplingDF.1, 2,tTestPValue )
# extract fraction significant
mean(tTestResults.1<0.05)
## [1] 0.0802
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
This data dependent policy seems fairly bad in that we inflate the type 1 error rate above the predetermined alpha. We are now much more likely to reject a null hypothesis than we would normally.