Repository navigation
Expand file tree
/
Copy pathStan_Models.Rmd
More file actions
301 lines (250 loc) · 6.67 KB
/
Copy pathStan_Models.Rmd
File metadata and controls
301 lines (250 loc) · 6.67 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
---
title: "Stan Models"
author: "Jason Doll"
date: "May 18, 2018"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Exercise 1: Estimating Mean and SD
```{r, engine="stan",output.var="Ex1", eval=F, echo=T}
data{
int<lower=0> n; //number of obserations
vector[n] y; //observations as a vector
}
parameters {
real<lower=0> sigma; //standard deviation to be estimated
real mu; //Mean to be estimated
}
model {
//reference priors
sigma ~ cauchy(0,5);
mu ~ normal(0,100);
//likelihood, loop through number of observations
for(i in 1:n){
y[i] ~ normal(mu, sigma);
}
}
```
\newpage
## Exercise 2: Linear Regression
```{r, engine="stan",output.var="Ex1", eval=F, echo=T}
data {
int<lower=0> n; //number of observations
vector[n] x; //observed x values, predictors
vector[n] y; //observed y values, response
}
parameters {
real<lower=0> sigma; //standard deviation
real alpha; //y-intercept
real beta; //slope
}
model {
//reference priors
alpha ~ normal(0,100);
beta ~ normal(0,100);
sigma ~ cauchy(0,5);
//likelihood, loop through number of observations
for(i in 1:n){
y[i] ~ normal(alpha + beta * x[i], sigma);
}
}
```
\newpage
## Exercise 3(a): Stan code for centered parameterization
```{r, engine="stan",output.var="Ex1", eval=F, echo=T}
parameters{
real y;
vector[9] x;
}
model {
y~normal(0,3);
x~normal(0,exp(y/2));
}
```
## Exercise 3(b): Stan code for non-centered parameterization
```{r, engine="stan",output.var="Ex1", eval=F, echo=T}
parameters {
real y_raw;
vector[9] x_raw;
}
transformed parameters {
real y;
vector[9] x;
y = 3.0 * y_raw;
x = exp(y/2) * x_raw;
}
model {
y_raw ~ normal(0, 1); // implies y ~ normal(0, 3)
x_raw ~ normal(0, 1); // implies x ~ normal(0, exp(y/2))
}
```
\newpage
## Exercise 4(a): Stan code for centered parameterization
```{r, engine="stan",output.var="Ex1", eval=F, echo=T}
data{
int<lower=0> n; //number of observations
vector[n] y; //observations as a vector
vector[n] x; //observed x values, predictors
int gp[n]; //subject indicator
int ngps; //number of groups
}
parameters{
real<lower=0> sigma_y; //individual standard deviation to be estimated
real<lower=0> sigma_mu; //group standard deviation to be estimated
real gpmu[ngps]; //Group mean
real mu; //Global mean across all groups
real beta; //slope
}
model {
//reference priors
sigma_y ~ cauchy(0,5);
sigma_mu ~ cauchy(0,5);
beta ~ normal(0,100);
mu ~ normal(0,100);
for(k in 1:ngps){
gpmu[k] ~ normal(mu,sigma_mu); //individual group means
}
//likelihood, loop through number of observations
for(i in 1:n){
y[i] ~ normal(gpmu[gp[i]] + beta * x[i], sigma_y);
}
}
```
\newpage
## Exercise 4(b): Stan code for non-centered parameterization
```{r, engine="stan",output.var="Ex1", eval=F, echo=T}
data{
int<lower=0> n; //number of obserations
vector[n] y; //observations as a vector
vector[n] x; //observed x values, predictors
int gp[n]; //subject indicator
int ngps; //number of groups
}
parameters{
real<lower=0> sigma_y; //individual standard deviation to be estimated
real<lower=0> sigma_mu; //group standard deviation to be estimated
real gpmu_raw[ngps]; //Group mean_raw
real mu; //Global mean across all groups
real beta; //slope
}
transformed parameters{
real gpmu[ngps]; //Group mean
for(k in 1:ngps){
gpmu[k] = mu + gpmu_raw[k] * sigma_mu;
}
}
model {
//reference priors
sigma_y ~ cauchy(0,5);
sigma_mu ~ cauchy(0,5);
beta ~ normal(0,100);
mu ~ normal(0,100);
for(k in 1:ngps){
gpmu_raw[k] ~ normal(0,1);//implies gpmu[k] ~ normal(mu,sigma_mu); //individual group mean
}
//likelihood, loop through number of observations
for(i in 1:n){
y[i] ~ normal(gpmu[gp[i]] + beta * x[i], sigma_y);
}
}
```
\newpage
## Exercise 5(a): WAIC, Stan code for centered parameterization
```{r, engine="stan",output.var="Ex1", eval=F, echo=T}
data {
int<lower=0> n; //number of observations
vector[n] x; //observed x values, predictors
vector[n] y; //observed y values, response
}
parameters {
real<lower=0> sigma; //standard deviation
real alpha; //y-intercept
real beta; //slope
}
model {
//reference priors
alpha ~ normal(0,100);
beta ~ normal(0,100);
sigma ~ cauchy(0,5);
//likelihood, loop through number of observations
for(i in 1:n){
y[i] ~ normal(alpha + beta * x[i], sigma);
}
}
generated quantities{
vector[n] log_lik;
for ( i in 1:n ) {
log_lik[i] = normal_lpdf( y[i] | alpha + beta * x[i], sigma);
}
}
```
\newpage
## Exercise 5(b): WAIC, Stan code for non-centered parameterization
```{r, engine="stan",output.var="Ex1", eval=F, echo=T}
data{
int<lower=0> n; //number of obserations
vector[n] y; //observations as a vector
vector[n] x; //observed x values, predictors
int gp[n]; //subject indicator
int ngps; //number of groups
}
parameters{
real<lower=0> sigma_y; //individual standard deviation to be estimated
real<lower=0> sigma_mu; //group standard deviation to be estimated
real gpmu_raw[ngps]; //Group mean_raw
real mu; //Global mean across all groups
real beta; //slope
}
transformed parameters{
real gpmu[ngps]; //Group mean
for(k in 1:ngps){
gpmu[k] = mu + gpmu_raw[k] * sigma_mu;
}
}
model {
//reference priors
sigma_y ~ cauchy(0,5);
sigma_mu ~ cauchy(0,5);
beta ~ normal(0,100);
mu ~ normal(0,100);
for(k in 1:ngps){
gpmu_raw[k] ~ normal(0,1);//implies gpmu[k] ~ normal(mu,sigma_mu); //individual group mean
}
//likelihood, loop through number of observations
for(i in 1:n){
y[i] ~ normal(gpmu[gp[i]] + beta * x[i], sigma_y);
}
}
generated quantities{
vector[n] log_lik;
for ( i in 1:n ) {
log_lik[i] = normal_lpdf( y[i] | gpmu[gp[i]] + beta * x[i], sigma_y);
}
}
```
\newpage
## Exercise 6: Iris data, Linear Regression
```{r, engine="stan",output.var="Ex1", eval=F, echo=T}
data {
int<lower=0> n; //number of observations
vector[n] x; //observed x values, predictors
vector[n] y; //observed y values, response
}
parameters {
real<lower=0> sigma; //standard deviation
real alpha; //y-intercept
real beta; //slope
}
model {
//reference priors
alpha ~ normal(0,100);
beta ~ normal(0,100);
sigma ~ cauchy(0,5);
//likelihood, loop through number of observations
for(i in 1:n){
y[i] ~ normal(alpha + beta * x[i], sigma);
}
}
```