diff --git a/NAMESPACE b/NAMESPACE index 7cfdf886..6d3d91f0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/fit_model.R b/R/fit_model.R index a90b9a4c..8346d08b 100644 --- a/R/fit_model.R +++ b/R/fit_model.R @@ -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 diff --git a/R/helpers-formula.R b/R/helpers-formula.R index 58dc8eb4..f4194605 100644 --- a/R/helpers-formula.R +++ b/R/helpers-formula.R @@ -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: is the formula valid for the specified model type @@ -101,7 +101,7 @@ 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) @@ -109,7 +109,7 @@ get_response <- function(formula) { #' #' imm_formula #' -bmm_formula <- function(formula, ...){ +bmmformula <- function(formula, ...){ # paste formulas into a single list dots <- list(...) formula <- list(formula) diff --git a/R/helpers-model.R b/R/helpers-model.R index 0aa3be5f..00b22647 100644 --- a/R/helpers-model.R +++ b/R/helpers-model.R @@ -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 @@ -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 diff --git a/R/helpers-prior.R b/R/helpers-prior.R index 6a748197..4bcfc3bf 100644 --- a/R/helpers-prior.R +++ b/R/helpers-prior.R @@ -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 @@ -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) #' diff --git a/man/bmm_formula.Rd b/man/bmmformula.Rd similarity index 96% rename from man/bmm_formula.Rd rename to man/bmmformula.Rd index c02777fd..186634ea 100644 --- a/man/bmm_formula.Rd +++ b/man/bmmformula.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/helpers-formula.R -\name{bmm_formula} -\alias{bmm_formula} +\name{bmmformula} +\alias{bmmformula} \title{Create formula for predicting parameters of a \code{bmmmodel}} \usage{ -bmm_formula(formula, ...) +bmmformula(formula, ...) } \arguments{ \item{formula}{Formula for predicting a \code{bmmmodel} parameter.} @@ -62,7 +62,7 @@ syntax. For more information on the \code{brms} formula syntax, see } \examples{ -imm_formula <- bmm_formula( +imm_formula <- bmmformula( c ~ 0 + setsize + (0 + setsize | id), a ~ 1, kappa ~ 0 + setsize + (0 + setsize | id) diff --git a/man/fit_model.Rd b/man/fit_model.Rd index 05ed0a7e..fe236027 100644 --- a/man/fit_model.Rd +++ b/man/fit_model.Rd @@ -15,7 +15,7 @@ fit_model( ) } \arguments{ -\item{formula}{An object of class \code{brmsformula}. A symbolic description of +\item{formula}{An object of class \code{bmmformula}. A symbolic description of the model to be fitted.} \item{data}{An object of class data.frame, containing data of all variables diff --git a/man/get_model_prior.Rd b/man/get_model_prior.Rd index d8dfc037..a95612b9 100644 --- a/man/get_model_prior.Rd +++ b/man/get_model_prior.Rd @@ -7,7 +7,7 @@ get_model_prior(formula, data, model, ...) } \arguments{ -\item{formula}{An object of class \code{brmsformula}. A symbolic description of +\item{formula}{An object of class \code{bmmformula}. A symbolic description of the model to be fitted.} \item{data}{An object of class data.frame, containing data of all variables @@ -58,7 +58,7 @@ Type \code{help(package=bmm)} for a full list of available help topics. dat <- data.frame(y = rsdm(n=2000)) # define formula -ff <- brms::bf(y ~ 1, +ff <- b(y ~ 1, c ~ 1, kappa ~ 1) diff --git a/man/get_stancode.Rd b/man/get_stancode.Rd index 201700a0..89ae515f 100644 --- a/man/get_stancode.Rd +++ b/man/get_stancode.Rd @@ -7,7 +7,7 @@ get_stancode(formula, data, model, prior = NULL, ...) } \arguments{ -\item{formula}{An object of class \code{brmsformula}. A symbolic description of +\item{formula}{An object of class \code{bmmformula}. A symbolic description of the model to be fitted.} \item{data}{An object of class data.frame, containing data of all variables diff --git a/man/get_stancode_parblock.Rd b/man/get_stancode_parblock.Rd index cb84829c..1b9a0eac 100644 --- a/man/get_stancode_parblock.Rd +++ b/man/get_stancode_parblock.Rd @@ -7,7 +7,7 @@ get_stancode_parblock(formula, data, model, prior = NULL, ...) } \arguments{ -\item{formula}{An object of class \code{brmsformula}. A symbolic description of +\item{formula}{An object of class \code{bmmformula}. A symbolic description of the model to be fitted.} \item{data}{An object of class data.frame, containing data of all variables diff --git a/tests/testthat/test-fit_model.R b/tests/testthat/test-fit_model.R index c67f99b3..0942df23 100644 --- a/tests/testthat/test-fit_model.R +++ b/tests/testthat/test-fit_model.R @@ -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) @@ -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) @@ -34,7 +34,7 @@ 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) @@ -42,7 +42,7 @@ 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, 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") @@ -66,7 +66,7 @@ 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.") } @@ -74,11 +74,11 @@ test_that('Available models produce expected errors', { 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") } diff --git a/tests/testthat/test-helpers-data.R b/tests/testthat/test-helpers-data.R index a3aa19d4..8b171788 100644 --- a/tests/testthat/test-helpers-data.R +++ b/tests/testthat/test-helpers-data.R @@ -1,16 +1,16 @@ 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))) @@ -18,21 +18,21 @@ test_that("check_data() produces expected errors and warnings", { 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") } }) @@ -40,7 +40,7 @@ test_that("check_data() produces expected errors and warnings", { 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") } }) @@ -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) diff --git a/tests/testthat/test-helpers-model.R b/tests/testthat/test-helpers-model.R index 09a59a8b..8e7b8f3d 100644 --- a/tests/testthat/test-helpers-model.R +++ b/tests/testthat/test-helpers-model.R @@ -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) diff --git a/tests/testthat/test-helpers-postprocess.R b/tests/testthat/test-helpers-postprocess.R index 09cfbff8..0f0a83ed 100644 --- a/tests/testthat/test-helpers-postprocess.R +++ b/tests/testthat/test-helpers-postprocess.R @@ -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, diff --git a/tests/testthat/test-helpers-prior.R b/tests/testthat/test-helpers-prior.R index c9161545..7a8cefb9 100644 --- a/tests/testthat/test-helpers-prior.R +++ b/tests/testthat/test-helpers-prior.R @@ -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) diff --git a/vignettes/mixture_models.Rmd b/vignettes/mixture_models.Rmd index f3002d9f..191d5df6 100644 --- a/vignettes/mixture_models.Rmd +++ b/vignettes/mixture_models.Rmd @@ -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)) ```