Skip to content

possible issue with the posterior_vs_prior function when using regex_pars to match a single parameter occurrence #662

Closed

Description

@brian-bucher

Hello,

I've done a bit of reading after running into some errors, and I think there might be an issue with the posterior_vs_prior function in rstanarm when using regex_pars to match a single parameter occurrence. Using the lme4::Pastes dataset, if I fit (1|batch/cask) and use regex_pars to match capital-S "Sigma" it works and pulls both. If I fit just (1|batch) or (1|cask) and try to match the only "Sigma" (capital S) I get an Error:

Error in s[, labs, drop = FALSE]: ! incorrect number of dimensions

Also, matching pars = "sigma" works to get just lower-case-s sigma, but regex_pars = "sigma" gives the same dimensions error.

I'm not a very experienced coder, so I don't know what to make of this other than it seems like it should work to match a single occurrence. If this is expected behavior, is there a flexible way to match either 1 or multiple parameter occurrences with regex_pars? Thank you.

I created a repex, but cut out the chains fitting so it's not so long.

#
# R code apparently showing rstanarm::posterior_vs_prior() having a problem with
# regex_pars only matching one pars.
#

library(rstanarm)
#> Loading required package: Rcpp
#> This is rstanarm version 2.32.2
#> - See https://mc-stan.org/rstanarm/articles/priors for changes to default priors!
#> - Default priors may change, so it's safest to specify priors, even if equivalent to the defaults.
#> - For execution on a local, multicore CPU with excess RAM we recommend calling
#>   options(mc.cores = parallel::detectCores())
library(bayesplot)
#> This is bayesplot version 1.16.0
#> - Online documentation and vignettes at mc-stan.org/bayesplot
#> - bayesplot theme set to bayesplot::theme_default()
#>    * Does _not_ affect other ggplot2 plots
#>    * See ?bayesplot_theme_set for details on theme setting

# use the Pastes dataset
df1 <- lme4::Pastes

# ignore div_trans
fit_stan_lmer1 <- stan_lmer(strength ~ 1 + (1|batch/cask), data = df1)
#> 
#> SAMPLING FOR MODEL 'continuous' NOW (CHAIN 1).


fit_stan_lmer2 <- stan_lmer(strength ~ 1 + (1|batch), data = df1)
#> 
#> SAMPLING FOR MODEL 'continuous' NOW (CHAIN 1).


fit_stan_lmer3 <- stan_lmer(strength ~ 1 + (1|cask), data = df1)
#> 
#> SAMPLING FOR MODEL 'continuous' NOW (CHAIN 1).


# for conciseness
names_fit_stan_lmer1 <- names(as.data.frame(fit_stan_lmer1))
grep("b\\[", names_fit_stan_lmer1, value = TRUE, invert = TRUE)
#> [1] "(Intercept)"                              
#> [2] "sigma"                                    
#> [3] "Sigma[cask:batch:(Intercept),(Intercept)]"
#> [4] "Sigma[batch:(Intercept),(Intercept)]"

# posterior_vs_prior(fit_stan_lmer1, group_by_parameter = TRUE) + theme_default()  # ok
# posterior_vs_prior(fit_stan_lmer1, pars="alpha") + theme_default()  # ok
posterior_vs_prior(fit_stan_lmer1, pars="sigma")          # ok
#> 
#> Drawing from prior...
#> Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
#> ℹ Please use tidy evaluation idioms with `aes()`.
#> ℹ See also `vignette("ggplot2-in-packages")` for more information.
#> ℹ The deprecated feature was likely used in the rstanarm package.
#>   Please report the issue at <https://github.com/stan-dev/rstanarm/issues>.
#> This warning is displayed once per session.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: The `size` argument of `grid_lines()` is deprecated as of bayesplot 1.16.0.
#> ℹ Please use the `linewidth` argument instead.
#> ℹ The deprecated feature was likely used in the rstanarm package.
#>   Please report the issue at <https://github.com/stan-dev/rstanarm/issues>.
#> This warning is displayed once per session.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

#' ![](steep-carp_reprex_files/figure-gfm/unnamed-chunk-2-1.png)<!-- -->

posterior_vs_prior(fit_stan_lmer1, regex_pars = "sigma")  # Error dimensions
#> 
#> Drawing from prior...
#> Error in `s[, labs, drop = FALSE]`:
#> ! incorrect number of dimensions
posterior_vs_prior(fit_stan_lmer1, regex_pars = "sigm")   # Error dimensions
#> 
#> Drawing from prior...
#> Error in `s[, labs, drop = FALSE]`:
#> ! incorrect number of dimensions
posterior_vs_prior(fit_stan_lmer1, regex_pars = "igma")   # ok (matches 3)
#> 
#> Drawing from prior...

#' ![](steep-carp_reprex_files/figure-gfm/unnamed-chunk-2-2.png)<!-- -->

posterior_vs_prior(fit_stan_lmer1, regex_pars = "Sigma")  # ok (matches 2)
#> 
#> Drawing from prior...

#' ![](steep-carp_reprex_files/figure-gfm/unnamed-chunk-2-3.png)<!-- -->


# fit_stan_lmer2, fitting only batch without cask
names_fit_stan_lmer2 <- names(as.data.frame(fit_stan_lmer2))
grep("b\\[", names_fit_stan_lmer2, value = TRUE, invert = TRUE)
#> [1] "(Intercept)"                         
#> [2] "sigma"                               
#> [3] "Sigma[batch:(Intercept),(Intercept)]"

posterior_vs_prior(fit_stan_lmer2, regex_pars = "Sigma")  # Error dimensions
#> 
#> Drawing from prior...
#> Error in `s[, labs, drop = FALSE]`:
#> ! incorrect number of dimensions

# check fit_stan_lmer3, fitting only cask without batch
posterior_vs_prior(fit_stan_lmer3, regex_pars = "Sigma")  # Error dimensions
#> 
#> Drawing from prior...
#> Error in `s[, labs, drop = FALSE]`:
#> ! incorrect number of dimensions

# Is the reason (regex_pars = "Sigma") works for fit1 but not fit2 because fit2 has only 
# one match
#   "Sigma[batch:(Intercept),(Intercept)]"
# whereas fit1 had two matches?
#   "Sigma[cask:batch:(Intercept),(Intercept)]"
#   "Sigma[batch:(Intercept),(Intercept)]"
#
# same for fit3

#eof

Activity

  1. jgabry commented on Oct 13, 2026

    @jgabry
    Member

    Thanks for pointing this out, this is indeed a bug. It should be fixed by #663. Until the fix is released, I think you can workaround this bug by using pars (instead of regex_pars) for single exact matches, e.g. pars = "sigma" or pars = "Sigma[batch:(Intercept),(Intercept)]".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions