-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment4.Rmd
503 lines (356 loc) · 17.4 KB
/
Assignment4.Rmd
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
---
title: "Assignment 4 - Applying meta-analytic priors"
author: "Riccardo Fusaroli"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Assignment 4
In this assignment we do the following:
- we run a Bayesian meta-analysis of pitch variability in ASD, based on previously published literature
- we analyze pitch variability in ASD in two new studies using both a conservative and a meta-analytic prior
- we assess the difference in model quality and estimates using the two priors.
The questions you need to anwser are: What are the consequences of using a meta-analytic prior? Evaluate the models with conservative and meta-analytic priors. Discuss the effects on estimates. Discuss the effects on model quality. Discuss the role that meta-analytic priors should have in scientific practice. Should we systematically use them? Do they have drawbacks? Should we use them to complement more conservative approaches? How does the use of meta-analytic priors you suggest reflect the skeptical and cumulative nature of science?
### Step by step suggestions
Step 1: Perform a meta-analysis of pitch variability from previous studies of voice in ASD
- the data is available as Ass4_MetaAnalysisData.tsv
- You should calculate Effect size (cohen's d) and Standard Error (uncertainty in the Cohen's d) per each study, using escalc() from the metafor package (also check the livecoding intro)
- N.B. we're only interested in getting a meta-analytic effect size for the meta-analytic prior (and not e.g. all the stuff on publication bias). See a brms tutorial here: https://vuorre.netlify.com/post/2016/09/29/meta-analysis-is-a-special-case-of-bayesian-multilevel-modeling/ The formula is EffectSize | se(StandardError) ~ 1 + (1 | Paper). Don't forget prior definition, model checking, etc.
- Write down the results of the meta-analysis in terms of a prior for step 2.
```{r, Meta-analysis}
#load data and packages
pacman::p_load(pacman, tidyverse, brms, psych, metafor, parallel, patchwork)
cores = detectCores()
dataMetaAnalysis <- read_delim("Ass4_MetaAnalysisData.tsv",
"\t")
dataMetaAnalysis <- dataMetaAnalysis %>%
mutate_at(
c(
"PitchVariabilityASD_Mean",
"PitchVariabilityASD_SD",
"PitchVariabilityTD_Mean",
"PitchVariabilityTD_SD"
),
as.numeric
)
#describe(dataMetaAnalysis)
#head(dataMetaAnalysis)
#removing papers without titles
dataMetaAnalysis <- dataMetaAnalysis %>% subset(!is.na(Paper))
#creating yi and vi for metaAnalysis
dataMetaAnalysis <- escalc(
measure = "SMD",
n1i = TD_N,
n2i = ASD_N,
m1i = PitchVariabilityTD_Mean,
m2i = PitchVariabilityASD_Mean,
sd1i = PitchVariabilityTD_SD,
sd2i = PitchVariabilityASD_SD,
data = dataMetaAnalysis,
slab = Paper
)
dataMetaAnalysis <- dataMetaAnalysis %>%
mutate(StandardError = sqrt(vi)) %>%
rename(EffectSize = yi)
summary(dataMetaAnalysis$EffectSize)
summary(dataMetaAnalysis$StandardError)
metaFormula <-
bf(EffectSize | se (StandardError) ~ 1 + (1 | Population))
get_prior(metaFormula, data = dataMetaAnalysis, family = gaussian())
#I Think this is a skeptial prior, but I'm not sure...
sd(dataMetaAnalysis$EffectSize, na.rm = T)#0.5
metaAnalysis_Prior <- c(prior(normal(0, 1), class = Intercept),
prior(normal(0, .25), class = sd))
metaAnalysis_m0 <- brm(
metaFormula,
data = dataMetaAnalysis,
family = gaussian(),
prior = metaAnalysis_Prior,
sample_prior = "only",
chains = 2,
cores = cores,
file = "priorCheckSkeptical"
)
#prior predictive Check
priorCheck_metaAnalysis_m0 <-
pp_check(metaAnalysis_m0, nsamples = 100)
priorCheck_metaAnalysis_m0 + labs(title = "Prior Check",
subtitle = "Skeptical Priors",
caption = "EffectSize | se (StandardError) ~ 1+ (1|Population)")
#fitting model
metaAnalysis_m1 <- brm(
metaFormula,
data = dataMetaAnalysis,
family = gaussian(),
prior = metaAnalysis_Prior,
sample_prior = T,
chains = 2,
cores = cores,
file = "metaAnalysis_m1"
)
#posterior predictive check
pp_check(metaAnalysis_m1, nsamples = 100) + labs(title = "Posterior Check",
subtitle = "Skeptical Priors",
caption = "EffectSize | se (StandardError) ~ 1+ (1|Population)")
#checking model
summary(metaAnalysis_m1) # effect of 0.43 sd = 0.10
analysisMean <- fixef(metaAnalysis_m1)[[1]]
analysis_SE <- fixef(metaAnalysis_m1)[[2]]
analysisHeterogeneity = 0.31
```
Step 2: Analyse pitch variability in ASD in two new studies for which you have access to all the trials (not just study level estimates)
- the data is available as Ass4_data.csv. Notice there are 2 studies (language us, and language dk), multiple trials per participant, and a few different ways to measure pitch variability (if in doubt, focus on pitch IQR, interquartile range of the log of fundamental frequency)
- Also, let's standardize the data, so that they are compatible with our meta-analytic prior (Cohen's d is measured in SDs).
- Is there any structure in the dataset that we should account for with random/varying effects? How would you implement that? Or, if you don't know how to do bayesian random/varying effects or don't want to bother, is there anything we would need to simplify in the dataset?
```{r, step 2... MEGA-ANALYSIS!}
#loading data
newData <-
read_csv("Ass4_data.csv", col_types = cols(ID = col_character()))
#checking classes
#head(newData)
#lapply(newData, class)
newData <- newData %>% mutate(PitchVariability = scale(Pitch_IQR))
hist(newData$PitchVariability)
ASD_DensityPitch <-
ggplot(data = filter(newData, Diagnosis == "ASD"), aes(PitchVariability, )) + geom_density() + ggtitle("Distribution of Pitch Variability IQ in ASD") + theme(legend.position = "none") + theme(plot.title = element_text(color = "red", size = 10, face = "bold"))
TD_DensityPitch <-
ggplot(data = filter(newData, Diagnosis == "TD"), aes(PitchVariability, )) + geom_density() + ggtitle("Distribution of Pitch Variability IQ in TD") + theme(legend.position = "none") + theme(plot.title = element_text(color = "red", size = 10, face = "bold"))
ASD_DensityPitch + TD_DensityPitch
#lets take a look at the structure of the data
##language only have two levels and it doesn't change, so that is a fixed effect.
##ID people might vary differently, so we will conewStudiesider that a varying effect
##Verbal IQ may interact with diagnosis and it the data does not look too skewed to cause trouble
ASD_Density_VIQ <-
ggplot(data = filter(newData, Diagnosis == "ASD"), aes(VIQ, color = ID)) + geom_density() + ggtitle("Distribution of Verbal IQ in ASD") + theme(legend.position = "none") + theme(plot.title = element_text(size = 10, face = "bold"))
TD_Density_VIQ <-
ggplot(data = filter(newData, Diagnosis == "TD"), aes(VIQ, color = ID)) + geom_density() + ggtitle("Distribution of Verbal IQ in TD") + theme(legend.position = "none") + theme(plot.title = element_text(size = 10, face = "bold"))
ASD_Density_VIQ + TD_Density_VIQ
```
Step 3: Build a regression model predicting Pitch variability from Diagnosis.
- how is the outcome distributed? (likelihood function). NB. given we are standardizing, and the meta-analysis is on that scale, gaussian is not a bad assumption. Lognormal would require us to convert the prior to that scale.
- how are the parameters of the likelihood distribution distributed? Which predictors should they be conditioned on? Start simple, with Diagnosis only. Add other predictors only if you have the time and energy!
- use a skeptical/conservative prior for the effects of diagnosis. Remember you'll need to motivate it.
- Evaluate model quality. Describe and plot the estimates.
```{r, MEGA-ANALYSIS pt 2!!!}
#formulas
newStudies_f0 <- bf(PitchVariability ~ 1 + Diagnosis + (1 | ID))
newStudies_f1 <-
bf(PitchVariability ~ 0 + Language + Diagnosis:Language + (1 |
ID))
get_prior(newStudies_f0, newData, family = gaussian())
#skeptical prior
newStudiesPrior0 <- c(
prior(normal(0, .3), class = Intercept),
prior(normal(0, .1), class = b),
prior(normal(0, .1), class = sd),
prior(normal(.5, .3), class = sigma)
)
newStudies_m0_pc <- brm(
newStudies_f0,
data = newData,
family = gaussian(),
prior = newStudiesPrior0,
sample_prior = "only",
chains = 2,
cores = cores,
file = "NewStudies_m0Prior"
)
pp_check(newStudies_m0_pc, nsamples = 100) + labs(title = "Prior Check",
subtitle = "Skeptical Priors",
caption = "PitchVariability ~ 1 + Diagnosis + (1 | ID)")
newStudies_m0 <- brm(
newStudies_f0,
data = newData,
family = gaussian(),
prior = newStudiesPrior0,
sample_prior = T,
chains = 2,
cores = cores,
file = "NewStudies_m0"
)
plot(newStudies_m0)
pp_check(newStudies_m0, nsamples = 100) + labs(title = "Posterior Check",
subtitle = "Skeptical Priors",
caption = "PitchVariability ~ 1 + Diagnosis + (1 | ID)")
# hypothesis testing
hypo_m0 <-
plot(hypothesis(newStudies_m0, "DiagnosisTD < 0"), plot = F)
hypo_m0[[1]] + labs(title = "DiagnosisTD < 0",
subtitle = "Skeptical Priors",
caption = "PitchVariability ~ 1 + Diagnosis + (1 | ID)") + theme(strip.text = element_blank())
hypothesis(newStudies_m0, "DiagnosisTD < 0")
#interactionmodel
get_prior(newStudies_f1, newData, family = gaussian())
# set priors
NewStudiesPrior1 <- c(
prior(normal(0, .1), class = b, coef = "Languagedk"),
prior(normal(0, .1), class = b, coef = "Languageus"),
prior(normal(0, .1), class = b, coef = "Languagedk:DiagnosisTD"),
prior(normal(0, .1), class = b, coef = "Languageus:DiagnosisTD"),
prior(normal(0, .1), class = sd),
prior(normal(.5, .1), class = sigma)
)
newStudies_m1_pc <- brm(
newStudies_f1,
newData,
family = gaussian(),
prior = NewStudiesPrior1,
sample_prior = "only",
chains = 2,
cores = cores,
file = "newStudies_m1_pc"
)
pp_check(newStudies_m1_pc, nsamples = 100) + labs(title = "Prior Check",
subtitle = "Skeptical Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID) ")
newStudies_m1 <- brm(
newStudies_f1,
newData,
family = gaussian(),
prior = NewStudiesPrior1,
sample_prior = T,
chains = 2,
cores = cores,
file = "newStudies_m1"
)
# posterior predictive check
pp_check(newStudies_m1, nsamples = 100) + labs(title = "Posterior Check",
subtitle = "Skeptical Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)")
# hypothesis testing
hypo_m1_dk <-
plot(hypothesis(newStudies_m1, "Languagedk:DiagnosisTD < 0"), plot = F)
hypo_m1_dk[[1]] + labs(title = "Languagedk:DiagnosisTD < 0",
subtitle = "Skeptical Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)") + theme(strip.text = element_blank())
hypo_m1_us <-
plot(hypothesis(newStudies_m1, "Languageus:DiagnosisTD < 0"), plot = F)
hypo_m1_us[[1]] + labs(title = "Languageus:DiagnosisTD < 0",
subtitle = "Skeptical Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)") + theme(strip.text = element_blank())
hypothesis(newStudies_m1, "Languagedk:DiagnosisTD < 0")
hypothesis(newStudies_m1, "Languageus:DiagnosisTD < 0")
hypo_m1_dk_us <- plot(
hypothesis(
newStudies_m1,
"Languagedk:DiagnosisTD < Languageus:DiagnosisTD"
),
plot = F
)
hypo_m1_dk_us[[1]] + labs(title = "Languagedk:DiagnosisTD < Languageus:DiagnosisTD",
subtitle = "Skeptical Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)") + theme(strip.text = element_blank())
hypothesis(newStudies_m1,
"Languagedk:DiagnosisTD < Languageus:DiagnosisTD")
summary(newStudies_m1)
condEff <- plot(conditional_effects(newStudies_m1),plot = F)
condEff[[1]] + labs(title = "Effect of Language",
subtitle = "Skeptical Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)")
condEff[[2]] + labs(title = "Effect of Language:Diagnosis",
subtitle = "Skeptical Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)")
newStudies_m0 <- add_criterion(newStudies_m0, criterion = "loo")
newStudies_m1 <- add_criterion(newStudies_m1, criterion = "loo")
loo_model_weights(newStudies_m0, newStudies_m1)
```
Step 4: Now re-run the model with the meta-analytic prior
- Evaluate model quality. Describe and plot the estimates.
```{r}
analysis_Mean <- fixef(metaAnalysis_m1)[[1]]
analysis_SE <- fixef(metaAnalysis_m1)[[2]]
analysisHeterogeneity = 0.31
# new priors set from old meta study ^
newStudiesInformed_prior <- c(
prior(normal(.2, .3), class = b, coef = "Languagedk"),
prior(normal(.2, .3), class = b, coef = "Languageus"),
prior(normal(-0.43, .1), class = b, coef = "Languagedk:DiagnosisTD"),
prior(normal(-0.43, .1), class = b, coef = "Languageus:DiagnosisTD"),
prior(normal(0, .1), class = sd),
prior(normal(.32, .1), class = sigma)
)
newStudiesInformed_m1_pc <- brm(
newStudies_f1,
newData,
family = gaussian(),
prior = newStudiesInformed_prior,
sample_prior = "only",
chains = 2,
cores = cores,
file = "newStudiesInformed_m1_pc"
)
pp_check(newStudiesInformed_m1_pc, nsamples = 100) + labs(title = "Prior Check",
subtitle = "Informed Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID) ")
newStudiesInformed_m1 <- brm(
newStudies_f1,
newData,
family = gaussian(),
prior = newStudiesInformed_prior,
sample_prior = T,
chains = 2,
cores = cores,
file = "newStudiesInformed_m"
)
pp_check(newStudiesInformed_m1, nsamples = 100) + labs(title = "Posterior Check",
subtitle = "Informed Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID) ")
# hypothesis testing
hypoInformed_dk <-
plot(hypothesis(newStudiesInformed_m1, "Languagedk:DiagnosisTD < 0"),
plot = F)
hypoInformed_dk[[1]] + labs(title = "Languagedk:DiagnosisTD < 0",
subtitle = "Informed Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)") + theme(strip.text = element_blank())
hypoInformed_us <-
plot(hypothesis(newStudiesInformed_m1, "Languageus:DiagnosisTD < 0"),
plot = F)
hypoInformed_us[[1]] + labs(title = "Languageus:DiagnosisTD < 0",
subtitle = "Informed Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)") + theme(strip.text = element_blank())
hypothesis(newStudiesInformed_m1, "Languagedk:DiagnosisTD < 0")
hypothesis(newStudiesInformed_m1, "Languageus:DiagnosisTD < 0")
hypoInformed_dk_us <- plot(
hypothesis(
newStudiesInformed_m1,
"Languagedk:DiagnosisTD < Languageus:DiagnosisTD"
),
plot = F
)
hypoInformed_dk_us[[1]] + labs(title = "Languagedk:DiagnosisTD < Languageus:DiagnosisTD",
subtitle = "Informed Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)") + theme(strip.text = element_blank())
hypothesis(newStudiesInformed_m1,
"Languagedk:DiagnosisTD < Languageus:DiagnosisTD")
summary(newStudiesInformed_m1)
newStudiesInformed_m1 <-
add_criterion(newStudiesInformed_m1,
criterion = "loo",
reloo = T)
```
Step 5: Compare the models
- Plot priors and posteriors of the diagnosis effect in both models
- Compare posteriors between the two models
- Compare the two models (LOO)
- Discuss how they compare and whether any of them is best.
```{r}
loo_model_weights(newStudies_m1, newStudiesInformed_m1)
# plot hypotheses
hypo_m1_dk[[1]] + labs(title = "Languagedk:DiagnosisTD < 0",
subtitle = "Skeptical Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)") + theme(strip.text = element_blank())
hypothesis(newStudies_m1, "Languagedk:DiagnosisTD < 0")
hypo_m1_us[[1]] + labs(title = "Languageus:DiagnosisTD < 0",
subtitle = "Skeptical Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)") + theme(strip.text = element_blank())
hypothesis(newStudies_m1, "Languageus:DiagnosisTD < 0")
hypoInformed_dk[[1]] + labs(title = "Languagedk:DiagnosisTD < 0",
subtitle = "Informed Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)") + theme(strip.text = element_blank())
hypothesis(newStudiesInformed_m1, "Languagedk:DiagnosisTD < 0")
hypoInformed_us[[1]] + labs(title = "Languageus:DiagnosisTD < 0",
subtitle = "Informed Priors",
caption = "PitchVariability~0 + Language + Diagnosis:Language+(1|ID)") + theme(strip.text = element_blank())
hypothesis(newStudiesInformed_m1, "Languageus:DiagnosisTD < 0")
```