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
ggplot2skills 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
ggplot2practice.
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, andFaces_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")
## Parsed with column specification:
## cols(
## subid = col_double(),
## age = col_double(),
## condition = col_character(),
## hand.look = col_double()
## )
First, use
ggplotto 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_for_agehist <- fvs %>%
group_by(subid) %>%
summarise(age = mean(age))
mean <- mean(fvs_for_agehist$age)
sd <- sd(fvs_for_agehist$age)
n <- length(fvs_for_agehist$subid)
fvs_for_agehist %>%
ggplot(
aes(x = age)) +
geom_histogram(binwidth = 1, bins = 28, colour = "lightblue", fill = "lightblue") +
ylab("Number of participants") +
xlab("Children's Age in Months") +
theme_light() +
stat_function(fun = function(x) dnorm(x, mean = mean, sd = sd) * n * 1,
color = "darkblue", size = 1)
#stat_function(
# fun = dnorm,
# args = with(fvs, c(mean = mean(age), sd = sd(age)))
#)
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, col = condition)) +
geom_point(alpha = 0.4) +
geom_smooth(method = "lm") +
ylab("Amount of Looking at Hands") +
xlab("Children's Age in Months") +
theme_classic()
What do you conclude from this pattern of data?
As they grow older, children seem to look at hands longer. This effect seems to be stronger in the faces_plus condition (with a more complex background) than in the faces_medium condition (with a white background).
What statistical analyses would you perform here to quantify these differences?
Seeing that each child completed both conditions, we could carry out a t-test or, alternatively, a regression model with age and condition as predictors of amount of 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
forloop.
set.seed(12) # fixing randomisation, ensuring you get the exact same results I got
t1 <- vector("list", 10000)
for (i in 1:10000) {
t1[[i]] <- t.test(rnorm(n = 30, mean = 0, sd = 1), mu = 0)
}
(table(sapply(t1, function(x) x[["p.value"]]) < .05))[2]/10000
## TRUE
## 0.0509
Next, do this using the
replicatefunction:
set.seed(12)
sim1 <- replicate(10000, t.test(rnorm(30, mean=0, sd=1), mu=0),
simplify=FALSE)
(table(sapply(sim1, function(x) x[["p.value"]]) < .05))[2]/10000
## TRUE
## 0.0509
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
The proportion of (false) positives is 509/10,000 = .0509 when using a for loop or the replicate function. Thus, both simulation produced results marginally above the intended false positive rate of \(\alpha=0.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.
set.seed(12)
double.sample <- function() {
sample_1 <- rnorm(30) # first sample, n = 30
sample_2 <- c(sample_1, rnorm(30)) # sample upon having added another n = 30
p_1 = t.test(sample_1)$p.value # running first test with n = 30
if (p_1 < .25 & p_1 > .05){
final_p <- t.test(sample_2)$p.value
}
else (final_p <- t.test(sample_1)$p.value)
return(final_p)
}
Now call this function 10k times and find out what happens.
sim2 <- replicate(10000, double.sample())
sim2_sig <- length(sim2[sim2<.05])/length(sim2)
sim2_sig
## [1] 0.0742
Is there an inflation of false positives? How bad is it?
There is quite an inflation; while the for loop and replicate single sample replications yielded significant results in .51 % of cases (i.e roughly at the set alpha level), the double sample paradigm yields a (false) positive rate of about 7.3 %.
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.
set.seed(12)
# with p_max as argument to double_sample
double.sample2 <- function(p_max) {
sample_1 <- rnorm(30) # first sample, n = 30
sample_2 <- c(sample_1, rnorm(30)) # sample upon having added another n = 30
p_1 = t.test(sample_1)$p.value # running first test with n = 30
if (p_1 < p_max & p_1 > .05){
final_p <- t.test(sample_2)$p.value
}
else (final_p <- t.test(sample_1)$p.value)
return(final_p)
}
# scenario 1 -- doubling whenever p < .5 but not not significant
double.sample_scen1 <- function() {
scen1 <- replicate(10000, double.sample2(0.5))
scen1_sig <- length(scen1[scen1 < .05])/length(scen1)
return(scen1_sig)
}
# scenario 2 -- doubling whenever p < .75 but not not significant
double.sample_scen2 <- function() {
scen1 <- replicate(10000, double.sample2(0.75))
scen1_sig <- length(scen1[scen1 < .05])/length(scen1)
return(scen1_sig)
}
# scenario 3 -- doubling p whenever not significant
double.sample_scen3 <- function() {
scen1 <- replicate(10000, double.sample2(1))
scen1_sig <- length(scen1[scen1 < .05])/length(scen1)
return(scen1_sig)
}
Scenario 1:
double.sample_scen1()
## [1] 0.0832
Scenario 2:
double.sample_scen2()
## [1] 0.0864
Scenario 3:
double.sample_scen3()
## [1] 0.088
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
Running the three scenarios in sequence shows that the more desperate a researcher is to find a positive result (manifesting in her willingness to add to the sample regardless of how much ihgher than the significance threshold her p-value is), the higher the false positive rate. Further, it shows that the esact p-value (regardless of whether or not it is significant) suggests something about the quality of the hypothesis. Lastly, and most disconcertingly, this example shows that if one keeps adding to the sample, the (false) positive rate goes up to 8.8 %. This underlines the importance of pre-registration. Thanks, Mike!