Skip to content

Instantly share code, notes, and snippets.

@credpath
Created August 31, 2018 22:07
Show Gist options
  • Save credpath/b2f5aeaadacfa7cca9d25d29faf5da4f to your computer and use it in GitHub Desktop.
Save credpath/b2f5aeaadacfa7cca9d25d29faf5da4f to your computer and use it in GitHub Desktop.
# This is the stan code to implement the model
data
{
int<lower=1> N; // number of observations
int<lower=1> G; // number of clusters
vector[N] y; // the predicted variable (income)
vector[N] x; // the predictor variable (education)
}
parameters
{
vector<lower=1> alpha; // intercept for each cluser
vector beta; // slope for each cluster
vector[2] mu; // intercept and slope means
vector[2] sigma; // intercept and slope standard errors
corr_matrix[2] rho; // correlation matrix for intercept and slope
}
transformed parameters
{
vector[2] params = [log(alpha) beta]'; // put log_intercept and slope into a vector
cov_matrix[2] S = diag_matrix[sigma] * rho * diag_matrix[sigma]; // formula for covariance matrix of intercept and slope
}
model
{
z ~ normal(0,1);
mvz ~ multi_normal([0 0]',[[1 0] [0 1]]);
rho ~ lkj_corr(2); // rho has LKJcorr distribution
log(y) = log(75000) + z*log(446); // distribution of household income is log normal with mean 75 000 and standard error 446
params = mu + mvz*S; // log_intercept and slope are distributed multivariate normal with mean mu and covariance matrix S
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment