-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecificationCurveAnalysis.R
More file actions
286 lines (194 loc) · 13.9 KB
/
SpecificationCurveAnalysis.R
File metadata and controls
286 lines (194 loc) · 13.9 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Specification curve analysis on simulated data set
# Set-up ----
library(ggplot2)
library(MASS)
windowsFonts(font = windowsFont("Times New Roman"))
simulations = 1000
strength = .34 # predictor ~ outcome variable covariance
# From correlation matrix to covariance matrix ----
set.seed(1)
R <- rbind(c(1,.9,strength,strength,.1,.15), c(.9,1,strength,strength,.1,.15), c(strength,strength,1,.85,.05,.15), c(strength,strength,.85,1,.05,.15), c(.1,.1,.05,.05,1,.1), c(.15,.15,.15,.15,.1,1)) # Correlation matrix
sd <- c(15, 20, 3, 7, 10, 11) # Standard deviation vector
sigma <- diag(sd) %*% R %*% diag(sd) # Covariance matrix
mu <- c(10,30,20,0,15,15) # Mean vector
# Create data set ----
df <- as.data.frame(mvrnorm(n=100, mu=mu, Sigma=sigma))
names(df) <- c('outcome_1', 'outcome_2', 'predictor_1', 'predictor_2', 'confounding_1', 'confounding_2')
df$predictor_3 <- df$predictor_1 + df$predictor_2
df_original <- df
# Inferential statistics specification curve analysis ----
estimate_median <- NULL
p_value_share <- NULL
## Loop through zero-effect multiverses
for (i in 1:simulations){
estimates <- NULL
print(paste0('Start simulation multiverse ', i))
df <- transform(df_original, outcome_1 = sample(outcome_1)) # Randomly shuffle the dependent variables
#df <- transform(df, outcome_2 = sample(outcome_2)) # Randomly shuffle the dependent variables
df_1 <- df
df_1$outcome <- df_1$outcome_1
df_2 <- df
df_2$outcome <- df_2$outcome_2
## Outlier removal
Q1 <- quantile(df_1$outcome, .25) # IQR outcome_1
Q3 <- quantile(df_1$outcome, .75)
IQR <- IQR(df_1$outcome)
df_3 <- df_1[df_1$outcome > (Q1 - 1.5*IQR) & df_1$outcome < (Q3 + 1.5*IQR),]
nrow(df_3)
Q1 <- quantile(df_2$outcome, .25) # IQR outcome_2
Q3 <- quantile(df_2$outcome, .75)
IQR <- IQR(df_2$outcome)
df_4 <- df_2[df_2$outcome > (Q1 - 1.5*IQR) & df_2$outcome < (Q3 + 1.5*IQR),]
nrow(df_4)
## Create multiverse
outlier_detection <- c('none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR', 'IQR')
predictor_variable <- c('1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum', '1', '2', 'sum')
confounding_variable <- c('none', '1', '1 + interaction', '2', '2 + interaction', 'none', '1', '1 + interaction', '2', '2 + interaction', 'none', '1', '1 + interaction', '2', '2 + interaction', 'none', '1', '1 + interaction', '2', '2 + interaction', 'none', '1', '1 + interaction', '2', '2 + interaction', 'none', '1', '1 + interaction', '2', '2 + interaction', 'none', '1', '1 + interaction', '2', '2 + interaction', 'none', '1', '1 + interaction', '2', '2 + interaction', 'none', '1', '1 + interaction', '2', '2 + interaction', 'none', '1', '1 + interaction', '2', '2 + interaction', 'none', '1', '1 + interaction', '2', '2 + interaction', 'none', '1', '1 + interaction', '2', '2 + interaction')
outcome_variable <- c('1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2', '1', '2')
estimates <- c(
summary(lm(outcome ~ predictor_1, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_2, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_3, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 + confounding_1, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 + confounding_1, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 + confounding_1, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 * confounding_1, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 * confounding_1, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 * confounding_1, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 + confounding_2, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 + confounding_2, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 + confounding_2, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 * confounding_2, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 * confounding_2, df_1))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 * confounding_2, df_1))$coefficients[2,1],
## df_2 - no outlier removal, outcome_2
summary(lm(outcome ~ predictor_1, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_2, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_3, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 + confounding_1, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 + confounding_1, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 + confounding_1, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 * confounding_1, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 * confounding_1, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 * confounding_1, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 + confounding_2, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 + confounding_2, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 + confounding_2, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 * confounding_2, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 * confounding_2, df_2))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 * confounding_2, df_2))$coefficients[2,1],
## df_3 - outlier removal, outcome_1
summary(lm(outcome ~ predictor_1, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_2, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_3, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 + confounding_1, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 + confounding_1, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 + confounding_1, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 * confounding_1, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 * confounding_1, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 * confounding_1, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 + confounding_2, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 + confounding_2, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 + confounding_2, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 * confounding_2, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 * confounding_2, df_3))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 * confounding_2, df_3))$coefficients[2,1],
## df_4 - outlier removal, outcome_2
summary(lm(outcome ~ predictor_1, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_2, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_3, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 + confounding_1, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 + confounding_1, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 + confounding_1, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 * confounding_1, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 * confounding_1, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 * confounding_1, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 + confounding_2, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 + confounding_2, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 + confounding_2, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_1 * confounding_2, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_2 * confounding_2, df_4))$coefficients[2,1],
summary(lm(outcome ~ predictor_3 * confounding_2, df_4))$coefficients[2,1]
)
p_values <- c(
summary(lm(outcome ~ predictor_1, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_2, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_3, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 + confounding_1, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 + confounding_1, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 + confounding_1, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 * confounding_1, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 * confounding_1, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 * confounding_1, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 + confounding_2, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 + confounding_2, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 + confounding_2, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 * confounding_2, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 * confounding_2, df_1))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 * confounding_2, df_1))$coefficients[2,4],
## df_2 - no outlier removal, outcome_2
summary(lm(outcome ~ predictor_1, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_2, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_3, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 + confounding_1, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 + confounding_1, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 + confounding_1, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 * confounding_1, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 * confounding_1, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 * confounding_1, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 + confounding_2, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 + confounding_2, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 + confounding_2, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 * confounding_2, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 * confounding_2, df_2))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 * confounding_2, df_2))$coefficients[2,4],
## df_3 - outlier removal, outcome_1
summary(lm(outcome ~ predictor_1, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_2, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_3, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 + confounding_1, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 + confounding_1, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 + confounding_1, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 * confounding_1, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 * confounding_1, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 * confounding_1, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 + confounding_2, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 + confounding_2, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 + confounding_2, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 * confounding_2, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 * confounding_2, df_3))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 * confounding_2, df_3))$coefficients[2,4],
## df_4 - outlier removal, outcome_2
summary(lm(outcome ~ predictor_1, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_2, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_3, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 + confounding_1, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 + confounding_1, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 + confounding_1, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 * confounding_1, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 * confounding_1, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 * confounding_1, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 + confounding_2, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 + confounding_2, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 + confounding_2, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_1 * confounding_2, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_2 * confounding_2, df_4))$coefficients[2,4],
summary(lm(outcome ~ predictor_3 * confounding_2, df_4))$coefficients[2,4]
)
temp <- data.frame(p_values, estimates)
p_value_share <- append(p_value_share, nrow(temp[temp$p_values <.05 & temp$estimates > 0,]))
estimate_median <- append(estimate_median, median(estimates))
}
estimate_median <- data.frame(estimate_median)
p_value_share <- data.frame(p_value_share)
# True multiverse(black line) versus zero-effect multiverses (density distribution) ----
## Median effect
ggplot(estimate_median, aes(x = estimate_median)) +
geom_density() +
geom_vline(xintercept = .5846434) +
theme_bw()
## Proportion of significant effects
ggplot(p_value_share, aes(x = p_value_share)) +
geom_density() +
geom_vline(xintercept = 42) +
theme_bw()