hsbadr / bayesian

Bindings for Bayesian TidyModels

Home Page:https://hsbadr.github.io/bayesian/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question about functions/aterms in formula

evesch opened this issue · comments

Hi Hamada,

Do functions/aterms in a formula work in this version? I get an error, see reprex below.
I just started playing with this package today so maybe I'm missing something.

Thanks, Eric

library(bayesian)
#> Loading required package: brms
#> Loading required package: Rcpp
#> Loading 'brms' package (version 2.14.4). Useful instructions
#> can be found by typing help('brms'). A more detailed introduction
#> to the package is available through vignette('brms_overview').
#> 
#> Attaching package: 'brms'
#> The following object is masked from 'package:stats':
#> 
#>     ar
#> Loading required package: parsnip

schools_dat <- data.frame(J = seq(1,8,1), 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

mod <- brm(y|se(sigma) ~  1 + (1 |J), 
           data = schools_dat)
#> Compiling Stan program...
#> Start sampling
#> 
#> SAMPLING FOR MODEL 'aa3fde0a9305834e2e72ea8f987cc227' NOW (CHAIN 1).
#> removed output....
#> Chain 4:  Elapsed Time: 0.049574 seconds (Warm-up)
#> Chain 4:                0.038749 seconds (Sampling)
#> Chain 4:                0.088323 seconds (Total)
#> Chain 4:

tmod <-
  bayesian() %>%
  set_engine("brms") %>%
  fit(
    y|se(sigma) ~  1 + (1 |J),
    data = schools_dat
  )
#> Error in se(sigma): could not find function "se"

Hi Eric,

Do functions/aterms in a formula work in this version? I get an error, see reprex below.

Yes. We need to add more examples and tests.

#> Error in se(sigma): could not find function "se"

There're multiple workarounds for this, but the best solution is use formula.override from the development version:

remotes::install_github("hsbadr/bayesian")

You can then fit the model using:

tmod <-
  bayesian() %>%
  set_engine(
      "brms",
      formula.override = bayesian_formula(
          y | se(sigma) ~  1 + (1 | J)
      )
  ) %>%
  fit(
    y ~ .,
    data = schools_dat
  )

Alternatively, you may define a dummy function to pass the formula checks to brms:

se <- function(sigma){}

but I do prefer the general solution with formula.override.

I'll close this issue, but feel free to reopen if needed.

Thanks!