Skip to content

Instantly share code, notes, and snippets.

@krishnanraman
Created December 6, 2020 02:42
Show Gist options
  • Save krishnanraman/a710ebac8a6af5aaf21c1f63728a0722 to your computer and use it in GitHub Desktop.
Save krishnanraman/a710ebac8a6af5aaf21c1f63728a0722 to your computer and use it in GitHub Desktop.
hierarchical model Stan
rm(list=ls())
library(rstan)
rstan_options(auto_write = FALSE)
options(mc.cores = parallel::detectCores())
N=1000
data = list(N=N,x1<-rnorm(N,5,2),x2<-rnorm(N,7,2),x3<-rnorm(N,9,2),y<-x1+x2+x3)
fit <- suppressMessages(stan(file = '~/Desktop/695/test.stan', data = data, iter=11000, warmup=1000, chains=2, seed=483892929, refresh=11000))
print(fit)
plot(fit)
data {
int<lower=0> N;
vector[N] y;
vector[N] x1;
vector[N] x2;
vector[N] x3;
}
parameters {
real mu1;
real mu2;
real mu3;
real mu;
real<lower=0> phi;
real<lower=0> sigma;
}
model {
x1 ~ normal(mu1, sigma);
x2 ~ normal(mu2, sigma);
x3 ~ normal(mu3, sigma);
mu1 ~ normal(mu, phi);
mu2 ~normal(mu,phi);
mu3 ~normal(mu,phi);
y ~ normal(mu*3, sqrt(3)*sigma);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment