Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Variable name generation within brms/get_prior() differs between base R and RMarkdown. #1737

Open
Temperche opened this issue Feb 4, 2025 · 3 comments

Comments

@Temperche
Copy link

Temperche commented Feb 4, 2025

Hi everyone,

I'd like to highlight a bug that I've encountered in brms. Different variable names are generated for brms models as shown by the get_priors() function. Compare e.g. "stimulus.stateT.B" vs. "stimulus.stateB" in base R vs RMarkdown. This makes consistent prior name specification impossible.

2025-02-04-sample-data.csv

Example:

Base R:


library(brms)

Dataset <- read.csv("2025-02-04-sample-data.csv")

get_prior(behavior_time_s~stimulus.type*stimulus.state+weight_g+(1|family), data=Dataset)
                    prior     class                               coef  group resp dpar nlpar lb ub       source
                   (flat)         b                                                                      default
                   (flat)         b                  stimulus.stateT.B                              (vectorized)
                   (flat)         b                   stimulus.typeT.D                              (vectorized)
                   (flat)         b stimulus.typeT.D:stimulus.stateT.B                              (vectorized)
                   (flat)         b                           weight_g                              (vectorized)
 student_t(3, 398, 379.5) Intercept                                                                      default
   student_t(3, 0, 379.5)        sd                                                            0         default
   student_t(3, 0, 379.5)        sd                                    family                  0    (vectorized)
   student_t(3, 0, 379.5)        sd                          Intercept family                  0    (vectorized)
   student_t(3, 0, 379.5)     sigma                                                            0         default

Knitted RMarkdown:

library(brms)

Dataset <- read.csv("2025-02-04-sample-data.csv")
get_prior(behavior_time_s~stimulus.type*stimulus.state+weight_g+(1|family), data=Dataset)

##                     prior     class                           coef  group resp
##                    (flat)         b                                           
##                    (flat)         b                stimulus.stateB            
##                    (flat)         b                 stimulus.typeD            
##                    (flat)         b stimulus.typeD:stimulus.stateB            
##                    (flat)         b                       weight_g            
##  student_t(3, 398, 379.5) Intercept                                           
##    student_t(3, 0, 379.5)        sd                                           
##    student_t(3, 0, 379.5)        sd                                family     
##    student_t(3, 0, 379.5)        sd                      Intercept family     
##    student_t(3, 0, 379.5)     sigma                                           
##  dpar nlpar lb ub       source
##                        default
##                   (vectorized)
##                   (vectorized)
##                   (vectorized)
##                   (vectorized)
##                        default
##              0         default
##              0    (vectorized)
##              0    (vectorized)
##              0         default

Thanks for your time and effort!
Temperche

@lunafazio
Copy link
Contributor

Trying your example for "base R" in my R terminal produces output that matches what the RMarkdown block shows, so your bug cannot be reproduced. I don't see any reason for brms to be adding letters that don't exist in the variable name based on where it's running either.

RMarkdown has a cache setting that you can control in the options header and that is far more likely to be the source of any discrepancies between what you run on your terminal and your knitted output. In general, I would recommend deleting the cache folder, turning off that option and manually handling any caching needs you may have for long-running models and the such.

@wds15
Copy link
Contributor

wds15 commented Feb 4, 2025

This looks to me as if there are different defaults for the contrasts at work here. I'd check the contrast setting for factors and ensure that these are consistent.

@Temperche
Copy link
Author

Temperche commented Feb 4, 2025

Thanks for the heads-up. I now think that it might be RCommander, which is a popular R GUI, that is at fault here rather than base R.

library(Rcmdr)

Is the bug reproducible for you if you use R Commander?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants