-
Notifications
You must be signed in to change notification settings - Fork 10
/
8schools.R
56 lines (51 loc) · 959 Bytes
/
8schools.R
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
## Eight schools data` `
y <- c(28, 8, -3, 7, -1, 1, 18, 12)
sigma <- c(15, 10, 16, 11, 9, 11, 10, 18)
standata <- list(y =y, sigma = sigma,J=length(y))
model_code <- "
data {
int<lower=0> J;
vector[J] y;
vector[J] sigma;
}
parameters {
vector[J] theta;
real mu;
real<lower = 0> tau;
}
model {
y ~ normal(theta, sigma);
theta ~ normal(mu, tau);
mu ~normal(0, 3);
tau ~ normal(0, 3);
}
"
library(rstan)
stan_data <- list(y = y, sigma = sigma, J = length(y))
fit <- stan(model_code = model_code, data = stan_data)
## BAD
## Attempt 2!
model_code2 <- "
data {
int<lower=0> J;
vector[J] y;
vector[J] sigma;
}
parameters {
vector[J] z;
real mu;
real<lower = 0> tau;
}
transformed parameters {
// ANY TRANSFORMATION OF PARAMETERS GOES HERE!!!!!
vector[J] theta = mu + tau*z;
}
model {
y ~ normal(theta, sigma);
z ~ normal(0, 1);
mu ~normal(0, 3);
tau ~ normal(0, 3);
}
"
fit2 <- stan(model_code = model_code2, data = stan_data)
print(fit2)