-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path16.2_Smart_Drug_Robust.Rmd
More file actions
320 lines (244 loc) · 6.31 KB
/
16.2_Smart_Drug_Robust.Rmd
File metadata and controls
320 lines (244 loc) · 6.31 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
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
---
title: "16.2: Smart Drug (robust estimation)"
output: html_notebook
---
## Introduction
This is the example from Section 16.2 of Kruschke on the (fictitious) effect of the "smart drug", this time using a Student t model for robust estimation. Make sure the "TwoGroupIQ.csv" file is in your project directory.
## Preliminaries
Load necessary packages:
```{r}
library(tidyverse)
library(rstan)
library(tidybayes)
library(bayesplot)
```
Set Stan to save compiled code.
```{r}
rstan_options(auto_write = TRUE)
```
Set Stan to use parallel processing where possible.
```{r}
options(mc.cores = parallel::detectCores())
```
## Data
```{r}
IQ_data <- read_csv("TwoGroupIQ.csv")
IQ_data
```
For this 1-sample test, we will only look at those who took the smart drug.
```{r}
IQ1_data <- IQ_data %>%
filter(Group == "Smart Drug")
IQ1_data
```
```{r}
ggplot(IQ1_data, aes(x = Score)) +
geom_histogram(binwidth = 10, boundary = 50)
```
```{r}
N <- NROW(IQ1_data)
y <- IQ1_data$Score
stan_data <- list(N = N, y = y)
```
## Prior
### Simulation code
The book uses `nu_minus_one` with an exponential prior. Then we will add one to to `nu_minus_one` to constrain `nu` to take values larger than 1. For the rate `R_nu_minus_one`, we'll use `1.0/29` as this implies an "average" of 30 degrees of freedom (which is when the Student t model starts looking indistinguishable from a normal model).
```{stan, output.var = "IQ_robust_prior", cache = TRUE}
data {
int<lower = 0> N;
array[N] real y;
}
transformed data {
real M;
real<lower = 0> S;
real<lower = 0> R_nu_minus_one;
real<lower = 0> R_sigma;
M = 100; // mean centered at 100
S = 50; // mean between 0 and 200
R_nu_minus_one = 1.0/29; // df of 30
R_sigma = 1.0/50; // sd of 50
}
generated quantities {
real<lower = 0> nu_minus_one;
real<lower = 1> nu;
real mu;
real<lower = 0> sigma;
array[N] real y_sim;
nu_minus_one = exponential_rng(R_nu_minus_one);
nu = nu_minus_one + 1;
mu = normal_rng(M, S);
sigma = exponential_rng(R_sigma);
for(n in 1:N) {
y_sim[n] = student_t_rng(nu, mu, sigma);
}
}
```
```{r, cache = TRUE}
fit_IQ_robust_prior <- sampling(IQ_robust_prior,
data = stan_data,
chains = 1,
algorithm = "Fixed_param",
seed = 11111,
refresh = 0)
```
```{r}
samples_IQ_robust_prior <- tidy_draws(fit_IQ_robust_prior)
samples_IQ_robust_prior
```
### Examine prior
```{r}
mcmc_hist(samples_IQ_robust_prior,
pars = "nu")
```
```{r}
mcmc_hist(samples_IQ_robust_prior,
pars = "mu")
```
```{r}
mcmc_hist(samples_IQ_robust_prior,
pars = "sigma")
```
```{r}
mcmc_pairs(fit_IQ_robust_prior,
pars = c("nu", "mu", "sigma"))
```
### Prior predictive distribution
```{r}
y_sim_IQ_robust_prior <- samples_IQ_robust_prior %>%
dplyr::select(starts_with("y_sim")) %>%
as.matrix()
```
```{r}
ppd_hist(ypred = y_sim_IQ_robust_prior[1:20,])
```
```{r}
ppd_boxplot(ypred = y_sim_IQ_robust_prior[1:10, ],
notch = FALSE)
```
As before, these priors are likely too wide, especially now that the Student t model is capable of generating outliers.
```{r}
ppd_intervals(ypred = y_sim_IQ_robust_prior)
```
## Model
In the model code below, we list `mu` and `sigma` as the parameters of interest. The `a` and `b` parameters are defined in the `transformed parameters` block. They make the model easier to read, but they are not the main objects of our inference.
```{stan, output.var = "IQ_robust", cache = TRUE}
data {
int<lower = 0> N;
array[N] real y;
}
transformed data {
real M;
real<lower = 0> S;
real<lower = 0> R_nu_minus_one;
real<lower = 0> R_sigma;
M = 100; // mean centered at 100
S = 50; // mean between 0 and 200
R_nu_minus_one = 1.0/29; // df of 30
R_sigma = 1.0/50; // sd of 50
}
parameters {
real<lower = 0> nu_minus_one;
real mu;
real<lower = 0> sigma;
}
transformed parameters {
real<lower = 1> nu;
nu = nu_minus_one + 1;
}
model {
nu_minus_one ~ exponential(R_nu_minus_one);
mu ~ normal(M, S);
sigma ~ exponential(R_sigma);
y ~ student_t(nu, mu, sigma);
}
generated quantities {
array[N] real y_sim;
for (n in 1:N) {
y_sim[n] = student_t_rng(nu, mu, sigma);
}
}
```
```{r, cache = TRUE}
fit_IQ_robust <- sampling(IQ_robust,
data = stan_data,
seed = 11111,
refresh = 0)
```
```{r}
samples_IQ_robust <- tidy_draws(fit_IQ_robust)
samples_IQ_robust
```
## Model diagnostics
```{r}
stan_trace(fit_IQ_robust,
pars = c("nu", "mu", "sigma"))
```
```{r}
mcmc_acf(fit_IQ_robust,
pars = c("nu", "mu", "sigma"))
```
```{r}
mcmc_rhat(rhat(fit_IQ_robust))
```
```{r}
mcmc_neff(neff_ratio(fit_IQ_robust))
```
## Model summary
```{r}
print(fit_IQ_robust,
pars = c("nu", "mu", "sigma"))
```
## Model visualization
```{r}
mcmc_areas(fit_IQ_robust, pars = "nu")
```
```{r}
mcmc_areas(fit_IQ_robust, pars = "mu")
```
```{r}
mcmc_areas(fit_IQ_robust, pars = "sigma")
```
```{r}
pairs(fit_IQ_robust,
pars = c("nu", "mu", "sigma"))
```
## Posterior predictive check
```{r}
y_sim_IQ_robust <- samples_IQ_robust %>%
dplyr::select(starts_with("y_sim")) %>%
as.matrix()
```
```{r}
ppc_hist(y = y,
yrep = y_sim_IQ_robust[1:19, ])
```
```{r}
ppc_boxplot(y = y,
yrep = y_sim_IQ_robust[1:10, ],
notch = FALSE)
```
```{r}
ppc_dens_overlay(y = y,
yrep = y_sim_IQ_robust[1:50, ])
```
```{r}
ppc_scatter(y = y,
yrep = y_sim_IQ_robust[1:9, ])
```
```{r}
ppc_scatter_avg(y = y,
yrep = y_sim_IQ_robust)
```
```{r}
ppc_intervals(y = y,
yrep = y_sim_IQ_robust[1:19, ])
```
```{r}
ppc_stat(y = y,
yrep = y_sim_IQ_robust)
```
```{r}
ppc_stat_2d(y = y,
yrep = y_sim_IQ_robust)
```
There are a few simulated data sets that are outliers, including one huge one. It doesn't invalidate our inference; our estimates of the parameters of interest are stable and reasonable. If there were more outliers like this, though, we would want to reconsider our model to make it less possible to get extreme outliers like this.