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_lmer` cannot be used through namespacing (`rstanarm::stan_lmer`) #589

Open rempsyc opened 1 year ago

rempsyc commented 1 year ago

Summary:

stan_lmer cannot be used through namespacing (rstanarm::stan_lmer), which creates issues in packages depending on rstanarm.

Description:

stan_lmer cannot be used through namespacing (rstanarm::stan_lmer), because it generates an error, could not find function "stan_glmer". This creates issues in packages depending on rstanarm, such as the report package.

A workaround that we have been using is super-assignment, of the following type:

stan_glmer <<- rstanarm::stan_glmer

However, for mysterious reasons, during our automated tests, even though we never explicitly load the rstanarm library, sometimes it will load itself. Once loaded, it will not allow super-assignment to overwrite its function name, with error cannot change value of locked binding for 'stan_glmer'. This can be reproduced with the steps below:

Reproducible Steps:

R.Version()$version.string
#> [1] "R version 4.3.0 (2023-04-21 ucrt)"
R.Version()$platform
#> [1] "x86_64-w64-mingw32"
packageVersion("rstanarm")
#> [1] '2.21.4'

x <- rstanarm::stan_lmer(Sepal.Length ~ Petal.Length + (1 | Species),
                         data = iris, refresh = 0, iter = 1000, seed = 333)
#> Error in stan_glmer(formula = Sepal.Length ~ Petal.Length + (1 | Species), : could not find function "stan_glmer"

suppressPackageStartupMessages(library(rstanarm))

stan_glmer <<- rstanarm::stan_glmer
#> Error in eval(expr, envir, enclos): cannot change value of locked binding for 'stan_glmer'

Created on 2023-05-07 with reprex v2.0.2

Question

Is there any official workaround for this issue?

rempsyc commented 1 year ago

This can be reproduced with R version 4.2.2 on Windows locally and on automated tests on GitHub actions machines.

R.Version()$version.string
#> [1] "R version 4.2.2 (2022-10-31 ucrt)"
R.Version()$platform
#> [1] "x86_64-w64-mingw32"
packageVersion("rstanarm")
#> [1] '2.21.3'

x <- rstanarm::stan_lmer(Sepal.Length ~ Petal.Length + (1 | Species),
                         data = iris, refresh = 0, iter = 1000, seed = 333)
#> Error in stan_glmer(formula = Sepal.Length ~ Petal.Length + (1 | Species), : could not find function "stan_glmer"

suppressPackageStartupMessages(library(rstanarm))

stan_glmer <<- rstanarm::stan_glmer
#> Error in eval(expr, envir, enclos): cannot change value of locked binding for 'stan_glmer'

Created on 2023-05-07 with reprex v2.0.2