I eventually figured this one out with much help from Doing Bayesian Data Analysis (Kruschke) and Data Analysis Using Regression and Multilevel/Hierarchical Models (Gelman). This model gives varying intercepts and slopes and the correlation between them.
y = dependent variable
sLvl = participant id at each data point
aLvlx = between subjects factor for each id
NaLvl = Number of levels for the between subject's factor
Ntotal = total length of data in long form
modelstring = "
model {
for( r in 1 : Ntotal ) {
y[r] ~ dnorm( mu[r] , tau )
mu[r] <- b0[ sLvl[r] ] + b1[ sLvl[r] ] * x[r]
}
#General priors
tau ~ dgamma( sG , rG )
sG <- pow(m,2)/pow(d,2)
rG <- m/pow(d,2)
m ~ dgamma(1, 0.001)
d ~ dgamma(1, 0.001)
#Subject level priors
for ( s in 1 : NsLvl ) {
b0[s] <- B[s,1]
b1[s] <- B[s,2]
B[s, 1:2] ~ dmnorm( B.hat[s, ], Tau.B[ , ] )
B.hat[s,1] <- hix1[aLvlx[s]]
B.hat[s,2] <- hix2[aLvlx[s]]
}
Tau.B[1:2 , 1:2] <- inverse(Sigma.B[,])
Sigma.B[1,1] <- pow(tau0G, 2)
Sigma.B[2,2] <- pow(tau1G, 2)
Sigma.B[1,2] <- rho * tau0G * tau1G
Sigma.B[2,1] <- Sigma.B[1,2]
tau0G ~ dunif(0.001,100)
tau1G ~ dunif(0.001,100)
rho ~ dunif(-1,1)
#Between subjects level priors
for ( k in 1:NaLvl ) {
hix1[k] ~ dnorm( 0 , 0.0000001 )
hix2[k] ~ dnorm( 0 , 0.0000001 )
}
}"
writeLines(modelstring,con="model.txt")
In the answers there (if I understood correctly) I learned that
within-subject variance does not effect inferences made about group
means and it is ok to simply take the averages of averages to
calculate group mean, then calculate within-group variance and use
that to perform significance tests.
Let me develop this idea here. The model for the individual observations is
$$y_{ijk}= \mu_i + \alpha_{ij} + \epsilon_{ijk}$$, where :
$y_{ijk}$ is the $k$-th measurement of individual $j$ of group $i$
$\alpha_{ij} \sim_{\text{iid}} {\cal N}(0, \sigma^2_b)$ is the random effect for individual $j$ of group $i$
$\epsilon_{ijk} \sim_{\text{iid}} {\cal N}(0, \sigma^2_w)$ is the within-error
In my answer to your first question, I have suggested you to note that one obtains a classical (fixed effects) Gaussian linear model for the subjects means $\bar y_{ij\bullet}$. Indeed you can easily check that $$\bar y_{ij\bullet} = \mu_i + \delta_{ij}$$ with $$\delta_{ij} = \alpha_{ij} + \frac{1}{K}\sum_k \epsilon_{ijk}
\sim_{\text{iid}} {\cal N}(0, \sigma^2) \quad \text{where } \quad \boxed{\sigma^2=\sigma^2_b+\frac{\sigma^2_w}{K}},$$
assuming $K$ repeated measurements for each individual. This is nothing but the one-way ANOVA model with a fixed factor.
And then I claimed that in order to draw inference about the $\mu_i$ you can simply consider the simple classical linear model whose observations are the subjects means $\bar y_{ij\bullet}$. Update 12/04/2014: Some examples of this idea are now written on my blog: Reducing a model to get confidence intervals. I'm under the impression that this always work when we average the data over the levels of a random effect.
In the answers there (if I understood correctly) I learned that
within-subject variance does not effect inferences made about group
means and it is ok to simply take the averages of averages to
calculate group mean, then calculate within-group variance and use
that to perform significance tests. I would like to use a method where
the larger the within subject variance the less sure I am about the
group means or understand why it does not make sense to desire that.
As you see from the boxed formula, the within-variance $\sigma^2_w$ plays a role in the model for the observed group means.
Best Answer
Usually you can do the predictions in JAGS. Below is a regression example with FEV (something to do with lung capacity) as the dependent variable and age and smoking indicator as predictors.
FEV20s and FEV20ns are the predicted FEV values for a 20 year old smoker and a 20 year old non-smoker.
Example from: Bayesian Ideas and Data Analysis