-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstan_models.Rmd
284 lines (231 loc) · 5.52 KB
/
stan_models.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
# (PART) Models {-}
# Stan models
Below are the Stan models built as a part of this course.
The original files are available in the GitHub repo in the "models" directory.
## Model: `8-schools.stan`
```stan
// 8 schools model from 'rstan' documentation.
// https://mc-stan.org/rstan/articles/rstan.html
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
real mu;
real<lower=0> tau;
vector[J] eta;
}
transformed parameters {
vector[J] theta;
theta = mu + tau * eta;
}
model {
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
}
```
## Model: `assignment06-bioassay.stan`
```stan
data {
int<lower=0> N; // number of data points
vector[N] x; // dose
int<lower=0> n[N]; // number of animals
int<lower=0> y[N]; // number of deaths
vector[2] mu; // prior on mean of theta
matrix<lower=0>[2, 2] sigma; // prior on covariance matrix of theta
}
parameters {
vector[2] mdl_params;
}
transformed parameters {
vector[N] theta;
theta = mdl_params[1] + mdl_params[2] * x;
}
model {
mdl_params ~ multi_normal(mu, sigma);
y ~ binomial_logit(n, theta);
}
```
## Model: `assignment07_factories_hierarchical.stan`
```stan
data {
int<lower=0> N; // number of data points per machine
int<lower=0> J; // number of machines
vector[J] y[N]; // quality control data points
}
parameters {
vector[J] mu;
real<lower=0> sigma;
real alpha;
real<lower=0> tau;
}
model {
// hyper-priors
alpha ~ normal(100, 10);
tau ~ normal(0, 10);
// priors
mu ~ normal(alpha, tau);
sigma ~ inv_chi_square(5);
// likelihood
for (j in 1:J){
y[,j] ~ normal(mu[j], sigma);
}
}
generated quantities {
// Compute the predictive distribution for the sixth machine.
real y6pred; // Leave for compatibility with earlier assignments.
vector[J] ypred;
real mu7pred;
real y7pred;
vector[J] log_lik[N];
y6pred = normal_rng(mu[6], sigma);
for (j in 1:J) {
ypred[j] = normal_rng(mu[j], sigma);
}
mu7pred = normal_rng(alpha, tau);
y7pred = normal_rng(mu7pred, sigma);
for (j in 1:J) {
for (n in 1:N) {
log_lik[n,j] = normal_lpdf(y[n,j] | mu[j], sigma);
}
}
}
```
## Model: `assignment07_factories_pooled.stan`
```stan
data {
int<lower=0> N; // number of data points
vector[N] y; // machine quality control data
}
parameters {
real mu;
real<lower=0> sigma;
}
model {
// priors
mu ~ normal(100, 10);
sigma ~ inv_chi_square(5);
// likelihood
y ~ normal(mu, sigma);
}
generated quantities {
real ypred;
vector[N] log_lik;
ypred = normal_rng(mu, sigma);
for (i in 1:N)
log_lik[i] = normal_lpdf(y[i] | mu, sigma);
}
```
## Model: `assignment07_factories_separate.stan`
```stan
data {
int<lower=0> N; // number of data points per machine
int<lower=0> J; // number of machines
vector[J] y[N]; // quality control data points
}
parameters {
vector[J] mu;
vector<lower=0>[J] sigma;
}
model {
// priors
for (j in 1:J) {
mu[j] ~ normal(100, 10);
sigma[j] ~ inv_chi_square(5);
}
// likelihood
for (j in 1:J){
y[,j] ~ normal(mu[j], sigma[j]);
}
}
generated quantities {
// Compute the predictive distribution for the sixth machine.
real y6pred;
vector[J] log_lik[N];
y6pred = normal_rng(mu[6], sigma[6]);
for (j in 1:J) {
for (n in 1:N) {
log_lik[n,j] = normal_lpdf(y[n,j] | mu[j], sigma[j]);
}
}
}
```
## Model: `assignment07-drownings.stan`
```stan
data {
int<lower=0> N; // number of data points
vector[N] x; // observation year
vector[N] y; // observation number of drowned
real xpred; // prediction year
}
parameters {
real alpha;
real beta;
real<lower=0> sigma; // fix: 'upper' should be 'lower'
}
transformed parameters {
vector[N] mu = alpha + beta*x;
}
model {
alpha ~ normal(135, 50); // prior on `alpha`
beta ~ normal(0, 26); // prior on `beta`
y ~ normal(mu, sigma); // fix: missing semicolor
}
generated quantities {
real ypred = normal_rng(alpha + beta*xpred, sigma); // fix: use `xpred`
}
```
## Model: `serial-dilution.stan`
```stan
data {
int<lower=0> N; // number of data points
int<lower=0> A; // constant used in model of measurement error
vector<lower=0>[N] x; // concentration values
vector<lower=0>[N] y; // observed color intensity
int<lower=0> M; // number of new x values
vector<lower=0>[M] xnew; // new x values
}
parameters {
vector<lower=0>[4] beta;
real<lower=0,upper=1> alpha;
real<lower=0> sigma;
}
transformed parameters {
vector<lower=0>[N] g;
vector<lower=0>[N] tau;
for (i in 1:N) {
g[i] = beta[1] + beta[2] / (1 + (x[i] / beta[3]) ^ (-beta[4]));
tau[i] = ((g[i] / A) ^ (2.0 * alpha)) * (sigma ^ 2.0);
}
}
model {
// Priors
alpha ~ beta(1, 1);
beta[1] ~ normal(10, 2.5);
beta[2] ~ normal(100, 5);
beta[3] ~ normal(0, 1);
beta[4] ~ normal(0, 2.5);
sigma ~ normal(0, 2.5);
// Likelihood
for (i in 1:N) {
y[i] ~ normal(g[i], tau[i]);
}
}
generated quantities {
vector[N] ypred;
vector[N] log_lik;
vector[M] g_hat;
vector[M] tau_hat;
vector[M] ynew;
for (i in 1:N) {
ypred[i] = normal_rng(g[i], tau[i]);
log_lik[i] = normal_lpdf(y[i] | g[i], tau[i]);
}
for (i in 1:M) {
g_hat[i] = beta[1] + beta[2] / (1 + (xnew[i] / beta[3]) ^ (-beta[4]));
tau_hat[i] = ((g_hat[i] / A) ^ (2.0 * alpha)) * (sigma ^ 2.0);
ynew[i] = normal_rng(g_hat[i], tau_hat[i]);
}
}
```