Skip to content

Commit

Permalink
add function for extracting the parameter block of stan code
Browse files Browse the repository at this point in the history
  • Loading branch information
venpopov committed Feb 9, 2024
1 parent 44b9431 commit 5d8f6f3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
^vignettes/articles$
^doc$
^Meta$
^dev_utils$
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Suggests:
ggplot2,
mixtur,
ggthemes,
cowplot
cowplot,
stringr
Config/testthat/edition: 3
Imports:
brms,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export(dsdm)
export(fit_model)
export(get_model_prior)
export(get_stancode)
export(get_stancode_parblock)
export(get_standata)
export(k2sd)
export(mixture2p)
Expand Down
50 changes: 50 additions & 0 deletions R/helpers-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,53 @@ get_stancode <- function(formula, data, model, prior=NULL, ...) {

return(stancode)
}



#' @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
#' @param formula An object of class `brmsformula`. 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
#' passed to the `bmmmodel` object for required argurments.
#' @param model A description of the model to be fitted. This is a call to a
#' `bmmmodel` such as `mixture3p()` function. Every model function has a
#' number of required arguments which need to be specified within the function
#' call. Call [supported_models()] to see the list of supported models and
#' their required arguments
#' @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
#' fitting, but you can provide prior constraints to model parameters
#' @param ... Further arguments passed to [brms::make_stancode()]. See the
#' description of [brms::make_stancode()] for more details
#'
#' @returns A character string containing the parameter block of fully commented
#' Stan code to fit a bmm model.
#'
#'
#' @seealso [supported_models()], [get_stancode()]
#'
#' @export
get_stancode_parblock <- function(formula, data, model, prior=NULL, ...) {
stancode <- get_stancode(formula, data, model, prior, ...)
parblock <- .extract_parblock(stancode)
return(parblock)
}


#' @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)
return(parblock)
}

0 comments on commit 5d8f6f3

Please sign in to comment.