stan-dev / rstanarm

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

`stan_gamm4()` and response expression #545

Open fweber144 opened 3 years ago

fweber144 commented 3 years ago

Summary:

stan_gamm4() seems to have a problem with an expression on the left-hand (response) side of the formula.

Description:

stan_gamm4() throws an unexpected error in case of an expression on the left-hand (response) side of the formula (see "Reproducible Steps" below). Such an error does not occur with stan_glm() or stan_glmer().

Reproducible Steps:

# Original source: Example section of `?rstanarm::stan_gamm4`.
library(rstanarm)
options(mc.cores = parallel::detectCores(logical = FALSE))
dat <- mgcv::gamSim(1, n = 400, scale = 2) ## simulate 4 term additive truth
## Now add 20 level random effect`fac'...
dat$fac <- fac <- as.factor(sample(1:20, 400, replace = TRUE))
dat$y <- dat$y + model.matrix(~ fac - 1) %*% rnorm(20) * .5
br <- stan_gamm4(abs(y) ~ s(x0) + x1 + s(x2), data = dat, random = ~ (1 | fac),
                 chains = 1,
                 iter = 500, # for example speed
                 seed = 1140350788)

throws the error

Error in eval(predvars, data, env) : object 'y' not found

which does not occur with stan_glm() or stan_glmer():

br_glm <- stan_glm(abs(y) ~ x0 + x1 + x2, data = dat,
                   chains = 1,
                   iter = 500, # for example speed
                   seed = 1140350788)

br_glmer <- stan_glmer(abs(y) ~ x0 + x1 + x2 + (1 | fac), data = dat,
                       chains = 1,
                       iter = 500, # for example speed
                       seed = 1140350788)

RStanARM Version:

2.21.2 (from https://mc-stan.org/r-packages/)

R Version:

4.1.1

Operating System:

Ubuntu 20.04.2

fweber144 commented 3 years ago

Perhaps this might be related to issue #253?