I think I have encountered an underlying error when trying to draw from the posterior predicted distribution of stan_nlmer models.
It appears that the group specific terms are not being passed to posterior_predict. In other words, it seems to behave as if re.form = NA and any predictions for groups end up drawn from the same posterior distribution. I have been able to replicate the problem using posterior_predict, add_predicted_draws etc.
The example below is drawn from the stan_nlmer vignette Estimating Generalized (Non-)Linear Models with Group-Specific Terms with rstanarm. Indeed if i exactly repeat the example in the vignette and change the level specified for Tree to a within sample tree, it generates the same distribution of predicted values as the out of sample tree.
data("Orange", package = "datasets")
Orange$age <- Orange$age / 100
Orange$circumference <- Orange$circumference / 100
post1 <- stan_nlmer(circumference ~ SSlogis(age, Asym, xmid, scal) ~ Asym|Tree,
data = Orange, cores = 2, seed = 12345, init_r = 0.5)
nd1 <- expand.grid(age = 1:20, Tree = as.factor(1:5))
pp <- posterior_predict(post1, newdata = nd1)
prediction <- colMeans(pp)
nd_samples <- cbind(nd1, prediction)
ggplot(Orange, aes(x = age, y = circumference, colour = Tree, fill = Tree)) +
geom_point() +
geom_line(aes(y=prediction, x=age, colour=Tree), data = nd_samples)
#all 5 prediction curves are identical
I have also tried passing the group level terms (e.g. Asy|Tree , ~1|Tree) direct to re.form but this gave errors, although i am not quite sure I specified them correctly (I haven’t got the hang of nlmer group level specification).
Thanks, Josh
- Operating System:Windows10
- rstanarm Version:2.21.1