Skip to content

Commit

Permalink
Merge pull request #114 from venpopov/feature/issue-102-allow-specify…
Browse files Browse the repository at this point in the history
…-nt_features-and-nt_distances-with-partial-name-rather-than-a-vector

Feature/issue 102 allow specify nt features and nt distances with partial name rather than a vector
  • Loading branch information
GidonFrischkorn authored Feb 22, 2024
2 parents 1c5fa23 + 0cc61b5 commit 5bc2f8b
Show file tree
Hide file tree
Showing 20 changed files with 434 additions and 87 deletions.
2 changes: 1 addition & 1 deletion 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.3.4.9000
Version: 0.3.5.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
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ S3method(check_data,vwm)
S3method(check_formula,bmmmodel)
S3method(check_formula,default)
S3method(check_formula,nontargets)
S3method(check_model,bmmmodel)
S3method(check_model,default)
S3method(configure_model,IMMabc)
S3method(configure_model,IMMbsc)
S3method(configure_model,IMMfull)
Expand All @@ -25,6 +27,7 @@ S3method(postprocess_brm,bmmmodel)
S3method(postprocess_brm,default)
S3method(postprocess_brm,sdmSimple)
S3method(postprocess_brm,vwm)
S3method(print,message)
S3method(rhs_vars,bmmformula)
S3method(rhs_vars,formula)
S3method(update,bmmfit)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* add postprocessing methods for sdmSimple to allow for pp_check(), conditional_effects and bridgesampling usage with the model (#30)
* add informed default priors for all models. You can always use the `get_model_prior()` function to see the default priors for a model
* add a new function `set_default_prior` for developers, which allows them to more easily set default priors on new models regardless of the user-specified formula
* you can now specify variables for models via regular expressions rather than character vectors [#102]

### Bug fixes
* fix a bug in the mixture3p and IMM models which caused an error when intercept was not supressed and set size was used as predictor
Expand Down
81 changes: 70 additions & 11 deletions R/bmm_model_IMM.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# MODELS ####
#############################################################################!

.model_IMMabc <- function(resp_err, nt_features, setsize, ...) {
.model_IMMabc <- function(resp_err, nt_features, setsize, regex = FALSE, ...) {
out <- list(
resp_vars = nlist(resp_err),
other_vars = nlist(nt_features, setsize),
Expand All @@ -29,11 +29,13 @@
)),
void_mu = FALSE
)
attr(out, "regex") <- regex
attr(out, "regex_vars") <- c('nt_features') # variables that can be specified via regular expression
class(out) <- c("bmmmodel", "vwm","nontargets","IMMabc")
out
}

.model_IMMbsc <- function(resp_err, nt_features, nt_distances, setsize, ...) {
.model_IMMbsc <- function(resp_err, nt_features, nt_distances, setsize, regex = FALSE, ...) {
out <- list(
resp_vars = nlist(resp_err),
other_vars = nlist(nt_features, nt_distances, setsize),
Expand All @@ -60,11 +62,14 @@
)),
void_mu = FALSE
)
attr(out, "regex") <- regex
# variables that can be specified via regular expression
attr(out, "regex_vars") <- c('nt_features', 'nt_distances')
class(out) <- c("bmmmodel","vwm","nontargets","IMMspatial","IMMbsc")
out
}

.model_IMMfull <- function(resp_err, nt_features, nt_distances, setsize, ...) {
.model_IMMfull <- function(resp_err, nt_features, nt_distances, setsize, regex = FALSE, ...) {
out <- list(
resp_vars = nlist(resp_err),
other_vars = nlist(nt_features, nt_distances, setsize),
Expand Down Expand Up @@ -92,6 +97,9 @@
)),
void_mu = FALSE
)
attr(out, "regex") <- regex
# variables that can be specified via regular expression
attr(out, "regex_vars") <- c('nt_features', 'nt_distances')
class(out) <- c("bmmmodel","vwm","nontargets","IMMspatial","IMMfull")
out
}
Expand All @@ -114,20 +122,71 @@
#'
#' - b = "Background activation (internally fixed to 0)"
#'
#' @param resp_err The name of the variable in the provided dataset containing the
#' response error. The response Error should code the response relative to the to-be-recalled
#' target in radians. You can transform the response error in degrees to radian using the `deg2rad` function.
#' @param nt_features A character vector with the names of the non-target variables.
#' The non_target variables should be in radians and be centered relative to the
#' target.
#' @param nt_distances A vector of names of the columns containing the distances of
#' non-target items to the target item. Only necessary for the `IMMbsc` and `IMMfull` models
#' @param resp_err The name of the variable in the provided dataset containing
#' the response error. The response Error should code the response relative to
#' the to-be-recalled target in radians. You can transform the response error
#' in degrees to radian using the `deg2rad` function.
#' @param nt_features A character vector with the names of the non-target
#' variables. The non_target variables should be in radians and be centered
#' relative to the target. Alternatively, if regex=TRUE, a regular
#' expression can be used to match the non-target feature columns in the
#' dataset.
#' @param nt_distances A vector of names of the columns containing the distances
#' of non-target items to the target item. Alternatively, if regex=TRUE, a regular
#' expression can be used to match the non-target distances columns in the
#' dataset. Only necessary for the `IMMbsc` and `IMMfull` models.
#' @param setsize Name of the column containing the set size variable (if
#' setsize varies) or a numeric value for the setsize, if the setsize is
#' fixed.
#' @param regex Logical. If TRUE, the `nt_features` and `nt_distances` arguments
#' are interpreted as a regular expression to match the non-target feature
#' columns in the dataset.
#' @param ... used internally for testing, ignore it
#' @return An object of class `bmmmodel`
#' @keywords bmmmodel
#' @examples
#' \dontrun{
#' # load data
#' data <- OberauerLin_2017
#'
#' # define formula
#' ff <- bmmformula(
#' kappa ~ 0 + set_size,
#' c ~ 0 + set_size,
#' a ~ 0 + set_size,
#' s ~ 0 + set_size
#' )
#'
#' # specify the full IMM model with explicit column names for non-target features and distances
#' model1 <- IMMfull(resp_err = "dev_rad",
#' nt_features = paste0('col_nt',1:7),
#' nt_distances = paste0('dist_nt',1:7),
#' setsize = 'set_size')
#'
#' # fit the model
#' fit <- fit_model(formula = ff,
#' data = data,
#' model = model1,
#' parallel = T,
#' iter = 500,
#' backend = 'cmdstanr')
#'
#' # alternatively specify the IMM model with a regular expression to match non-target features
#' # this is equivalent to the previous call, but more concise
#' model2 <- IMMfull(resp_err = "dev_rad",
#' nt_features = 'col_nt',
#' nt_distances = 'dist_nt',
#' setsize = 'set_size',
#' regex = TRUE)
#'
#' # fit the model
#' fit <- fit_model(formula = ff,
#' data = data,
#' model = model2,
#' parallel=T,
#' iter = 500,
#' backend='cmdstanr')
#'}
#' @export
IMMfull <- .model_IMMfull

