-
Notifications
You must be signed in to change notification settings - Fork 10
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
Integration with brms #12
Comments
I'm not sure about the other error you're getting but for this one I think you can solve it just by using |
You bring up good points, @odaniel1. And thanks for the Bayesian workflow paper, super helpful and relevant! @jgabry, if there is anything I can do to help, please let me know.
Thanks @jgabry! I confirmed that
I went back and forth on this one early in development, but I ultimately decided But as you saw, that means when you try to use targets::tar_visnetwork() So the problem is:
Possible alternatives:
I propose we go with (3). This is how I am thinking about the targetopia anyway: one targetopia package for each methodology package. For example, I am just starting to develop a new @odaniel1 or @jgabry, is |
And just to be clear, if we go with (3), you could use |
Thanks @wlandau and @jgabry for your thoughts - and really insightful to understand the logic behind requiring I've copied below a reprex with a hack that serves as a workaround to both issues (with thanks to @jgabry for the unclass solution). Its not particularly elegant - but should work for me, and hopefully others, as a stop-gap. Re: a
Created on 2020-12-18 by the reprex package (v0.3.0)
|
Your workaround looks pretty good. The only issue I see at a glance is the tar_target(
stan_model,
make_stancode(
formula = stan_formula,
data = data,
save_model = stan_model_path
)
)
# _targets.R
library(targets)
library(stantargets)
library(targets)
library(fs)
stan_model_path <- fs::file_create("x.stan")
make_standata_target <- function(stan_model, ...){
if(!("brmsmodel" %in% class(stan_model)))
stop("Expecting an object of class brmsmodel in stan_model argument")
standata <- make_standata(...)
return(unclass(standata))
}
options(crayon.enabled = FALSE)
tar_option_set(
packages = c("tibble", "brms"),
memory = "transient",
garbage_collection = TRUE
)
tar_pipeline(
# New target to track the template model so the brms model stays up to date..
tar_target(
stan_model_path_target,
stan_model_path,
format = "file"
),
tar_target(
data,
tibble(x = rnorm(10), y= 0.5 * rnorm(10))
),
tar_target(
stan_formula,
brmsformula(formula = y ~ 1 + x)
),
tar_target(
stan_model,
make_stancode(
formula = stan_formula,
data = data,
save_model = stan_model_path_target
)
),
tar_target(
stan_data,
make_standata_target(
stan_model,
formula = stan_formula,
data = data
)
),
tar_stan_mcmc(
example,
stan_files = stan_model_path,
data = stan_data,
refresh = 0,
init = 1,
show_messages = FALSE
)
) |
Your workaround looks pretty good. The only issue I see at a glance is the tar_target(
stan_model,
make_stancode(
formula = stan_formula,
data = data,
save_model = stan_model_path
)
)
# _targets.R
library(targets)
library(stantargets)
library(targets)
library(fs)
stan_model_path <- fs::file_create("x.stan")
make_standata_target <- function(stan_model, ...){
if(!("brmsmodel" %in% class(stan_model)))
stop("Expecting an object of class brmsmodel in stan_model argument")
standata <- make_standata(...)
return(unclass(standata))
}
options(crayon.enabled = FALSE)
tar_option_set(
packages = c("tibble", "brms"),
memory = "transient",
garbage_collection = TRUE
)
tar_pipeline(
# New target to track the template model so the brms model stays up to date.
tar_target(
stan_model_path_target,
stan_model_path,
format = "file"
),
tar_target(
data,
tibble(x = rnorm(10), y= 0.5 * rnorm(10))
),
tar_target(
stan_formula,
brmsformula(formula = y ~ 1 + x)
),
tar_target(
stan_model,
make_stancode(
formula = stan_formula,
data = data,
save_model = stan_model_path_target
)
),
tar_target(
stan_data,
make_standata_target(
stan_model,
formula = stan_formula,
data = data
)
),
tar_stan_mcmc(
example,
stan_files = stan_model_path,
data = stan_data,
refresh = 0,
init = 1,
show_messages = FALSE
)
) |
And of course, no pressure to take on |
I'm certainly not opposed to a separate P.S. @wlandau these diagrams are really cool! |
I figured as much, you are already everywhere in the Stan dev space. I expect to create
Thanks! If you like |
I mentioned |
I am happy to hear that you consider adding brms to your list of supported workflows! If there is anything you want or need my input on, please let me know. I am also happy to have a call with those interested in building brmstargets if you think that would help you. |
Fantastic, Paul, thank you for your support! I will let you know when I have a chance to start working on |
Prework
Description
Hi Will - I've been a user of
drake
for half a year now, and am just starting to try outtargets
so was excited to see your post on the Stan discourse aboutstantargets
.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 integratingbrms
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 classstandata
which are returned by calls tobrms::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
Created on 2020-12-17 by the reprex package (v0.3.0)
The text was updated successfully, but these errors were encountered: