Skip to content
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

Remove deprecated functions; hard dependency on brms 2.21.0 #174

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .dev/bmm_default_priors.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ All model parameters are `nlpar` so they get class `b` with coef `Intercept`
```{r}
model <- mixture3p('dev_rad', nt_features = paste0('col_nt',1:7), setsize='set_size')
formula <- bmf(kappa ~ 1, thetat ~ 1, thetant ~ 1)
get_model_prior(formula, dat, model)
default_prior(formula, dat, model)
```

```{r}
Expand All @@ -102,7 +102,7 @@ For kappa, which we include and intercept and a predictor, we get class `b` with

```{r}
formula <- bmf(kappa ~ session, thetat ~ 1, thetant ~ 1)
get_model_prior(formula, dat, model)
default_prior(formula, dat, model)
```

```{r}
Expand All @@ -115,7 +115,7 @@ For kappa, which we include and intercept and a predictor, we get class `b` with

```{r}
formula <- bmf(kappa ~ 0+session, thetat ~ 1, thetant ~ 1)
get_model_prior(formula, dat, model)
default_prior(formula, dat, model)
```

```{r}
Expand All @@ -126,7 +126,7 @@ terms(formula$kappa)

```{r}
formula <- bmf(kappa ~ 0+session + ( 0+session|ID), thetat ~ 1, thetant ~ 1)
get_model_prior(formula, dat, model)
default_prior(formula, dat, model)
```

```{r}
Expand All @@ -138,7 +138,7 @@ terms(formula$kappa)
```{r}

formula <- bmf(kappa ~ session + cond, thetat ~ 1, thetant ~ 1)
get_model_prior(formula, dat, model)
default_prior(formula, dat, model)
```

```{r}
Expand All @@ -154,7 +154,7 @@ What we can do, is for each predictor, check how many levels appear.
```{r}

formula <- bmf(kappa ~ 0 + session + cond, thetat ~ 1, thetant ~ 1)
get_model_prior(formula, dat, model)
default_prior(formula, dat, model)
```

```{r}
Expand All @@ -166,7 +166,7 @@ terms(formula$kappa)
```{r}

formula <- bmf(kappa ~ 0 + session * cond, thetat ~ 1, thetant ~ 1)
get_model_prior(formula, dat, model)
default_prior(formula, dat, model)
```

```{r}
Expand All @@ -178,7 +178,7 @@ terms(formula$kappa)
```{r}

formula <- bmf(kappa ~ 0 + session:cond + cond + session, thetat ~ 1, thetant ~ 1)
get_model_prior(formula, dat, model)
default_prior(formula, dat, model)
```

```{r}
Expand All @@ -190,7 +190,7 @@ terms(formula$kappa)
```{r}

formula <- bmf(kappa ~ 0 + session:cond, thetat ~ 1, thetant ~ 1)
get_model_prior(formula, dat, model)
default_prior(formula, dat, model)
```

