Skip to content

Integration with brms #12

@odaniel1

Description

@odaniel1

Prework

  • Read and agree to the code of conduct and contributing guidelines.
  • If there is already a relevant issue, whether open or closed, comment on the existing thread instead of posting a new issue.
  • Post a minimal reproducible example so the maintainer can troubleshoot the problems you identify. A reproducible example is:
    • Runnable: post enough R code and data so any onlooker can create the error on their own computer.
    • Minimal: reduce runtime wherever possible and remove complicated details that are irrelevant to the issue at hand.
    • Readable: format your code according to the tidyverse style guide.

Description

Hi Will - I've been a user of drake for half a year now, and am just starting to try out targets so was excited to see your post on the Stan discourse about stantargets.

I'm testing stantargets as a part of a Bayesian workflow [Gelman et al. 2020] - for which I was interested in the model specification being a part of the pipeline: specifically integrating brms ability to convert a formula into stan code (brms::make_stancode) and to appropriately shape the data (brms::make_standata).

Intuitively this seemed like it should fit well with the the targetopia/pipeline framework - but my attempt at an implementation had two conflicts with tar_stan_mcmc:

  • The stan_files argument does not appear to accept a target as an argument - in the implementation below I'm defining this file path as a target so that it is shared between defining the model (brms::make_stancode) and running the model (tar_stan_mcmc).

  • The data argument does not appear to support arguments of the class standata which are returned by calls to brms::make_standata.

I've indicated my current workarounds in the comments - I'd welcome your views as to their optimality, and any indications as to whether it'd be possible to support this type of workflow more intuitively, or alternatively views as to why this isn't desired!

Reproducible example

library(targets)

tar_script({
  library(targets)
  library(stantargets)
  
  options(crayon.enabled = FALSE)
  tar_option_set(
    packages = c("tibble", "brms"),
    memory = "transient",
    garbage_collection = TRUE
  )
  
  tar_pipeline(
    tar_target(
      data,
      tibble(x = rnorm(10), y=  0.5 * rnorm(10))
    ),
    tar_target(
      stan_model_path,
      "x.stan",
      format = "file"
    ),
    tar_target(
      stan_formula,
      brmsformula(formula = y ~ x)
    ),
    tar_target(
      stan_model,
      make_stancode(
        formula = stan_formula,
        data = data,
        save_model = stan_model_path)
    ),
    tar_target(
      stan_data,
      make_standata(
        formula = stan_formula,
        data = data
      )
    ),
    tar_stan_mcmc(
      example,
      # this fails - explicitly providing the path x.stan works, so long as the
      # stan_model target has run. But this incurs duplication/implicit dependency,
      # and  breaks the pipeline workflow.
      stan_files = stan_model_path,
      # this fails - the data argument expects the object returned to be of
      # type list(), whereas brms::make_standata returns type standata
      data = stan_data,
      refresh = 0,
      init = 1,
      show_messages = FALSE,
      chains = 4,
      parallel_chains = 4,
      iter_warmup = 200,
      iter_sampling = 400
    )
  )
})

Created on 2020-12-17 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions