forked from psych251/problem_sets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps4.qmd
168 lines (106 loc) · 6.75 KB
/
ps4.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
---
title: 'Psych 251 PS4: Simulation + Analysis'
author: "Mike Frank"
date: "2019"
format:
html:
toc: true
---
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).
```{r}
fvs <- read.csv("data/FVS2011-hands.csv")
```
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.
```{r}
unique_fvs <- fvs[match(unique(fvs$subid), fvs$subid),]
hist(unique_fvs$age)
```
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.
```{r, message=F,warning=F}
library(ggplot2)
```
```{r}
ggplot(fvs, aes(x=age, y=hand.look)) + geom_point() + facet_wrap(~condition) + xlab("Age") + ylab("Hand Looking") + geom_smooth(method = 'lm')
```
What do you conclude from this pattern of data?
>Perhaps there was a significant positive correlation between age and hand looking in the faces_plus condition, but not in the faces_medium condition.
What statistical analyses would you perform here to quantify these differences?
>Perhaps a t-test between linear regression models of each of the conditions.
# Part 2: Simulation
```{r, warning=F, message=F}
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.
```{r}
count = 0
for (i in 1:10000) {
if (t.test(rnorm(30))$p.value < .05) {
count = count + 1
}
}
forloop_proportion = count / 10000
```
Next, do this using the `replicate` function:
```{r}
d <- replicate(n = 10000, t.test(rnorm(30))$p.value)
replicate_proportion = length(which(d < 0.05)) / 10000
```
How does this compare to the intended false-positive rate of $\alpha=0.05$?
> About the same!
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.
```{r}
double.sample <- function(x) {
count = 0
data = rnorm(30)
pval = t.test(data)$p.value
while (pval > 0.05) {
if (pval > x) {
break
}
data = c(data, rnorm(30))
pval = t.test(data)$p.value
}
if (pval < 0.05) {
count = 1
}
return (count)
}
```
Now call this function 10k times and find out what happens.
```{r}
sigsum <- sum(replicate(n = 10000,double.sample(0.25)))
hacked_proportion <- sigsum/10000
hacked_proportion
```
Is there an inflation of false positives? How bad is it?
> There is - false positives occur between 8 and 9 percent of the time now, rather than 5 percent.
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.
```{r}
sigsum_0.5 <- sum(replicate(n = 10000,double.sample(0.5)))
proportion_0.5 <- sigsum_0.5/10000
proportion_0.5
sigsum_0.75 <- sum(replicate(n = 10000,double.sample(0.75)))
proportion_0.75 <- sigsum_0.75/10000
proportion_0.75
# This is my code for doubling the sample whenever there is a non-significant p-value. Because doubling the sample does not guarantee a significant p value, this code often runs for a very long time (potentially forever), thus I have commented it out.
##sigsum_1 <- sum(replicate(n = 10,double.sample(1)))
##proportion_1 <- sigsum_1/10
##proportion_1
```
What do you conclude on the basis of this simulation? How bad is this kind of data-dependent policy?
> It's pretty bad; the proportion of significant p-values increases from 5% to ~13% with the less than 0.5 criteria, and 18% with the less than 0.75 criteria.