I am getting reacquainted with Stan. I am currently trying to understand the following issue. Below, I fit a simple random intercept model. My intercepts are in the alpha vector. I have the following questions:
How do I interpret mean_alpha
, and why do I get such a different estimate for it compared to mu_alpha
? Specifically, both have a posterior mean of 0.41 but the posterior standard deviation for mean_alpha
is essentially 0 but for mu_alpha
it is 0.1
library(rstan)
# Set up the data
set.seed(123)
N <- 1000 # Number of observations
J <- 100 # Number of persons
person <- sample(1:J, N, replace = TRUE) # Group/person indices
group_intercepts <- rnorm(J, mean = 0.5, sd = 1) # Generating random intercepts for simplicity
Y <- group_intercepts[person] + rnorm(N, mean = 0, sd = 0.1) # Response variable
data_list <- list(N = N, J = J, person = person, Y = Y)
# Stan model code
stan_code <- '
data {
int<lower=0> N;
int<lower=0> J;
int<lower=1, upper=J> person[N];
vector[N] Y;
}
parameters {
vector[J] alpha;
real mu_alpha;
real<lower=0> sigma_alpha;
real<lower=0> sigma_Y;
}
model {
alpha ~ normal(mu_alpha, sigma_alpha);
for (n in 1:N) {
Y[n] ~ normal(alpha[person[n]], sigma_Y);
}
}
generated quantities {
real mean_alpha = mean(alpha);
}
'
# Compile and fit the model
fit <- stan(model_code = stan_code, data = data_list, iter = 2000, chains = 4, warmup = 1000, seed = 123)
# Print the fit summary
print(fit, pars = c("mu_alpha", "mean_alpha"))
stan_dens(fit, pars = c("mu_alpha", "mean_alpha"))
# Plot traceplots for diagnostics
library("bayesplot")
traceplot(fit, pars = c("sigma_Y", "mu_alpha"))
library(lme4)
lmeFit <- lmer(Y ~ 1 + (1|Person), data = data.frame(Person = person, Y = Y))