library(tidyverse)
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")
## 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.
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.
single_condition_fvs = fvs %>%
filter(condition=="Faces_Medium")
ggplot(data=single_condition_fvs, aes(single_condition_fvs$age)) +
geom_histogram() +
ylab("Count") +
xlab("Age (Months)")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
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.
conditioned_fvs = fvs %>%
group_by(condition)
ggplot(conditioned_fvs, aes(x = age, y = hand.look)) +
geom_point() +
geom_smooth(method='lm') +
geom_jitter(height = .1, width = 0) +
facet_wrap(~condition) +
ylab("Looking Times") +
xlab("Age (months)")
## `geom_smooth()` using formula 'y ~ x'
What do you conclude from this pattern of data?
For both conditions, older children spend more time looking at hands as compared to younger children. However, there is a sharper increase for the Face_Plus condition compared to the Face_Medium condition, meaning as children get older there is a greater positive difference (increase) in how much they spend time looking at hands in the Faces Plus condition compared to when they are younger. This positive difference is greater for the Faces Plus condition although both show a positive increase.
What statistical analyses would you perform here to quantify these differences?
We want to determine whether the relationship between age and looking times is significantly different between the two conditions, Faces_Medium and Faces_Plus. By fitting lines we can compute the coefficients of the linear fits in each condition. After obtaining these coefficients, say \(\beta_{1}\) and \(\beta_{2}\), our null hypothesis would be \(\beta_{1} == \beta_{2}\) and the alternative hypopthesis would be that they are not equal. This would then follow a students t-distribution with \(n_1 + n_2 - 4\) degrees of freedom, where \(\beta_{1}\) and \(n_1\) would be the slope coefficient for the Face_Medium condition and the number of samples for that condition, and \(\beta_{2}\) and \(n_2\) would be the slope coefficient for the Face_Plus condition and the number of samples for that condition.
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 <- 30
mu <- 0
sd <- 1
n_ttests = 10000
p_values <- 0
for(i in 1:n_ttests) {
observations <- rnorm(n, mu, sd)
ttest = t.test(x=observations, y = NULL)
p_values[i] <- ttest$p.value
}
proportion = length(p_values[p_values<0.05]) / length(p_values)
proportion
## [1] 0.0491
Next, do this using the replicate function:
p_values2 = replicate(n_ttests,t.test(rnorm(n, mu, sd),y=NULL)$p.value)
proportion2 = length(p_values2[p_values2<0.05]) / length(p_values2)
proportion2
## [1] 0.0477
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
The for loop and the replicate gives values very close to that of $% which means this is a good estimate when computing a t-test 10,000 times.
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 (n=30, mu=0, sd=1) {
observations <- rnorm(n, mu, sd)
ttest <- t.test(x=observations, y = NULL)
p_value <- ttest$p.value
if (p_value > 0.05 && p_value < 0.25) {
more_observations = rnorm(n, mu, sd)
all_observations = c(observations, more_observations)
ttest <- t.test(all_observations, y = NULL)
p_value <- ttest$p.value
}
return(p_value)
}
Now call this function 10k times and find out what happens.
p_values3 = replicate(n_ttests,double.sample(n=30, mu=0, sd=1))
proportion3 = length(p_values3[p_values3<0.05]) / length(p_values3)
print(proportion3)
## [1] 0.07
Is there an inflation of false positives? How bad is it?
Yes, the inflation has grown from approximately 0.05 to 0.071, essentially an increase of about 2.1% percent greater chance of having a p-vaue below 0.05.
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.
n_ttests = 50000
double.sample2 <- function (p_upper, n=30, mu=0, sd=1) {
observations = rnorm(n, mu, sd)
ttest <- t.test(x=observations, y = NULL)
p_value <- ttest$p.value
if (p_value > 0.05 && p_value < p_upper) {
more_observations = rnorm(n, mu, sd)
all_observations = c(observations, more_observations)
ttest <- t.test(x=all_observations, y = NULL)
p_value <- ttest$p.value
}
return(p_value)
}
p_values4 = replicate(n_ttests,double.sample2(p_upper=0.5, n=30, mu=0, sd=1))
proportion4 = length(p_values4[p_values4<0.05]) / length(p_values4)
sprintf("Any p-value that is not significant but less than 0.5, proportion = %f", proportion4)
## [1] "Any p-value that is not significant but less than 0.5, proportion = 0.079280"
p_values5 = replicate(n_ttests,double.sample2(p_upper=0.75, n=30, mu=0, sd=1))
proportion5 = length(p_values5[p_values5<0.05]) / length(p_values5)
sprintf("Any p-value that is not significant but less than 0.75, proportion = %f", proportion5)
## [1] "Any p-value that is not significant but less than 0.75, proportion = 0.082060"
p_values6 = replicate(n_ttests,double.sample2(p_upper=1.0, n=30, mu=0, sd=1))
proportion6 = length(p_values6[p_values6<0.05]) / length(p_values6)
sprintf("Any p-value that is not significant, proportion = %f", proportion6)
## [1] "Any p-value that is not significant, proportion = 0.083600"
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
Well this is obviously bad because what we are showing is that you can hack your p-value by simply getting more data whenever your data results in a test with not low enough of a p-value. When you get more data you increase the probability of your p-value to be less than 0.05 and hence significant. This is demonstrating that this is the case even when your data is sampled from the same distribution. Of course chaning the upper p here jsut demonstrates how liberal we are with gettting more data. The higher the upper p, the more liberal we are, meaning any time we don’t get a low p-value is sufficient excuse to gather more data and hence incrase our chances of lower p-value.