Implement: BayesPharma and Tidymodels
Matthew O’Meara
implement_tidymodels.Rmd
## here() starts at /Users/maom/opt/bayespharma
## Error in library(tidymodels): there is no package called 'tidymodels'
## Loading required package: brms
## Loading required package: Rcpp
## Loading 'brms' package (version 2.21.0). 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:bayesplot':
##
## rhat
## The following object is masked from 'package:stats':
##
## ar
## Loading required package: parsnip
Using BayesPharma models with Tidymodels
Tidymodels is a platform for running and analyzing models in R. This vignette demonstrates how to use the BayesPharma models with Tidy Models.
load(here::here("data", "kor_antag.rda"))
kor_antag <- kor_antag |> dplyr::transmute(
substance_id = Drug,
cell_id = cell_number,
log_dose = log_dose,
response = normalized_measurement)
Define a recipe and assign roles to different columns
kor_recipe <- kor_antag |>
recipes::recipe() |>
recipes::update_role(response, new_role = "outcome") |>
recipes::update_role(log_dose, new_role = "predictor") |>
recipes::update_role(substance_id, new_role = "predictor")
Define the model using the wrapper package for Tidymodels.
kor_model <- bayesian::bayesian(
cores = 4,
chains = 4,
iter = 2000,
control = list(adapt_delta = 0.99),
stanvars = BayesPharma::sigmoid_stanvar(),
init = BayesPharma::sigmoid_antagonist_init(top = 100),
prior = BayesPharma::sigmoid_antagonist_prior(top = 100)) |>
parsnip::set_engine("brms") |>
parsnip::set_mode("regression") |>
recipes::update(
formula.override = bayesian::bayesian_formula(
..y ~ sigmoid(ic50, hill, top, bottom, log_dose),
ic50 + hill + top + bottom ~ 0 + substance_id,
nl = TRUE))
Build and run the workflow by adding the recipe and model and then running fit.
kor_workflow <- workflows::workflow() |>
workflows::add_recipe(kor_recipe) |>
workflows::add_model(spec = kor_model) |>
parsnip::fit(data = kor_antag)
## Compiling Stan program...
## Start sampling
## Warning in .local(object, ...): some chains had errors; consider specifying
## chains = 1 to debug
## here are whatever error messages were returned
## [[1]]
## Stan model 'anon_model' does not contain samples.
##
## [[2]]
## Stan model 'anon_model' does not contain samples.
##
## [[3]]
## Stan model 'anon_model' does not contain samples.
##
## [[4]]
## Stan model 'anon_model' does not contain samples.
kor_workflow
## ══ Workflow [trained] ════════════════════════════════════════════════════════════════════════════════════════════════
## Preprocessor: Recipe
## Model: bayesian()
##
## ── Preprocessor ──────────────────────────────────────────────────────────────────────────────────────────────────────
## 0 Recipe Steps
##
## ── Model ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Family: gaussian
## Links: mu = identity; sigma = identity
## Formula: ..y ~ sigmoid(ic50, hill, top, bottom, log_dose)
## ic50 ~ 0 + substance_id
## hill ~ 0 + substance_id
## top ~ 0 + substance_id
## bottom ~ 0 + substance_id
## Data: ~data (Number of observations: 73)
##
## The model does not contain posterior draws.