Skip to content

Commit

Permalink
rename bmm_formula to bmmformula and replace all old brmsformula refe…
Browse files Browse the repository at this point in the history
…rences in documentation

- there are still brms::bf cases in examples and vignettes, but let's change those at the end when everything is working
  • Loading branch information
venpopov committed Feb 13, 2024
1 parent 58b635d commit f33ff77
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export("%>%")
export(IMMabc)
export(IMMbsc)
export(IMMfull)
export(bmm_formula)
export(bmmformula)
export(c_bessel2sqrtexp)
export(c_sqrtexp2bessel)
export(calc_error_relative_to_nontargets)
Expand Down
2 changes: 1 addition & 1 deletion R/fit_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' This is a wrapper function for [brms::brm], which is used to estimate the
#' model.
#'
#' @param formula An object of class `brmsformula`. A symbolic description of
#' @param formula An object of class `bmmformula`. A symbolic description of
#' the model to be fitted.
#' @param data An object of class data.frame, containing data of all variables
#' used in the model. The names of the variables must match the variable names
Expand Down
8 changes: 4 additions & 4 deletions R/helpers-formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ check_formula <- function(model, formula) {
# Pre-Check: was a valid brms formula provided
if (inherits(formula, 'brmsformula')) {
stop("The provided formula is a brms formula.
Please specify formula with the bmm_formula() function instead of
Please specify formula with the bmmformula() function instead of
the brmsformula() or bf() function.
E.g.: bmm_formula(kappa ~ 1, thetat ~ 1")
E.g.: bmmformula(kappa ~ 1, thetat ~ 1")

Check warning on line 12 in R/helpers-formula.R

View check run for this annotation

Codecov / codecov/patch

R/helpers-formula.R#L9-L12

Added lines #L9 - L12 were not covered by tests
}

# Check: is the formula valid for the specified model type
Expand Down Expand Up @@ -101,15 +101,15 @@ get_response <- function(formula) {
#' @return A list of formulas for each parameters being predicted
#' @export
#' @examples
#' imm_formula <- bmm_formula(
#' imm_formula <- bmmformula(
#' c ~ 0 + setsize + (0 + setsize | id),
#' a ~ 1,
#' kappa ~ 0 + setsize + (0 + setsize | id)
#' )
#'
#' imm_formula
#'
bmm_formula <- function(formula, ...){
bmmformula <- function(formula, ...){
# paste formulas into a single list
dots <- list(...)
formula <- list(formula)
Expand Down
4 changes: 2 additions & 2 deletions R/helpers-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ use_model_template <- function(model_name,
#' @description A wrapper around `brms::make_stancode()` for models specified with
#' `bmm`. Given the `model`, the `data` and the `formula` for the model, this
#' function will return the combined stan code generated by `bmm` and `brms`
#' @param formula An object of class `brmsformula`. A symbolic description of
#' @param formula An object of class `bmmformula`. A symbolic description of
#' the model to be fitted.
#' @param data An object of class data.frame, containing data of all variables
#' used in the model. The names of the variables must match the variable names
Expand Down Expand Up @@ -550,7 +550,7 @@ get_stancode <- function(formula, data, model, prior=NULL, ...) {
#' `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
#' @param formula An object of class `brmsformula`. A symbolic description of
#' @param formula An object of class `bmmformula`. A symbolic description of
#' the model to be fitted.
#' @param data An object of class data.frame, containing data of all variables
#' used in the model. The names of the variables must match the variable names
Expand Down
4 changes: 2 additions & 2 deletions R/helpers-prior.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ combine_prior <- function(prior1, prior2) {
#' return all model parameters that have no prior specified (flat priors). This can help to
#' get an idea about which priors need to be specified and also know which priors were
#' used if no user-specified priors were passed to the [fit_model()] function.
#' @param formula An object of class `brmsformula`. A symbolic description of
#' @param formula An object of class `bmmformula`. A symbolic description of
#' the model to be fitted.
#' @param data An object of class data.frame, containing data of all variables
#' used in the model. The names of the variables must match the variable names
Expand Down Expand Up @@ -53,7 +53,7 @@ combine_prior <- function(prior1, prior2) {
#' dat <- data.frame(y = rsdm(n=2000))
#'
#' # define formula
#' ff <- brms::bf(y ~ 1,
#' ff <- b(y ~ 1,
#' c ~ 1,
#' kappa ~ 1)
#'
Expand Down
8 changes: 4 additions & 4 deletions man/bmm_formula.Rd → man/bmmformula.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/fit_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/get_model_prior.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/get_stancode.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/get_stancode_parblock.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions tests/testthat/test-fit_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ test_that('Available mock models run without errors',{
)

# two-parameter model mock fit
f <- bmm_formula(kappa ~ 1, thetat ~ 1)
f <- bmmformula(kappa ~ 1, thetat ~ 1)
mock_fit <- fit_model(f, dat, mixture2p(respErr = "respErr"), backend="mock", mock_fit=1, rename=FALSE)
expect_equal(mock_fit$fit, 1)
expect_type(mock_fit$fit_args, "list")
expect_equal(names(mock_fit$fit_args[1:4]), c("formula", "data", "family", "prior"))

# three-parameter model mock fit
f <- bmm_formula(kappa ~ 1, thetat ~ 1, thetant ~ 1)
f <- bmmformula(kappa ~ 1, thetat ~ 1, thetant ~ 1)
mock_fit <- fit_model(f, dat, mixture3p(respErr = "respErr", setsize = 3,
non_targets = paste0('Item',2:3,'_rel')),
backend="mock", mock_fit=1, rename=FALSE)
Expand All @@ -25,7 +25,7 @@ test_that('Available mock models run without errors',{
expect_equal(names(mock_fit$fit_args[1:4]), c("formula", "data", "family", "prior"))

# IMMabc model mock fit
f <- bmm_formula(kappa ~ 1, c ~ 1, a ~ 1)
f <- bmmformula(kappa ~ 1, c ~ 1, a ~ 1)
mock_fit <- fit_model(f, dat, IMMabc(respErr = "respErr", setsize =3,
non_targets = paste0('Item',2:3,'_rel')),
backend="mock", mock_fit=1, rename=FALSE)
Expand All @@ -34,15 +34,15 @@ test_that('Available mock models run without errors',{
expect_equal(names(mock_fit$fit_args[1:4]), c("formula", "data", "family", "prior"))

# IMMbsc model mock fit
f <- bmm_formula(kappa ~ 1, c ~ 1, s ~ 1)
f <- bmmformula(kappa ~ 1, c ~ 1, s ~ 1)
mock_fit <- fit_model(f, dat, IMMbsc(respErr = "respErr", setsize=3, non_targets = paste0('Item',2:3,'_rel'), spaPos=paste0('spaD',2:3)),
backend="mock", mock_fit=1, rename=FALSE)
expect_equal(mock_fit$fit, 1)
expect_type(mock_fit$fit_args, "list")
expect_equal(names(mock_fit$fit_args[1:4]), c("formula", "data", "family", "prior"))

# IMMbsc model mock fit
f <- bmm_formula(kappa ~ 1, c ~ 1, a ~ 1, s ~ 1)
f <- bmmformula(kappa ~ 1, c ~ 1, a ~ 1, s ~ 1)
mock_fit <- fit_model(f, dat, IMMfull(respErr = "respErr", setsize=3, non_targets = paste0('Item',2:3,'_rel'), spaPos=paste0('spaD',2:3)), backend="mock", mock_fit=1, rename=FALSE)
expect_equal(mock_fit$fit, 1)
expect_type(mock_fit$fit_args, "list")
Expand All @@ -66,19 +66,19 @@ test_that('Available models produce expected errors', {
args_list <- formals(model)
test_args <- lapply(args_list, function(x) {NULL})
model <- brms::do_call(model, test_args)
expect_error(fit_model(bmm_formula(kappa~1), model=model, backend="mock", mock_fit=1, rename=FALSE),
expect_error(fit_model(bmmformula(kappa~1), model=model, backend="mock", mock_fit=1, rename=FALSE),
"Data must be specified using the 'data' argument.")
}


okmodels <- c('mixture3p','IMMabc','IMMbsc','IMMfull')
for (model in okmodels) {
model1 <- get_model2(model)(respErr = "respErr", non_targets='Item2_rel', setsize=5, spaPos='spaD2')
expect_error(fit_model(bmm_formula(kappa~1), data=dat, model=model1, backend="mock",
expect_error(fit_model(bmmformula(kappa~1), data=dat, model=model1, backend="mock",
mock_fit=1, rename=FALSE),
"'non_targets' is less than max\\(setsize\\)-1")
model2 <- get_model2(model)(respErr = "respErr", non_targets='Item2_rel', setsize=TRUE, spaPos='spaD2')
expect_error(fit_model(bmm_formula(kappa~1), data=dat, model=model2, backend="mock",
expect_error(fit_model(bmmformula(kappa~1), data=dat, model=model2, backend="mock",
mock_fit=1, rename=FALSE),
"'setsize' must be either a single numeric value or a character string")
}
Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test-helpers-data.R
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
test_that("check_data() produces expected errors and warnings", {
expect_error(check_data(.model_mixture2p(respErr = "y")),
"Data must be specified using the 'data' argument.")
expect_error(check_data(.model_mixture2p(respErr = "y"), data.frame(), bmm_formula(kappa ~ 1)),
expect_error(check_data(.model_mixture2p(respErr = "y"), data.frame(), bmmformula(kappa ~ 1)),
"Argument 'data' does not contain observations.")
expect_error(check_data(.model_mixture2p(respErr = "y"), data.frame(x = 1), bmm_formula(kappa ~ 1)),
expect_error(check_data(.model_mixture2p(respErr = "y"), data.frame(x = 1), bmmformula(kappa ~ 1)),
"The response variable 'y' is not present in the data.")

mls <- lapply(c('mixture2p','mixture3p','IMMabc','IMMbsc','IMMfull'), get_model2)
for (ml in mls) {
expect_warning(check_data(ml(respErr = "y", non_targets = 'x', setsize=2, spaPos = 'z'),
data.frame(y = 12, x = 1, z = 2),
bmm_formula(kappa ~ 1)),
bmmformula(kappa ~ 1)),
"It appears your response variable is in degrees.\n")
expect_silent(check_data(ml(respErr = "y", non_targets = 'x', setsize=2, spaPos = 'z'),
data.frame(y = 1, x = 1, z = 2), brms::bf(y ~ 1)))
}

mls <- lapply(c('mixture3p','IMMabc','IMMbsc','IMMfull'), get_model2)
for (ml in mls) {
expect_error(check_data(ml(respErr = "y", non_targets = 'x', spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmm_formula(kappa ~ 1)),
expect_error(check_data(ml(respErr = "y", non_targets = 'x', spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmmformula(kappa ~ 1)),
'argument "setsize" is missing, with no default')
expect_error(check_data(ml(respErr = "y",setsize = 'x', spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmm_formula(kappa ~ 1)),
expect_error(check_data(ml(respErr = "y",setsize = 'x', spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmmformula(kappa ~ 1)),
'argument "non_targets" is missing, with no default')
expect_error(check_data(ml(respErr = "y",non_targets='x', setsize = TRUE, spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmm_formula(kappa ~ 1)),
expect_error(check_data(ml(respErr = "y",non_targets='x', setsize = TRUE, spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmmformula(kappa ~ 1)),
"Argument 'setsize' must be either a single numeric value or a character string.")
expect_error(check_data(ml(respErr = "y",non_targets='x', setsize = c(1,2,3), spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmm_formula(kappa ~ 1)),
expect_error(check_data(ml(respErr = "y",non_targets='x', setsize = c(1,2,3), spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmmformula(kappa ~ 1)),
"Argument 'setsize' must be either a single numeric value or a character string.")
expect_error(check_data(ml(respErr = "y",non_targets='x', setsize = 5, spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmm_formula(kappa ~ 1)),
expect_error(check_data(ml(respErr = "y",non_targets='x', setsize = 5, spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmmformula(kappa ~ 1)),
"'non_targets' is less than max\\(setsize\\)-1")
}

mls <- lapply(c('IMMbsc','IMMfull'), get_model2)
for (ml in mls) {
expect_error(check_data(ml(respErr = "y",non_targets=paste0('x',1:4), setsize = 5, spaPos = 'z'), data.frame(y = 1, x1 = 1, x2=2,x3=3,x4=4, z = 2), bmm_formula(kappa ~ 1)),
expect_error(check_data(ml(respErr = "y",non_targets=paste0('x',1:4), setsize = 5, spaPos = 'z'), data.frame(y = 1, x1 = 1, x2=2,x3=3,x4=4, z = 2), bmmformula(kappa ~ 1)),
"'spaPos' is less than max\\(setsize\\)-1")
}
})

test_that("check_data() returns a data.frame()", {
mls <- lapply(supported_models(print_call=FALSE), get_model)
for (ml in mls) {
expect_s3_class(check_data(ml(respErr = "y",non_targets = 'x', setsize=2, spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmm_formula(kappa ~ 1)), "data.frame")
expect_s3_class(check_data(ml(respErr = "y",non_targets = 'x', setsize=2, spaPos = 'z'), data.frame(y = 1, x = 1, z = 2), bmmformula(kappa ~ 1)), "data.frame")
}
})

Expand Down Expand Up @@ -86,7 +86,7 @@ test_that("rad2deg returns the correct values for 0, pi/2, 2*pi", {

test_that("get_standata() returns a string", {
# define formula
ff <- bmm_formula(kappa ~ 1,
ff <- bmmformula(kappa ~ 1,
thetat ~ 1,
thetant ~ 1)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-helpers-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test_that("use_model_template() prevents duplicate models", {

test_that("get_stancode() returns a string", {
# define formula
ff <- bmm_formula(kappa ~ 1,
ff <- bmmformula(kappa ~ 1,
thetat ~ 1,
thetant ~ 1)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-helpers-postprocess.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_that("bmm version is added to mock model", {
dat <- data.frame(y = rsdm(n=10))

ff <- bmm_formula(c ~ 1,
ff <- bmmformula(c ~ 1,
kappa ~ 1)

fit <- fit_model(formula = ff,
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-helpers-prior.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("get_model_prior() returns a brmsprior object", {
# define formula
ff <- bmm_formula(kappa ~ 1,
ff <- bmmformula(kappa ~ 1,
thetat ~ 1,
thetant ~ 1)

Expand Down
2 changes: 1 addition & 1 deletion vignettes/mixture_models.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ The model formula has three components:
[^fn-1]: `brms` does not directly estimate the probabilities that each response comes from each distribution (e.g. $p_{mem}$ and $p_{guess}$). Instead, brms estimates mixing proportions that are weights applied to each of the mixture distributions and they are transformed into probabilities (e.g. $p_{mem}$ and $p_{guess}$) using a softmax normalization. To get $p_{mem}$ we can use the softmax function, that is: $p_{mem} = \frac{exp(\theta_{target})}{1+exp(\theta_{target})}$

```{r}
ff <- bmm_formula(thetat ~ 0 + set_size + (0 + set_size | id),
ff <- bmmformula(thetat ~ 0 + set_size + (0 + set_size | id),
kappa ~ 0 + set_size + (0 + set_size | id))
```

Expand Down

0 comments on commit f33ff77

Please sign in to comment.