[Error with stan_glmer?] variable name=prior_scale_for_aux; dims declared=(); dims found=(1)

I’m getting an error when calling rstanarm’s stan_glmer using prior_aux=NULL. Using the default priors causes no problems. Here’s the most relevant error:

> fit_just_aux_ranef <- stan_glmer(value ~ variable + (1|boy), data=df, prior_aux = NULL)

Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=prior_scale_for_aux; dims declared=(); dims found=(1)  (in '/data/hyperparameters.stan' at line 4; included from 'model_continuous' at line 56)

failed to create the sampler; sampling not done
Error in check_stanfit(stanfit) : 
  Invalid stanfit object produced please report bug
Error in dimnamesGets(x, value) : 
  invalid dimnames given for “dgCMatrix” object

The longer version is that I pulled the boys’ shoes example from the BHH2 package, reformatted it into long format, and am running various analysis (lm, lmer, stan_glm, stan_glmer) for learning purposes. I ran into problems when I wanted to use stan_glmer and treat the boy number as a random effect rather than a fixed effect, and explored the use of flat priors for the analysis. It seems to be a problem with prior_aux=NULL. Example code found below.

So, is this a bug, or am I just missing something?
Thanks.

library(lme4)
library(rstanarm)

df<-structure(list(boy = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("1", 
 "2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "factor"), 
 variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("matA", 
 "matB"), class = "factor"), value = c(13.2, 8.2, 10.9, 14.3, 
 10.7, 6.6, 9.5, 10.8, 8.8, 13.3, 14, 8.8, 11.2, 14.2, 11.8, 
 6.4, 9.8, 11.3, 9.3, 13.6)), row.names = c(NA, -20L), class = "data.frame")

str(df)

fit_lm <- lm(value ~ variable + boy, data=df)
fit_lmer <- lmer(value ~ variable + (1|boy), data=df)
fit_default <- stan_glm(value ~ variable + boy, data=df)
fit_flat <- stan_glm(value ~ variable + boy, data=df,
                     prior=NULL, prior_intercept = NULL, prior_aux = NULL)

# this call to stan_glmer (default priors) works
fit_default_ranef <- stan_glmer(value ~ variable + (1|boy), data=df)

# these two calls to stan_glmer crash
fit_flat_ranef <- stan_glmer(value ~ variable + (1|boy), data=df,
                             prior=NULL, prior_intercept = NULL, prior_aux = NULL)
fit_just_aux_ranef <- stan_glmer(value ~ variable + (1|boy), data=df,
                                 prior_aux = NULL)
1 Like

Yeah you definitely found a bug, thanks for reporting this! I just fixed this on GitHub:

If you want to install the version from GitHub you can try:

Sys.setenv(MAKEFLAGS = "-j4") # change 4 to however many cores you can/want to use to parallelize install 
Sys.setenv("R_REMOTES_NO_ERRORS_FROM_WARNINGS" = "true")
remotes::install_github("stan-dev/rstanarm", INSTALL_opts = "--no-multiarch", force = TRUE)

You’ll need a working rstan package and the install will unfortunately be a bit slow because it has to compile all the Stan programs, unlike for the released version that has them already compiled.

1 Like

Great, good to know, thanks.

1 Like