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(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(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.5
## ✔ tibble 3.1.8 ✔ stringr 1.4.1
## ✔ tidyr 1.2.1 ✔ forcats 0.5.2
## ✔ readr 2.1.3
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(ggplot2)
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.
ggplot(fvs, aes(x = age)) + geom_histogram(binwidth = 3)
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 = hand.look, y = age, color = condition, group = condition)) +
geom_smooth(method = lm) +
geom_jitter(data = fvs, aes(x = hand.look, y = age, color = condition),
alpha = 0.25, width = 0.1)
## `geom_smooth()` using formula 'y ~ x'
What do you conclude from this pattern of data?
It would seem like the handlooking values are higher for those in the Faces_Plus Condition.
What statistical analyses would you perform here to quantify these differences?
I would run an t test to see if hand looking difference is significant between different age and condition groups.
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
for (x in 1:1000) {
r <- t.test(rnorm(n = 30, mean = 0, sd = 1), mu = 0)
#print(r$p.value)
if (r$p.value <= 0.05) {
count <- count + 1
}
}
print(count)
## [1] 57
We have around 55/1000 (0.055, 5.5%) for proportion of significant results.
Next, do this using the replicate function:
r = replicate(1000, t.test(rnorm(n = 30, mean = 0, sd = 1), mu = 0))
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
The values we get are higher than the intended false-positive rate of 0.05 (I ran it a couple of times and am getting mostly 0.051-0.058 range), however that being said it also depends on the trial because there are a fair amount of times where I got below 0.05.So overall, I wouls say it is around 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.
double.sample <- function () {
count <- 0
z <- TRUE
t_list <- c()
while (z) {
count <- count + 1
for (a in 1:30) {
t = rnorm(n = 30, mean = 0, sd = 1)
t_list <- append(t_list, mean(t))
}
r = t.test(t_list, mu = 0)
if (r$p.value <= 0.05 | r$p.value >= 0.25) {
z <- FALSE
#print(count)
#print(r$p.value)
return(r$p.value)
#return(count)
}
}
}
Now call this function 10k times and find out what happens.
r = replicate(10000, double.sample())
Is there an inflation of false positives? How bad is it?
We can see that at lot of the times the program is done after the first time because p value is > 0.25, however if the p value is small enough, by adding more trials, we are able to get the p value to below 0.05 with a few more trials. There is definitely a decent amount of false positives in the mix.
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 <- function () {
count <- 0
z <- TRUE
t_list <- c()
while (z) {
count <- count + 1
for (a in 1:30) {
t = rnorm(n = 30, mean = 0, sd = 1)
t_list <- append(t_list, mean(t))
}
r = t.test(t_list, mu = 0)
if (r$p.value <= 0.05 | r$p.value >= 0.50) {
z <- FALSE
#print(count)
#print(r$p.value)
return(r$p.value)
#return(count)
}
}
}
r = replicate(10000, double.sample())
double.sample <- function () {
count <- 0
z <- TRUE
t_list <- c()
while (z) {
count <- count + 1
for (a in 1:30) {
t = rnorm(n = 30, mean = 0, sd = 1)
t_list <- append(t_list, mean(t))
}
r = t.test(t_list, mu = 0)
if (r$p.value <= 0.05 | r$p.value >= 0.75) {
z <- FALSE
#print(count)
#print(r$p.value)
return(r$p.value)
#return(count)
}
}
}
r = replicate(10000, double.sample())
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
From the two new simulations, we can see that as the p value gets farther from 0.05, and we repeat trials, it is not as effective. Even for the ones we manage to get under 0.05 sometimes takes 18 trials etc. and even then sometimes it was unsuccessful and often ended with a p value of above 0.5/0.75 respectively. Based on this we can see that if the p value is close to 0.05 and we add more trials, there’s a good chance of making insignificant data significant, however as the p value veers away towards the higher end, this becomes harder. This kind of data-dependent policy is bad because if someone gets insignificant results but is close to the threshold (ex. 0.05), then they could quite easily turn that into a false positive by adding more trials.