Create monty_model
from a function computing density
Source: R/model-function.R
monty_model_function.Rd
Create a monty_model from a function that computes density. This allows use of any R function as a simple monty model. If you need advanced model features, then this interface may not suit you and you may prefer to use monty_model directly.
Arguments
- density
A function to compute log density. It can take any number of parameters
- packer
Optionally, a monty_packer object to control how your function parameters are packed into a numeric vector. You can typically omit this if all the arguments to your functions are present in your numeric vector and if they are all scalars.
- fixed
Optionally, a named list of fixed values to substitute into the call to
density
. This cannot be used in conjunction withpacker
(you should use thefixed
argument tomonty_packer
instead).
Value
A monty_model object that computes log density with the
provided density
function, given a numeric vector argument
representing all parameters.
Details
This interface will expand in future versions of monty to support gradients, stochastic models, parameter groups and simultaneous calculation of density.
Examples
banana <- function(a, b, sd) {
dnorm(b, log = TRUE) + dnorm((a - b^2) / sd, log = TRUE)
}
m <- monty_model_function(banana, fixed = list(sd = 0.25))
m
#>
#> ── <monty_model> ───────────────────────────────────────────────────────────────
#> ℹ Model has 2 parameters: 'a' and 'b'
#> ℹ See `?monty_model()` for more information
# Density from our new model. Note that this computes density
# using an unstructured parameter vector, which is mapped to 'a'
# and 'b':
monty_model_density(m, c(0, 0))
#> [1] -1.837877
# Same as the built-in banana example:
monty_model_density(monty_example("banana"), c(0, 0))
#> [1] -1.837877