Here is mu multilevel model:
data {
int<lower=1> N; //number of observations
real M[N]; //reaction times
real X[N]; //predictor (days of sleep deprivation)
// grouping factor
int<lower=1> J; //number of subjects
int<lower=1,upper=J> Subject[N]; //subject id
}
parameters {
vector[2] beta; // fixed-effects parameters
real<lower=0> sigma_e; // residual std
vector<lower=0>[2] sigma_u; // random effects standard deviations
//declare L_u to be the Choleski factor of a 2x2 correlation matrix
cholesky_factor_corr[2] L_u;
matrix[2,J] z_u; // random effect matrix
}
transformed parameters {
// this transform random effects so that they have the correlation
// matrix specified by the correlation matrix above
matrix[2,J] u;
u = diag_pre_multiply(sigma_u, L_u) * z_u;
}
model {
real mu; // conditional mean of the dependent variable
//priors
L_u ~ lkj_corr_cholesky(2); // LKJ prior for the correlation matrix
to_vector(z_u) ~ normal(0,5);
sigma_e ~ normal(0, 5); // prior for residual standard deviation
beta[1] ~ normal(0, 0.5); // prior for fixed-effect intercept
beta[2] ~ normal(0, 2); // prior for fixed-effect slope
//likelihood
for (i in 1:N){
mu = beta[1] + u[1,Subject[i]] + beta[2]*X[i] + u[2,Subject[i]]*X[i];
M[i] ~ normal(mu, sigma_e);
}
}
This is my stan model. However, when I try to run it using my data, it does not work.
Warning messages:
1: The largest R-hat is NA, indicating chains have not mixed.
Running the chains for more iterations may help. See
http://mc-stan.org/misc/warnings.html#r-hat
2: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
Running the chains for more iterations may help. See
http://mc-stan.org/misc/warnings.html#bulk-ess
3: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
Running the chains for more iterations may help. See
http://mc-stan.org/misc/warnings.html#tail-ess [sim.csv|attachment]
Here the 'data' in R:
data=list(N = nrow(MyData),
X = MyData$x,
M = MyData$m,
J = length(unique(MyData$id)),
Subject = as.numeric(factor(MyData$id,
labels = 1:length(unique(MyData$id)))))
Here is the model in R:
fit = stan(file = 'mediationHM.stan', data=data, iter=4000, chains =4).
What am I doing wrong?
(upload://nMC1lZfQXULLasNi8i4Js20MEl4.csv) (49.9 KB)