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.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.4 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.3 ✓ stringr 1.4.0
## ✓ readr 2.0.1 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(dplyr)
fvs <- read.csv("data/FVS2011-hands.csv")
colnames(fvs)
## [1] "subid" "age" "condition" "hand.look"
head(fvs)
## subid age condition hand.look
## 1 2 3.156164 Faces_Medium 0.03187500
## 2 93 5.030137 Faces_Medium 0.11885333
## 3 29 5.852055 Faces_Medium 0.09212000
## 4 76 5.852055 Faces_Medium 0.12961111
## 5 48 6.082192 Faces_Medium 0.01378571
## 6 101 6.147945 Faces_Medium 0.04384706
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.
fvs %>%
group_by(subid, age) %>%
summarise()
## `summarise()` has grouped output by 'subid'. You can override using the `.groups` argument.
## # A tibble: 119 × 2
## # Groups: subid [119]
## subid age
## <int> <dbl>
## 1 1 12.0
## 2 2 3.16
## 3 3 9.53
## 4 4 15.4
## 5 5 9.86
## 6 6 9.40
## 7 7 13.6
## 8 8 13.3
## 9 9 12.5
## 10 10 14.5
## # … with 109 more rows
ggplot(fvs, aes(x=fvs$age)) + geom_histogram()
## Warning: Use of `fvs$age` is discouraged. Use `age` instead.
## `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.
ggplot(fvs, aes(x=fvs$age, y=fvs$hand.look)) +
geom_point(shape = ".") +
geom_smooth() +
theme_classic() +
ylab("Looking at hand") +
xlab("Age in months")
## Warning: Use of `fvs$age` is discouraged. Use `age` instead.
## Warning: Use of `fvs$hand.look` is discouraged. Use `hand.look` instead.
## Warning: Use of `fvs$age` is discouraged. Use `age` instead.
## Warning: Use of `fvs$hand.look` is discouraged. Use `hand.look` instead.
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
What do you conclude from this pattern of data?
Infants start paying more attention to hands around 15 months
What statistical analyses would you perform here to quantify these differences?
A regression?
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.
result <- numeric(10000)
for (i in 1:10000) {
X <- rnorm(30, 0, 1)
result[i] <- t.test(X)$p.value
}
sig_results <- result < 0.05
sum(sig_results)/10000
## [1] 0.046
Next, do this using the replicate function:
pvals <- replicate(10000, t.test(rnorm(30))$p.value)
sig_pvals <- pvals < 0.05
sum(sig_pvals)/10000
## [1] 0.0537
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
The output here shows a false positive rate of ALMOST 5 percent (slightly less than 5 percent), which is expected given we are testing for \(p < .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 () {
first_sample <- rnorm(30)
pvals_2 <- (t.test(first_sample)$p.value)
if(pvals_2<0.05) {
return(pvals_2)
} else if (pvals_2>0.25) {
return(pvals_2)
} else {
second_sample <- c(first_sample, rnorm(30))
return(t.test(second_sample)$p.value)
}
}
Now call this function 10k times and find out what happens.
tenk_results <- replicate(10000, double.sample())
sum(tenk_results < 0.05)/10000
## [1] 0.0742
Is there an inflation of false positives? How bad is it?
Yes, now we have about a 7/8 percent false positives. The double sampling technique gives us a slightly higher false positives.
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.
# The researcher doubles the sample whenever their pvalue is not significant, but it's less than 0.5.
double.sample0.5 <- function () {
first_sample_0.05 <- rnorm(30)
pvals_2_0.05 <- (t.test(first_sample_0.05)$p.value)
if(pvals_2_0.05<0.05) {
return(pvals_2_0.05)
} else if (pvals_2_0.05>0.5) {
return(pvals_2_0.05)
} else {
second_sample_0.05 <- c(first_sample_0.05, rnorm(30))
return(t.test(second_sample_0.05)$p.value)
}
}
tenk_results_0.05 <- replicate(10000, double.sample0.5())
sum(tenk_results_0.05 < 0.05)/10000
## [1] 0.0782
# The researcher doubles the sample whenever their pvalue is not significant, but it's less than 0.75.
double.sample0.75 <- function () {
first_sample_0.75 <- rnorm(30)
pvals_2_0.75 <- (t.test(first_sample_0.75)$p.value)
if(pvals_2_0.75<0.05) {
return(pvals_2_0.75)
} else if (pvals_2_0.75>0.75) {
return(pvals_2_0.75)
} else {
second_sample_0.75 <- c(first_sample_0.75, rnorm(30))
return(t.test(second_sample_0.75)$p.value)
}
}
tenk_results_0.75 <- replicate(10000, double.sample0.75())
sum(tenk_results_0.75 < 0.05)/10000
## [1] 0.0812
# The researcher doubles the sample whenever their pvalue is not significant.
double.sample_whenever <- function () {
first_sample_whenever <- rnorm(30)
pvals_2_whenever <- (t.test(first_sample_whenever)$p.value)
if(pvals_2_whenever<0.05) {
return(pvals_2_whenever)
} else {
second_sample_whenever <- c(first_sample_whenever, rnorm(30))
return(t.test(second_sample_whenever)$p.value)
}
}
tenk_results_whenever <- replicate(10000, double.sample_whenever())
sum(tenk_results_whenever < 0.05)/10000
## [1] 0.0862
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
This kind of data-dependent policy can create increasing numbers of false positives even if p value is set at less than .05.