From d1988538e0783a8edccf42d3e510fb5df71ad851 Mon Sep 17 00:00:00 2001 From: GidonFrischkorn Date: Wed, 21 Feb 2024 10:13:24 +0100 Subject: [PATCH 1/4] Import `save_pars` from `brms` --- NAMESPACE | 1 + R/bmm-package.R | 1 + R/utils.R | 2 -- 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 84eb276d..659e1dc2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -76,4 +76,5 @@ export(softmaxinv) export(supported_models) export(use_model_template) export(wrap) +importFrom(brms,save_pars) importFrom(magrittr,"%>%") diff --git a/R/bmm-package.R b/R/bmm-package.R index a65cf643..3fd6905c 100644 --- a/R/bmm-package.R +++ b/R/bmm-package.R @@ -2,5 +2,6 @@ "_PACKAGE" ## usethis namespace: start +#' @importFrom brms save_pars ## usethis namespace: end NULL diff --git a/R/utils.R b/R/utils.R index 04248f69..a01a8b17 100644 --- a/R/utils.R +++ b/R/utils.R @@ -299,5 +299,3 @@ order_data_query <- function(model, data, formula) { } data } - - From ff48f0f99d645804f7d224c6ae0fa34f980e81d8 Mon Sep 17 00:00:00 2001 From: GidonFrischkorn Date: Wed, 21 Feb 2024 11:40:33 +0100 Subject: [PATCH 2/4] Add `save_pars_bmm2brms` --- NAMESPACE | 1 + R/utils.R | 23 +++++++++++++++++++++++ man/save_pars_bmm2brms.Rd | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 man/save_pars_bmm2brms.Rd diff --git a/NAMESPACE b/NAMESPACE index 659e1dc2..20473677 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -69,6 +69,7 @@ export(rad2deg) export(rmixture2p) export(rmixture3p) export(rsdm) +export(save_pars_bmm2brms) export(sdmSimple) export(set_default_prior) export(softmax) diff --git a/R/utils.R b/R/utils.R index a01a8b17..313dfa34 100644 --- a/R/utils.R +++ b/R/utils.R @@ -299,3 +299,26 @@ order_data_query <- function(model, data, formula) { } data } + +#' @title Pass options to save parameters from `bmm` t0 `brms` +#' @description +#' When calling `fit_model` with additional information to save parameters you can use this +#' function to pass information about saving parameter draws to `brms` without having to load `brms`. +#' Alternatively, you can also load `brms` and call `save_pars`. For details see ?brms::save_pars(). +#' @param group logical. Indicates if group-level parameter for each level of the grouping variable should be saved. +#' Default is `TRUE`. +#' @param all logical. Indicates if draws from all variables defined in the STAN parameter block +#' should be saved. Default is `FALSE`. If you want to use methods such as `bridge_sampler` or +#' `bayes_factor` you need to set this to `TRUE`. +#' @param manual character vector. Vector naming the STAN variables for which draws should be saved. +#' These names need to match the names inside the STAN code before renaming. You can access these +#' by having a lot at the STAN code using the `get_stancode` or `get_stancode_parblock`. +#' @return A list of `brms` class "save_pars. +#' @export +#' @examples +#' # example code +#' +save_pars_bmm2brms <- function(group = TRUE, all = FALSE, manual = NULL) { + save_pars <- brms::save_pars(group, all, manual) + save_pars +} diff --git a/man/save_pars_bmm2brms.Rd b/man/save_pars_bmm2brms.Rd new file mode 100644 index 00000000..6f1e2ec2 --- /dev/null +++ b/man/save_pars_bmm2brms.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{save_pars_bmm2brms} +\alias{save_pars_bmm2brms} +\title{Pass options to save parameters from \code{bmm} t0 \code{brms}} +\usage{ +save_pars_bmm2brms(group = TRUE, all = FALSE, manual = NULL) +} +\arguments{ +\item{group}{logical. Indicates if group-level parameter for each level of the grouping variable should be saved. +Default is \code{TRUE}.} + +\item{all}{logical. Indicates if draws from all variables defined in the STAN parameter block +should be saved. Default is \code{FALSE}. If you want to use methods such as \code{bridge_sampler} or +\code{bayes_factor} you need to set this to \code{TRUE}.} + +\item{manual}{character vector. Vector naming the STAN variables for which draws should be saved. +These names need to match the names inside the STAN code before renaming. You can access these +by having a lot at the STAN code using the \code{get_stancode} or \code{get_stancode_parblock}.} +} +\value{ +A list of \code{brms} class "save_pars. +} +\description{ +When calling \code{fit_model} with additional information to save parameters you can use this +function to pass information about saving parameter draws to \code{brms} without having to load \code{brms}. +Alternatively, you can also load \code{brms} and call \code{save_pars}. For details see ?brms::save_pars(). +} +\examples{ +# example code + +} From 98a37e8b82a2419d9c99f71ffb9561e8636ea1a8 Mon Sep 17 00:00:00 2001 From: GidonFrischkorn Date: Wed, 21 Feb 2024 11:48:10 +0100 Subject: [PATCH 3/4] Add tests for `save_pars_bmm2brms` --- R/utils.R | 2 +- tests/testthat/test-utils.R | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 313dfa34..699e0566 100644 --- a/R/utils.R +++ b/R/utils.R @@ -319,6 +319,6 @@ order_data_query <- function(model, data, formula) { #' # example code #' save_pars_bmm2brms <- function(group = TRUE, all = FALSE, manual = NULL) { - save_pars <- brms::save_pars(group, all, manual) + save_pars <- brms::save_pars(group = group, all = all, manual = manual) save_pars } diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 9b5de0be..70144e87 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -16,3 +16,10 @@ test_that("empty dots don't crash the function", { out <- combine_args(nlist(config_args)) expect_equal(out, list(formula = 'a', family = 'b', prior = 'c', data = 'd', stanvars = 'e', init = 1)) }) + +test_that("save_pars_bmm2brm works as expected", { + expect_equal(brms::save_pars(all = TRUE), save_pars_bmm2brms(all = TRUE)) + expect_error(save_pars_bmm2brms(all = "all")) + expect_error(save_pars_bmm2brms(latent = TRUE)) +}) + From 0177d477dc8877d06ad5a083b3783f7c09f75479 Mon Sep 17 00:00:00 2001 From: venpopov Date: Wed, 21 Feb 2024 15:43:08 +0100 Subject: [PATCH 4/4] simplify wrapper around save_pars() --- NAMESPACE | 2 +- R/utils.R | 28 ++++++------------------ man/save_pars.Rd | 43 +++++++++++++++++++++++++++++++++++++ man/save_pars_bmm2brms.Rd | 32 --------------------------- tests/testthat/test-utils.R | 7 ------ 5 files changed, 51 insertions(+), 61 deletions(-) create mode 100644 man/save_pars.Rd delete mode 100644 man/save_pars_bmm2brms.Rd diff --git a/NAMESPACE b/NAMESPACE index 20473677..4c4b7e17 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -69,7 +69,7 @@ export(rad2deg) export(rmixture2p) export(rmixture3p) export(rsdm) -export(save_pars_bmm2brms) +export(save_pars) export(sdmSimple) export(set_default_prior) export(softmax) diff --git a/R/utils.R b/R/utils.R index 699e0566..0d15cf4e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -300,25 +300,11 @@ order_data_query <- function(model, data, formula) { data } -#' @title Pass options to save parameters from `bmm` t0 `brms` -#' @description -#' When calling `fit_model` with additional information to save parameters you can use this -#' function to pass information about saving parameter draws to `brms` without having to load `brms`. -#' Alternatively, you can also load `brms` and call `save_pars`. For details see ?brms::save_pars(). -#' @param group logical. Indicates if group-level parameter for each level of the grouping variable should be saved. -#' Default is `TRUE`. -#' @param all logical. Indicates if draws from all variables defined in the STAN parameter block -#' should be saved. Default is `FALSE`. If you want to use methods such as `bridge_sampler` or -#' `bayes_factor` you need to set this to `TRUE`. -#' @param manual character vector. Vector naming the STAN variables for which draws should be saved. -#' These names need to match the names inside the STAN code before renaming. You can access these -#' by having a lot at the STAN code using the `get_stancode` or `get_stancode_parblock`. -#' @return A list of `brms` class "save_pars. +#' @inherit brms::save_pars title params return +#' @description Thin wrapper around [brms::save_pars()]. When calling +#' [fit_model] with additional information to save parameters you can use this +#' function to pass information about saving parameter draws to `brms` without +#' having to load `brms`. Alternatively, you can also load `brms` and call +#' `save_pars`. For details see ?brms::save_pars. #' @export -#' @examples -#' # example code -#' -save_pars_bmm2brms <- function(group = TRUE, all = FALSE, manual = NULL) { - save_pars <- brms::save_pars(group = group, all = all, manual = manual) - save_pars -} +save_pars <- brms::save_pars diff --git a/man/save_pars.Rd b/man/save_pars.Rd new file mode 100644 index 00000000..2febb2da --- /dev/null +++ b/man/save_pars.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{save_pars} +\alias{save_pars} +\title{Control Saving of Parameter Draws} +\usage{ +save_pars(group = TRUE, latent = FALSE, all = FALSE, manual = NULL) +} +\arguments{ +\item{group}{A flag to indicate if group-level coefficients for +each level of the grouping factors should be saved (default is +\code{TRUE}). Set to \code{FALSE} to save memory. Alternatively, +\code{group} may also be a character vector naming the grouping factors +for which to save draws of coefficients.} + +\item{latent}{A flag to indicate if draws of latent variables obtained by +using \code{me} and \code{mi} terms should be saved (default is +\code{FALSE}). Saving these draws allows to better use methods such as +\code{posterior_predict} with the latent variables but leads to very large +\R objects even for models of moderate size and complexity. Alternatively, +\code{latent} may also be a character vector naming the latent variables +for which to save draws.} + +\item{all}{A flag to indicate if draws of all variables defined in Stan's +\code{parameters} block should be saved (default is \code{FALSE}). Saving +these draws is required in order to apply the certain methods such as +\code{bridge_sampler} and \code{bayes_factor}.} + +\item{manual}{A character vector naming Stan variable names which should be +saved. These names should match the variable names inside the Stan code +before renaming. This feature is meant for power users only and will rarely +be useful outside of very special cases.} +} +\value{ +A list of class \code{"save_pars"}. +} +\description{ +Thin wrapper around \code{\link[brms:save_pars]{brms::save_pars()}}. When calling +\link{fit_model} with additional information to save parameters you can use this +function to pass information about saving parameter draws to \code{brms} without +having to load \code{brms}. Alternatively, you can also load \code{brms} and call +\code{save_pars}. For details see ?brms::save_pars. +} diff --git a/man/save_pars_bmm2brms.Rd b/man/save_pars_bmm2brms.Rd deleted file mode 100644 index 6f1e2ec2..00000000 --- a/man/save_pars_bmm2brms.Rd +++ /dev/null @@ -1,32 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R -\name{save_pars_bmm2brms} -\alias{save_pars_bmm2brms} -\title{Pass options to save parameters from \code{bmm} t0 \code{brms}} -\usage{ -save_pars_bmm2brms(group = TRUE, all = FALSE, manual = NULL) -} -\arguments{ -\item{group}{logical. Indicates if group-level parameter for each level of the grouping variable should be saved. -Default is \code{TRUE}.} - -\item{all}{logical. Indicates if draws from all variables defined in the STAN parameter block -should be saved. Default is \code{FALSE}. If you want to use methods such as \code{bridge_sampler} or -\code{bayes_factor} you need to set this to \code{TRUE}.} - -\item{manual}{character vector. Vector naming the STAN variables for which draws should be saved. -These names need to match the names inside the STAN code before renaming. You can access these -by having a lot at the STAN code using the \code{get_stancode} or \code{get_stancode_parblock}.} -} -\value{ -A list of \code{brms} class "save_pars. -} -\description{ -When calling \code{fit_model} with additional information to save parameters you can use this -function to pass information about saving parameter draws to \code{brms} without having to load \code{brms}. -Alternatively, you can also load \code{brms} and call \code{save_pars}. For details see ?brms::save_pars(). -} -\examples{ -# example code - -} diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 70144e87..9b5de0be 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -16,10 +16,3 @@ test_that("empty dots don't crash the function", { out <- combine_args(nlist(config_args)) expect_equal(out, list(formula = 'a', family = 'b', prior = 'c', data = 'd', stanvars = 'e', init = 1)) }) - -test_that("save_pars_bmm2brm works as expected", { - expect_equal(brms::save_pars(all = TRUE), save_pars_bmm2brms(all = TRUE)) - expect_error(save_pars_bmm2brms(all = "all")) - expect_error(save_pars_bmm2brms(latent = TRUE)) -}) -