library(qualtRics)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.5 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.4 ✓ stringr 1.4.0
## ✓ readr 2.0.2 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(knitr)
library(psych)
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
library(graphics)
library(ggplot2)
library(foreign)
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.
describe(fvs)
## vars n mean sd median trimmed mad min max range skew
## subid 1 232 59.79 34.18 59.50 59.74 43.74 1.00 119.00 118.00 0.01
## age 2 232 12.37 4.34 11.87 12.07 4.24 3.16 27.85 24.69 0.75
## condition* 3 232 1.50 0.50 1.50 1.50 0.74 1.00 2.00 1.00 0.00
## hand.look 4 232 0.07 0.06 0.06 0.06 0.06 0.00 0.35 0.35 1.37
## kurtosis se
## subid -1.20 2.24
## age 0.70 0.29
## condition* -2.01 0.03
## hand.look 2.82 0.00
age_df <- fvs %>%
group_by(subid) %>%
dplyr::summarise(age = mean(age))
ggplot(data = age_df, aes(x = age)) +
geom_histogram(aes(y = ..density..)) +
labs(x = "Age (in Months)", y = "Density") +
scale_y_continuous(limits = c(0, 0.14)) +
scale_x_continuous(breaks = 3:28) +
ggthemes::theme_few()
## `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(data = fvs, aes(x = age, y = hand.look, color = condition)) +
geom_point(alpha = .5) +
geom_smooth(method = "lm") +
scale_x_continuous(breaks = 3:28) +
scale_y_continuous(limits = c(0, 0.4)) +
labs(x = "Age (in Months)", y = "Hand Looking") +
ggtitle("Simple Linear Model") +
ggthemes::theme_few() +
theme(legend.position = "top")
## `geom_smooth()` using formula 'y ~ x'
ggplot(data = fvs, aes(x = age, y = hand.look, color = condition)) +
geom_point(alpha = .5) +
geom_smooth(method = "lm", formula = y ~ poly(x, 2), se = T) +
scale_x_continuous(breaks = 3:28) +
scale_y_continuous(limits = c(0, 0.4)) +
labs(x = "Age (in Months)", y = "Hand Looking") +
ggtitle("Linear Model with the Second Polynomial") +
ggthemes::theme_few() +
theme(legend.position = "top")
What do you conclude from this pattern of data?
In general, there is positive assciation between age (in months) and hand looking. The slopes of the regression lines are different for each condition. For “Faces_Plus” condition, the positive association between age (in months) and hand looking is stronger than that for “Faces_Medium” condition.
What statistical analyses would you perform here to quantify these differences?
I would use a linear model with a moderator variable, with moderator being the condition (faces_medium or faces_plus.) I would consider adding in the second polynomial of age to the model as well.
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_sim <- 10000
N <- 30
vec_pvalue <- c()
set.seed(1)
for (i in 1:n_sim){
one_sample <- rnorm(N, mean = 0, sd = 1)
sim_test <- t.test(one_sample, mu = 0, alternative = "two.sided")
vec_pvalue[i] <- sim_test$p.value
}
tab_t1 <- table(vec_pvalue < 0.05)
tab_t1
##
## FALSE TRUE
## 9498 502
print(paste0("The proportion of significant results is ", (tab_t1[2] / (tab_t1[1] + tab_t1[2]))))
## [1] "The proportion of significant results is 0.0502"
Next, do this using the replicate
function:
set.seed(1)
simulated_t2 <- replicate(n_sim, t.test(rnorm(N, mean = 0, sd = 1), mu = 0, alternative = "two.sided"), simplify = F)
tab_t2 <- table(sapply(simulated_t2, "[[", "p.value") < 0.05)
tab_t2
##
## FALSE TRUE
## 9498 502
print(paste0("The proportion of significant results is ", (tab_t2[2] / (tab_t2[1] + tab_t2[2]))))
## [1] "The proportion of significant results is 0.0502"
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
The proportion of “significant” results is 0.0502 for both methods (I set the seed to 1). The acquired value is very similar to 0.05, although it is slightly larger than 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 (N, upper, lower) {
sample_vec <- c()
one_sample <- rnorm(N, mean = 0, sd = 1)
sample_vec <- c(sample_vec, one_sample)
result <- t.test(sample_vec, mu = 0, alternative = "two.sided")
test_value <- result$p.value
while (test_value < upper & test_value > lower){
one_sample <- rnorm(N, mean = 0, sd = 1)
sample_vec <- c(sample_vec, one_sample)
result <- t.test(sample_vec, mu = 0, alternative = "two.sided")
test_value <- result$p.value
}
return(test_value)
}
Now call this function 10k times and find out what happens.
n_sim <- 10000
N <- 30
upper <- .25
lower <- .05
pval_vec <- c()
set.seed(1)
for (i in 1:n_sim){
pval_vec[i] <- double.sample(30, .25, .05)
}
tab_t3 <- table(pval_vec < 0.05)
tab_t3
##
## FALSE TRUE
## 9143 857
print(paste0("The proportion of significant results is ", (tab_t3[2] / (tab_t3[1] + tab_t3[2]))))
## [1] "The proportion of significant results is 0.0857"
Is there an inflation of false positives? How bad is it?
There seems to be an inflation of false positives. The proportion of significant results should be around 0.05, but I got 0.0857 from the simulation above. I would say it is bad because the proportion of “significant” results almost doubled as a result of the simulation above.
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:
n_sim <- 10000
N <- 30
pval_vec2 <- c()
set.seed(1)
for (i in 1:n_sim){
pval_vec2[i] <- double.sample(30, .5, .05)
}
tab_t4 <- table(pval_vec2 < 0.05)
tab_t4
##
## FALSE TRUE
## 8736 1264
print(paste0("The proportion of significant results is ", (tab_t4[2] / (tab_t4[1] + tab_t4[2]))))
## [1] "The proportion of significant results is 0.1264"
n_sim <- 10000
N <- 30
pval_vec3 <- c()
set.seed(1)
for (i in 1:n_sim){
pval_vec3[i] <- double.sample(30, .75, .05)
}
tab_t5 <- table(pval_vec3 < 0.05)
tab_t5
##
## FALSE TRUE
## 8207 1793
print(paste0("The proportion of significant results is ", (tab_t5[2] / (tab_t5[1] + tab_t5[2]))))
## [1] "The proportion of significant results is 0.1793"
#n_sim <- 10000
#N <- 30
#pval_vec4 <- c()
#set.seed(1)
#for (i in 1:n_sim){
# pval_vec4[i] <- double.sample(30, 1, .05)
#}
#tab_t6 <- table(pval_vec4 < 0.05)
#tab_t6
#print(paste0("The proportion of significant results is ", (tab_t6[2] / (tab_t6[1] + tab_t6[2]))))
For this, I do not even need to run a simulation. If the researcher doubles the sample whenever they get ANY p-value that is not significant, they are going to end up with significant value every time. The codes aboves run the simulation for the situation but I am not including the results here because it was taking me too long to run the whole simulation. In this case, the proportion of significant results would be 1.00 (100%).
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.
# CHECK THE ABOVE FOR THE CODES FOR EACH SCENARIO.
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
As I ran simulations for more “optimistic” scenarios, the false positive rate went up. (The proportion of “significant” results went up substantially, which is bad because it is supposed to stay at around the value of alpha, which is 0.05 in this case.) This kind of data-dependent policy is really bad because it means we are going to have many more “false positive” results. With this kind of data-dependent policy and publication bias, the precision of science is doomed. So we need to be careful not to implement such data-dependent policy.