-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment-4.Rmd
224 lines (152 loc) · 4.21 KB
/
assignment-4.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
---
title: "Assignment 4"
description: |
Effect of tDCS stimulation on episodic memory
date: 05-28-2021
author:
- first_name: "Andrew"
last_name: "Ellis"
url: https://github.com/awellis
affiliation: Kognitive Psychologie, Wahrnehmung und Methodenlehre, Universität Bern
affiliation_url: https://www.kog.psy.unibe.ch
orcid_id: 0000-0002-2788-936X
citation_url: https://awellis.github.io/learnmultilevelmodels/asssignment-4.html
bibliography: ./bibliography.bib
output:
distill::distill_article:
toc: true
toc_float: true
toc_depth: 2
code_folding: true
self_contained: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
library(brms)
theme_set(theme_grey(base_size = 14) +
theme(panel.grid = element_blank()))
```
# Effect of tDCS on memory
You are going to analyze data from a study in which 34 elderly subjects were given anodal (activating) tDCS over their left tempero-parietal junction (TPJ). Subjects were instructed to learn association between images and pseudo-words (data were inspired by @antonenkoTDCSinducedEpisodicMemory2019).
Episodic memory is measured by percentage of correctly recalled word-image pairings. We also have response times for correct decisions.
Each subject was tested 5 times in the TPJ stimulation condition, and a further 5 times in a sham stimulation condition.
The variables in the dataset are:
```
subject: subject ID
stimulation: TPJ or sham (control)
block: block
age: age
correct: accuracy per block
rt: mean RTs for correct responses
```
You are mainly interested in whether recall ability is better during the TPJ stimulation condition than during sham.
```{r}
d <- read_csv("https://raw.githubusercontent.com/kogpsy/neuroscicomplab/main/data/tdcs-tpj.csv")
```
```{r}
glimpse(d)
```
```{r}
d <- d %>%
mutate(across(c(subject, stimulation, block), ~as_factor(.))) %>%
drop_na()
```
:::exercise
1) specify a linear model
2) specify a null model
3) compare models using approximate leave-one-out cross-validation
4) estimate a Bayes factor for the effect of TPJ stimulation
+ using the Savage-Dickey density ratio test
+ using the bridge sampling method
5) control for subjects' age
:::
## Linear model
```{r}
fit1 <- brm(correct ~ stimulation + (stimulation| subject),
prior = prior(normal(0, 1), class = b),
data = d,
file = "models/ass4-1")
```
```{r}
summary(fit1)
```
```{r}
mcmc_plot(fit1, "b_", type = "areas")
```
## Null model
```{r}
fit2 <- brm(correct ~ 1 + (stimulation| subject),
data = d,
file = "models/ass4-2")
```
## Model comparison using loo
```{r}
loo_fit_1 <- loo(fit1)
loo_fit_1
```
```{r}
loo_fit_2 <- loo(fit2)
loo_fit_2
```
```{r}
loo_compare(loo_fit_1, loo_fit_2)
```
## Model comparison via Bayes factor
```{r}
fit1_bf <- brm(correct ~ stimulation + (stimulation| subject),
prior = prior(normal(0, 1), class = b),
data = d,
iter = 1e4,
save_pars = save_pars(all = TRUE),
file = "models/ass4-bf_1")
```
```{r}
fit2_bf <- brm(correct ~ 1 + (stimulation| subject),
prior = prior(normal(0, 1), class = b),
data = d,
iter = 1e4,
save_pars = save_pars(all = TRUE),
file = "models/ass4-bf_2")
```
```{r}
loglik_1 <- bridge_sampler(fit1_bf, silent = TRUE)
loglik_2 <- bridge_sampler(fit2_bf, silent = TRUE)
```
```{r}
BF_10 <- bayes_factor(loglik_1, loglik_2)
BF_10
```
## Control for age
```{r}
fit3 <- brm(correct ~ stimulation + age + (stimulation | subject),
prior = prior(normal(0, 1), class = b),
data = d,
file = "models/ass4-3")
```
```{r}
fit3
```
```{r}
loo_fit_3 <- loo(fit3)
```
```{r}
loo_compare(loo_fit_1, loo_fit_3)
```
```{r}
fit3_bf <- brm(correct ~ stimulation + age + (stimulation| subject),
prior = prior(normal(0, 1), class = b),
data = d,
iter = 1e4,
save_pars = save_pars(all = TRUE),
file = "models/ass4-bf_3")
```
```{r}
loglik_3 <- bridge_sampler(fit3_bf, silent = TRUE)
```
```{r}
BF_01 <- bayes_factor(loglik_3, loglik_1)
BF_01
```