Expand Down
30 changes: 24 additions & 6 deletions R/bmm_model_mixture3p.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# MODELS ####
#############################################################################!

.model_mixture3p <- function(resp_err, nt_features, setsize, ...) {
.model_mixture3p <- function(resp_err, nt_features, setsize, regex = FALSE, ...) {
out <- list(
resp_vars = nlist(resp_err),
other_vars = nlist(nt_features, setsize),
Expand Down Expand Up @@ -30,6 +30,8 @@
)),
void_mu = FALSE
)
attr(out, "regex") <- regex
attr(out, "regex_vars") <- c('nt_features') # variables that can be specified via regular expression
class(out) = c("bmmmodel", "vwm", "nontargets", "mixture3p")
out
}
Expand All @@ -43,11 +45,15 @@
#' the to-be-recalled target in radians. You can transform the response error
#' in degrees to radians using the `deg2rad` function.
#' @param nt_features A character vector with the names of the non-target
#' feature values. The non_target feature values should be in radians and centered
#' relative to the target.
#' feature values. The non_target feature values should be in radians and
#' centered relative to the target. Alternatively, if regex=TRUE, a regular
#' expression can be used to match the non-target feature columns in the
#' dataset.
#' @param setsize Name of the column containing the set size variable (if
#' setsize varies) or a numeric value for the setsize, if the setsize is
#' fixed.
#' @param regex Logical. If TRUE, the `nt_features` argument is interpreted as
#' a regular expression to match the non-target feature columns in the dataset.
#' @param ... used internally for testing, ignore it
#' @return An object of class `bmmmodel`
#' @keywords bmmmodel
Expand All @@ -69,13 +75,25 @@
#' thetant ~ 1
#' )
#'
#' # specify the 3-parameter model
#' model <- mixture3p(resp_err = "y", nt_features = paste0('nt',1:3,'_loc'), setsize = 4)
#' # specify the 3-parameter model with explicit column names for non-target features
#' model1 <- mixture3p(resp_err = "y", nt_features = paste0('nt',1:3,'_loc'), setsize = 4)
#'
#' # fit the model
#' fit <- fit_model(formula = ff,
#' data = dat,
#' model = model,
#' model = model1,
#' parallel=T,
#' iter = 500,
#' backend='cmdstanr')
#'
#' # alternatively specify the 3-parameter model with a regular expression to match non-target features
#' # this is equivalent to the previous call, but more concise
#' model2 <- mixture3p(resp_err = "y", nt_features = "nt.*_loc", setsize = 4, regex = TRUE)
#'
#' # fit the model
#' fit <- fit_model(formula = ff,
#' data = dat,
#' model = model2,
#' parallel=T,
#' iter = 500,
#' backend='cmdstanr')
Expand Down
2 changes: 1 addition & 1 deletion R/fit_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fit_model <- function(formula, data, model, parallel = FALSE, chains = 4,
opts <- configure_options(nlist(parallel, chains, sort_data, silent))

# check model, formula and data, and transform data if necessary
model <- check_model(model)
model <- check_model(model, data)
data <- check_data(model, data, formula)
formula <- check_formula(model, data, formula)

Expand Down
2 changes: 1 addition & 1 deletion R/helpers-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ rad2deg <- function(rad){
get_standata <- function(formula, data, model, prior=NULL, ...) {

# check model, formula and data, and transform data if necessary
model <- check_model(model)
model <- check_model(model, data)
data <- check_data(model, data, formula)
formula <- check_formula(model, data, formula)

Expand Down
Loading

0 comments on commit 5bc2f8b

Please sign in to comment.