stan-dev / rstanarm

rstanarm R package for Bayesian applied regression modeling
https://mc-stan.org/rstanarm
GNU General Public License v3.0
389 stars 133 forks source link

posterior_predict() doesn't work on three-level models #300

Open tjmahr opened 6 years ago

tjmahr commented 6 years ago

Summary:

posterior_predict() with new group levels doesn't work on three-level models because pp_b_ord() assembles the wrong name containing the substring _NEW_. It raises a no matches bug error.

Description:

A model fit with a three-level random effect like (1 | a/b/c) doesn't work with posterior_predict() when there are new group levels. I traced through the code and found the error happens when it's trying to match Z_names with columns from the posterior distribution in the function pp_b_ord().

Aside from fixing this code, is there any way to manually pull out the posterior samples for _NEW_ levels?

Reproducible Steps:

library(lme4)
library(dplyr)
library(rstanarm)

zzz <- stan_lmer(
  angle ~ 1 + (1 | replicate/recipe/temp), 
  data = cake,
  prior_PD = TRUE, 
  iter = 100, 
  chains = 1)

new_cake <- cake %>% 
  distinct(temp, recipe) %>% 
  mutate(replicate = 100)

# Failure on a new grouping level
posterior_linpred(zzz, newdata = new_cake)

old_cake <- cake %>% 
  distinct(temp, recipe) %>% 
  mutate(replicate = 1)

# Works on a known grouping level
posterior_linpred(zzz, newdata = old_cake)

RStanARM Version:

2.17.4

R Version:

3.4.x

Operating System:

Windows 10

bgoodri commented 6 years ago

Still having trouble figuring out how to fix this. Will probably have to release 2.18 with the bug unfixed.

melondonkey commented 4 years ago

I have encountered this bug as well. Are there any known workarounds.