fvs <- read.csv("/Users/ciddecatur/Desktop/problem_sets/data/FVS2011-hands.csv")Psych 251 PS4: Simulation + Analysis
Note: I did receive assistance from generative AI after attempting each problem by myself. Specifically, AI was used for syntax. Interpretation and application takeaways/free responses were my work.
This was primarily in the development of the for loop and afterwards in pset. GGplots were largely constructed using personal knowledge, google, and experience from class. This is the first statistical simulation I’ve attempted. Have never seen replicate function, nor
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).
Part 1: ggplot practice
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(ggplot2)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.
#First remove repeated measures
unique_data <- fvs
unique_data %>%
group_by(subid) %>%
slice(1)# A tibble: 119 × 4
# Groups: subid [119]
subid age condition hand.look
<int> <dbl> <chr> <dbl>
1 1 12.0 Faces_Medium 0.0530
2 2 3.16 Faces_Medium 0.0319
3 3 9.53 Faces_Medium 0.0352
4 4 15.4 Faces_Medium 0.136
5 5 9.86 Faces_Plus 0
6 6 9.40 Faces_Medium 0.0698
7 7 13.6 Faces_Medium 0.0384
8 8 13.3 Faces_Medium 0.0111
9 9 12.5 Faces_Medium 0.0830
10 10 14.5 Faces_Medium 0.136
# ℹ 109 more rows
#Make plot
ggplot(unique_data, aes(x = age)) +
geom_histogram(binwidth = 1, fill = "blue", color = "black") +
theme_minimal() +
labs(title = "Histogram of Participant Age", x = "Age", y= "number of participants")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=age, y=hand.look, color = condition)) +
labs(title="Hand Looking as a Function of Age and Condition",
x = "Age (years)", y = "Hand looking (seconds)") +
geom_point() +
geom_smooth(method = "lm", se = FALSE)`geom_smooth()` using formula = 'y ~ x'
What do you conclude from this pattern of data?
It looks like the data is tightly clustered with a nonzero portion of the Faces_Plus data being higher in looking time; mostly in higher ages. It’s very possible that a combination of age and placement in the Faces_Plus condition lead to higher looking time.
What statistical analyses would you perform here to quantify these differences?
Since this is a within-subject design and data look “football shaped” enough, I believe a linear model using age and condition as covariates would be appropriate. Could also use paired Samples T-test if you don’t want to include multiple covariates.
Part 2: Simulation
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.
# Vector to store p-values
p_values <- numeric(10000)
# Loop to run the t-tests and store p-values
for(i in 1:10000) {
experiment <- rnorm(30, mean = 0, sd = 1)
t_test <- t.test(experiment, mu = 0)
p_values[i] <- t_test$p.value
}
# Calculate the proportion of significant results
proportion_significant <- mean(p_values < 0.05)
proportion_significant[1] 0.051
Next, do this using the replicate function:
p_values <- replicate(10000, {
experiment <- rnorm(30, mean = 0, sd = 1)
t_test <- t.test(experiment, mu = 0)
return(t_test$p.value)
})
mean(p_values <0.05)[1] 0.0521
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
these are both very close to the false positive rate of .05: one above and one below due to randomness in the sampling distrobution. Could increase replication numbers to get closer to exacly 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.
Note: this is not truly my code, I’m interested to see how this works.
double.sample <- function() {
initial_sample_size <- 30
# Sample and conduct the first t-test
initial_sample <- rnorm(initial_sample_size, mean = 0, sd = 1)
initial_test <- t.test(initial_sample, mu = 0)
# Decision criteria based on the p-value
if (initial_test$p.value < 0.05) {
# Significant result, stop and report
return(initial_test$p.value)
} else if (initial_test$p.value > 0.25) {
# Not even close to significant, stop and report
return(initial_test$p.value)
} else {
# P-value is in the range [0.05, 0.25], collect more data
additional_sample <- rnorm(initial_sample_size, mean = 0, sd = 1)
combined_sample <- c(initial_sample, additional_sample)
combined_test <- t.test(combined_sample, mu = 0)
return(combined_test$p.value)
}
}
double.sample()[1] 0.6845195
Now call this function 10k times and find out what happens.
p_values <- replicate(10000, double.sample())
mean(p_values <0.05)[1] 0.0693
Is there an inflation of false positives? How bad is it?
This reports a rejection of the null hypothesis >7% of the time over 10k trials of this p-sniffing methods, which is an additional 50% false positive rate… it’s definitely not excellent.
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:
- The researcher doubles the sample whenever their pvalue is not significant, but it’s less than 0.5.
- The researcher doubles the sample whenever their pvalue is not significant, but it’s less than 0.75.
- The research doubles their sample whenever they get ANY pvalue that is not significant.
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.
again, AI-assisted code, I wanted to get to the punchline of this exercise
double.sample <- function(upper_threshold) {
initial_sample_size <- 30
# Sample and conduct the first t-test
initial_sample <- rnorm(initial_sample_size, mean = 0, sd = 1)
initial_test <- t.test(initial_sample, mu = 0)
# Decision criteria based on the p-value
if (initial_test$p.value < 0.05) {
# Significant result, stop and report
return(initial_test$p.value)
} else if (initial_test$p.value > upper_threshold) {
# Not significant and not close enough, stop and report
return(initial_test$p.value)
} else {
# P-value is not significant but within the threshold, collect more data
additional_sample <- rnorm(initial_sample_size, mean = 0, sd = 1)
combined_sample <- c(initial_sample, additional_sample)
combined_test <- t.test(combined_sample, mu = 0)
return(combined_test$p.value)
}
}
#double experiment number
num_experiments <- 20000
#running less than .5
results0_5 <- replicate(num_experiments, double.sample(0.5))
mean(results0_5 <0.05)[1] 0.07865
#running less than .75
results_0_75 <- replicate(num_experiments, double.sample(0.75))
mean(results_0_75 < 0.05)[1] 0.08485
#running any p-val
results_any <- replicate(num_experiments, double.sample(1))
mean(results_any < 0.05)[1] 0.0814
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
As you keep increasing the upper limitt there are diminishing effects on the false positives. It seems to top out at under 90%, which is not good. This practice can all but guarantee “significant” test results.