-
Notifications
You must be signed in to change notification settings - Fork 0
/
stan_model_with_prior.stan
67 lines (54 loc) · 1.36 KB
/
stan_model_with_prior.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
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
//
// This Stan program defines a simple model, with a
// vector of values 'y' modeled as normally distributed
// with mean 'mu' and standard deviation 'sigma'.
//
// Learn more about model development with Stan at:
//
// http://mc-stan.org/users/interfaces/rstan.html
// https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started
//
data {
// Define variables in data
// Number of observations (an integer)
int<lower=0> N;
// Number of parameters
int<lower=0> p;
// Variables
real<lower=0> age[N];
int<lower=0> died[N];
int<lower=0> sex[N];
int<lower=0> job[N];
int<lower=0> urban[N];
int<lower=0> edu[N];
}
parameters {
// A-priori mean for prediction
real mu_p;
// Define parameters to estimate
real beta[p];
// standard deviation (a positve real number)
real<lower=0> sigma;
}
transformed parameters {
// Mean
real mu[N];
for (i in 1:N) {
mu[i] = beta[1] + beta[2]*died[i] + beta[3]*sex[i] + beta[4]*job[i] +
beta[5]*urban[i] + beta[6]*edu[i];
}
}
model {
// Weakly informative prior
mu_p ~ normal(60, 10);
sigma ~ normal(0,10);
// Likelihood part of the Bayesian inference
age ~ normal(mu, sigma);
}
generated quantities{
real predct_age;
vector[N] log_lik;
predct_age = normal_rng(mu_p, sigma);
for (i in 1:N)
log_lik[i] = normal_lpdf(age[i] | mu[i], sigma);
}