forked from avehtari/BDA_R_demos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlin_std.stan
40 lines (40 loc) · 908 Bytes
/
lin_std.stan
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
// Gaussian linear model with standardized data
data {
int<lower=0> N; // number of data points
vector[N] x; //
vector[N] y; //
real xpred; // input location for prediction
}
transformed data {
vector[N] x_std;
vector[N] y_std;
real xpred_std;
x_std = (x - mean(x)) / sd(x);
y_std = (y - mean(y)) / sd(y);
xpred_std = (xpred - mean(x)) / sd(x);
}
parameters {
real alpha;
real beta;
real<lower=0> sigma_std;
}
transformed parameters {
vector[N] mu_std;
mu_std = alpha + beta*x_std;
}
model {
alpha ~ normal(0, 1);
beta ~ normal(0, 1);
y_std ~ normal(mu_std, sigma_std);
}
generated quantities {
vector[N] mu;
real<lower=0> sigma;
real ypred;
vector[N] log_lik;
mu = mu_std*sd(y) + mean(y);
sigma = sigma_std*sd(y);
ypred = normal_rng((alpha + beta*xpred_std)*sd(y)+mean(y), sigma*sd(y));
for (i in 1:N)
log_lik[i] = normal_lpdf(y[i] | mu[i], sigma);
}