```{r}
Expand Down Expand Up @@ -235,11 +235,11 @@ A helper function to compare the priors with and without the default priors.

```{r}
compare_priors <- function(formula,dat,model) {
p1 <- withr::with_options(list('bmm.default_priors'=FALSE), get_model_prior(formula, dat, model)) %>%
p1 <- withr::with_options(list('bmm.default_priors'=FALSE), default_prior(formula, dat, model)) %>%
brms:::prepare_print_prior() %>%
rename(priorBRMS=prior) %>%
select(priorBRMS,class,coef,group,resp,dpar,nlpar)
p2 <- withr::with_options(list('bmm.default_priors'=TRUE), get_model_prior(formula, dat, model)) %>%
p2 <- withr::with_options(list('bmm.default_priors'=TRUE), default_prior(formula, dat, model)) %>%
brms:::prepare_print_prior() %>%
rename(priorBMM=prior) %>%
select(priorBMM,class,coef,group,resp,dpar,nlpar)
Expand Down
2 changes: 1 addition & 1 deletion .dev/visualizing_priors.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ data <- OberauerLin_2017
data$session <- as.factor(data$session)
formula <- bmf(c ~ 0 + set_size, kappa ~ session)
model <- sdmSimple('dev_rad')
get_model_prior(formula, data, model)
default_prior(formula, data, model)


x <- exp(rnorm(1000000,3,1.75))
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: bmm
Title: Easy and Accesible Bayesian Measurement Models using 'brms'
Version: 0.4.6.9000
Version: 0.4.7.9000
Authors@R: c(
person("Vencislav", "Popov", , "[email protected]", role = c("aut", "cre", "cph")),
person("Gidon", "Frischkorn", , "[email protected]", role = c("aut", "cph")),
Expand Down Expand Up @@ -53,5 +53,5 @@ Additional_repositories:
VignetteBuilder: knitr
Depends:
R (>= 3.6.0),
brms
brms (>= 2.21.0)
LazyData: true
9 changes: 4 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ S3method(configure_prior,IMMfull)
S3method(configure_prior,bmmmodel)
S3method(configure_prior,default)
S3method(configure_prior,mixture3p)
S3method(default_prior,bmmformula)
S3method(fit_info,brmsfit)
S3method(fit_info,brmsfit_list)
S3method(identical,bmmformula)
Expand All @@ -52,6 +53,7 @@ S3method(reset_env,brmsfamily)
S3method(reset_env,brmsformula)
S3method(reset_env,default)
S3method(reset_env,formula)
S3method(restructure,bmmfit)
S3method(revert_postprocess_brm,default)
S3method(revert_postprocess_brm,sdmSimple)
S3method(rhs_vars,bmmformula)
Expand Down Expand Up @@ -82,11 +84,7 @@ export(dmixture3p)
export(dsdm)
export(fit_info)
export(fit_model)
export(get_model_prior)
export(get_stancode)
export(get_standata)
export(k2sd)
export(make_stancode_parblock)
export(mixture2p)
export(mixture3p)
export(pIMM)
Expand All @@ -101,7 +99,6 @@ export(qmixture3p)
export(qsdm)
export(rIMM)
export(rad2deg)
export(restructure_bmm)
export(revert_postprocess_brm)
export(rmixture2p)
export(rmixture3p)
Expand All @@ -113,6 +110,8 @@ export(supported_models)
export(use_model_template)
export(wrap)
import(stats)
importFrom(brms,default_prior)
importFrom(brms,restructure)
importFrom(brms,stancode)
importFrom(brms,standata)
importFrom(glue,glue)
Expand Down
7 changes: 5 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### New features
* add a custom **summary()** method for bmm models (#144)
* add a global options **bmm.summary_backend** to control the backend used for the summary() method (choices are "bmm" and "brms")
* deprecate **get_model_prior(), get_stancode() and get_standata()**. These functions will be removed in future versions of the package. Due to [recent changes](https://github.com/paul-buerkner/brms/pull/1604) in *brms* version 2.20.14, you can now use the *brms* functions `default_prior`, `stancode` and `standata` directly with *bmm* models (alternatively, their older aliases, "get_prior", "make_stancode", "make_standata").
* BREAKING CHANGE: remove **get_model_prior(), get_stancode() and get_standata()**. Due to [recent changes](https://github.com/paul-buerkner/brms/pull/1604) in *brms* version 2.21.0, you can now use the *brms* functions `default_prior`, `stancode` and `standata` directly with *bmm* models.
* function **restructure()** now allows to apply methods introduced in newer bmm versions to bmmfit objects created by older bmm versions
* you can now specify any model parameter to be a constant by using an equal sign in the `bmmformula` (#142)
* you can now choose to estimate parameters that are fixed to a constant by default for all models (#145)
Expand All @@ -13,9 +13,12 @@

### Bug fixes
* fix a bug preventing the sort_data check from being executed (#72)
* fix a bug with the summary() function not displaying implicit parameters (#152)
* fix bugs with the summary() function not displaying implicit parameters (#152) and not working properly with some hierarhical designs (#173)
* fix a bug in which the sort_data check occured in cases where it shouldn't (#158)

### Other changes
* `bmm` now requires the latest version of `brms` (>= 2.21.0).

# bmm 0.4.0

### New features
Expand Down
2 changes: 1 addition & 1 deletion R/fit_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#' @param chains Numeric. Number of Markov chains (defaults to 4)
#' @param prior One or more `brmsprior` objects created by [brms::set_prior()]
#' or related functions and combined using the c method or the + operator. See
#' also [get_model_prior()] for more help. Not necessary for the default model
#' also [default_prior()] for more help. Not necessary for the default model
#' fitting, but you can provide prior constraints to model parameters
#' @param sort_data Logical. If TRUE, the data will be sorted by the predictor
#' variables for faster sampling. If FALSE, the data will not be sorted, but
Expand Down
35 changes: 7 additions & 28 deletions R/helpers-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,50 +221,27 @@ rad2deg <- function(rad){
#' this function will return the combined stan data generated by `bmm` and
#' `brms`
#'
#' @details This function is deprecated. Please use `standata()` or
#' `make_standata()` (if using `brms` >= 2.20.14) instead. In `brms` >=
#' 2.20.14, `make_standata()` became an alias for `standata()`, and
#' `standata()` is the recommended function to use.
#'
#' @inheritParams fit_model
#' @param object A `bmmformula` object
#' @param formula Deprecated. Use `object` instead.
#' @param ... Further arguments passed to [brms::make_standata()]. See the
#' description of [brms::make_standata()] for more details
#' @param ... Further arguments passed to [brms::standata()]. See the
#' description of [brms::standata()] for more details
#'
#' @returns A named list of objects containing the required data to fit a bmm
#' model with Stan.
#'
#'
#' @seealso [supported_models()], [brms::make_standata()], [brms::standata()]
#' @seealso [supported_models()], [brms::standata()]
#'
#' @export
#'
#' @keywords extract_info
#' @importFrom brms standata
#'
#' @examples
#' sdata1 <- standata(bmf(c ~ 1, kappa ~ 1),
#' data = OberauerLin_2017,
#' model = sdmSimple(resp_err = "dev_rad"))
#' str(sdata1)
#' @importFrom brms standata
#' @export
get_standata <- function(object, data, model, prior=NULL, formula = object, ...) {
if (utils::packageVersion('brms') >= "2.20.14") {
message("get_standata is deprecated. Please use standata() or make_standata() instead.")
} else {
message("get_standata is deprecated. Please use standata() instead.")
}
warnif(missing(object) && !missing(formula),
"The 'formula' argument is deprecated for consistency with brms (>= 2.20.14). \\
Please use 'object' instead.")

standata.bmmformula(object = formula, data = data,
model = model, prior = prior, ...)
}

#' @export
#' @rdname get_standata
standata.bmmformula <- function(object, data, model, prior = NULL, ...) {
# check model, formula and data, and transform data if necessary
formula <- object
Expand All @@ -281,7 +258,9 @@ standata.bmmformula <- function(object, data, model, prior = NULL, ...) {
# extract stan code
dots <- list(...)
fit_args <- combine_args(nlist(config_args, dots, prior))
brms::do_call(brms::make_standata, fit_args)
fit_args$object <- fit_args$formula
fit_args$formula <- NULL
brms::do_call(brms::standata, fit_args)
}

# check if the data is sorted by the predictors
Expand Down
74 changes: 8 additions & 66 deletions R/helpers-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -608,48 +608,26 @@ use_model_template <- function(model_name,


#' @title Generate Stan code for bmm models
#' @description A wrapper around `brms::make_stancode()` for models specified
#' with `bmm`. Given the `model`, the `data` and the `formula` for the model,
#' @description Given the `model`, the `data` and the `formula` for the model,
#' this function will return the combined stan code generated by `bmm` and
#' `brms`
#'
#' @inheritParams fit_model
#' @param object A `bmmformula` object
#' @param formula Deprecated. Use `object` instead.
#' @param ... Further arguments passed to [brms::make_stancode()]. See the
#' description of [brms::make_stancode()] for more details
#' @param ... Further arguments passed to [brms::stancode()]. See the
#' description of [brms::stancode()] for more details
#'
#' @details This function is deprecated. Please use `stancode()` or
#' `make_stancode()` (if using `brms` >= 2.20.14) instead. In `brms` >=
#' 2.20.14, `make_stancode()` became an alias for `stancode()`, and
#' `stancode()` is the recommended function to use.
#' @returns A character string containing the fully commented Stan code to fit a
#' bmm model.
#'
#' @seealso [supported_models()], [brms::make_stancode()]
#' @export
#' @seealso [supported_models()], [brms::stancode()]
#' @keywords extract_info
#' @importFrom brms stancode
#' @examples
#' scode1 <- stancode(bmf(c ~ 1, kappa ~ 1),
#' data = OberauerLin_2017,
#' model = sdmSimple(resp_err = "dev_rad"))
#' cat(scode1)
get_stancode <- function(object, data, model, prior=NULL, formula = object, ...) {
if (utils::packageVersion('brms') >= "2.20.14") {
message("get_stancode is deprecated. Please use stancode() or make_stancode() instead.")
} else {
message("get_stancode is deprecated. Please use stancode() instead.")
}
warnif(missing(object) && !missing(formula),
"The 'formula' argument is deprecated for consistency with brms (>= 2.20.14). \\
Please use 'object' instead.")

stancode.bmmformula(object = formula, data = data,
model = model, prior = prior, ...)
}

#' @rdname get_stancode
#' @importFrom brms stancode
#' @export
stancode.bmmformula <- function(object, data, model, prior = NULL, ...) {
withr::local_options(bmm.sort_data = FALSE)
Expand All @@ -669,43 +647,7 @@ stancode.bmmformula <- function(object, data, model, prior = NULL, ...) {
# extract stan code
dots <- list(...)
fit_args <- combine_args(nlist(config_args, dots, prior))
brms::do_call(brms::make_stancode, fit_args)
}



#' @title Get the parameter block from a generated Stan code for bmm models
#' @description A wrapper around `get_stancode()` for models specified with
#' `bmm`. Given the `model`, the `data` and the `formula` for the model, this
#' function will return just the parameters block. Useful for figuring out
#' which paramters you can set initial values on
#' @inheritParams fit_model
#' @param ... Further arguments passed to [brms::make_stancode()]. See the
#' description of [brms::make_stancode()] for more details
#'
#' @keywords extract_info
#'
#' @returns A character string containing the parameter block of fully commented
#' Stan code to fit a bmm model.
#'
#'
#' @seealso [supported_models()], [get_stancode()]
#'
#' @export
make_stancode_parblock <- function(formula, data, model, prior=NULL, ...) {
stancode <- get_stancode(formula, data, model, prior, ...)
.extract_parblock(stancode)
}


#' @title Extract the parameter block from the Stan code
#' @description Given the Stan code for a model, this function will extract the
#' parameter block from the Stan code
#' @param stancode A character string containing the fully commented Stan code
#' @noRd
.extract_parblock <- function(stancode) {
parblock <- stringr::str_match(as.character(stancode),
"(?s)parameters \\{\\n(.*?)\\}\\ntransformed")[,2]
class(parblock) <- class(stancode)
parblock
fit_args$object <- fit_args$formula
fit_args$formula <- NULL
brms::do_call(brms::stancode, fit_args)
}
Loading