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).
#import appropriate packages first library(readr)library(ggplot2)#read in datafilefvs <-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.
ages <-ggplot(fvs, aes(x=age)) +#added white line separators to show clear differences between binsgeom_histogram(color ="white", binwidth =1) +labs(x ='Age (Years)', y ='Count')ages
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.
#used the loess method because lmmethod seems to be underfitting the datapointshand_looking <-ggplot(fvs, aes(x = age, y = hand.look, color = condition)) +geom_point() +geom_smooth(method ="loess") +# clean up plot by adding labels and bolding title labs(title ='Hand Looking as a Function of Age and Condition', x ='Age (Years)', y ='Hand Looking Time (ms)', color ="Condition") +theme(plot.title =element_text(face="bold"))hand_looking
`geom_smooth()` using formula = 'y ~ x'
What do you conclude from this pattern of data?
From this pattern of data, I see that from Ages 0 - 13, hand-looking time seems to be quite consistent for both Faces_Medium and Faces_Plus conditions (~0.05). Then, around 13+ years, both conditions show an increase in hand looking time. We, however, see a bigger increase in the Faces_Plus condition.
What statistical analyses would you perform here to quantify these differences?
Since we want to see the differences between two different groups (conditions), I would suggest that we can perform a t-test to quantify the differences in hand-looking.
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.
count <-0total <-10000for (i in1:total) {#draw sample from 30 person experiment with standard distribution data <-rnorm(30,mean =0, sd =1) p <-t.test(data)$p.valueif (p <0.05) { count = count +1 }}proportion_significant <- count/totalproportion_significant
How does this compare to the intended false-positive rate of \(\alpha=0.05\)?
The intended false-positive rate is \(\alpha=0.05\). It can be seen from both of the functions above that the replicated rates are also around 0.05, varying from a difference of less than 0.01.
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(upper_value =0.25) { all_data <-numeric(0) p <-0.2#loop until p value meets conditions while(p < upper_value && p >0.05) { frame <-replicate(30,(rnorm(30,mean =0, sd =1)))#append all samples together and update new p_value all_data <-c(all_data,frame) p <-t.test(all_data)$p.value }return(p)}
Now call this function 10k times and find out what happens.
#run above set amount of times and find resulting p_values run_simulation <-function(total =10000, upper_value =0.25) { all_pvalues =replicate(total, double.sample(upper_value)) results =sum(all_pvalues <0.05)/totalreturn(results)}run_simulation()
[1] 0.0861
Is there an inflation of false positives? How bad is it?
Yes, the p_value is inflated by quite a lot after this procedure. From my results, I see that it has inflated by more than 0.03.
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.
#the function has been updated to have upper_value as parameter (default = 0.25)a <-run_simulation(15000,0.5)b <-run_simulation(15000, 0.75)#any p_value larger than 0.05 is not significantc <-run_simulation(15000,0.06)a
[1] 0.1277333
b
[1] 0.1764
c
[1] 0
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
This simulation showed that the higher the upper threshold, the larger the resulting p_value. For this reason, when the upper threshold is around 0.75, the p_value goes up to 0.17 but when the upper threshold is as small as 0.06 the p_value zeroes out. This type of policy is not effective because the p_values shift drastically as the threshold that is being chosen shifts.