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...
## Trying to compile a simple C file
## Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
## using C compiler: ‘Apple clang version 15.0.0 (clang-1500.3.9.4)’
## using SDK: ‘’
## clang -arch x86_64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/4.2/Resources/library/Rcpp/include/" -I"/Library/Frameworks/R.framework/Versions/4.2/Resources/miworkspace-library/RcppEigen/include/" -I"/Library/Frameworks/R.framework/Versions/4.2/Resources/miworkspace-library/RcppEigen/include/unsupported" -I"/Library/Frameworks/R.framework/Versions/4.2/Resources/miworkspace-library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.2/Resources/library/StanHeaders/include/src/" -I"/Library/Frameworks/R.framework/Versions/4.2/Resources/library/StanHeaders/include/" -I"/Library/Frameworks/R.framework/Versions/4.2/Resources/library/RcppParallel/include/" -I"/Library/Frameworks/R.framework/Versions/4.2/Resources/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include '/Library/Frameworks/R.framework/Versions/4.2/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I/opt/R/x86_64/include -fPIC -falign-functions=64 -Wall -g -O2 -c foo.c -o foo.o
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.2/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:
## In file included from /Library/Frameworks/R.framework/Versions/4.2/Resources/miworkspace-library/RcppEigen/include/Eigen/Dense:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.2/Resources/miworkspace-library/RcppEigen/include/Eigen/Core:88:
## /Library/Frameworks/R.framework/Versions/4.2/Resources/miworkspace-library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:1: error: unknown type name 'namespace'
## namespace Eigen {
## ^
## /Library/Frameworks/R.framework/Versions/4.2/Resources/miworkspace-library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:16: error: expected ';' after top level declarator
## namespace Eigen {
## ^
## ;
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.2/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:
## In file included from /Library/Frameworks/R.framework/Versions/4.2/Resources/miworkspace-library/RcppEigen/include/Eigen/Dense:1:
## /Library/Frameworks/R.framework/Versions/4.2/Resources/miworkspace-library/RcppEigen/include/Eigen/Core:96:10: fatal error: 'complex' file not found
## #include <complex>
## ^~~~~~~~~
## 3 errors generated.
## make[1]: *** [foo.o] Error 1
## 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.