extend-bugs: Add a custom R function or distribution to the BUGS language.

Description Usage Arguments Value See Also Examples

Description

Add a custom R function or distribution to the BUGS language.

In the current version, the custom distributions can only be used for unobserved nodes.

Usage

1
2
3
4
5
6
7
biips_add_function(name, n_param, fun_dim, fun_eval,
  fun_check_param = function(...) TRUE, fun_is_discrete = function(...)
  FALSE)

biips_add_distribution(name, n_param, fun_dim, fun_sample,
  fun_check_param = function(...) TRUE, fun_is_discrete = function(...)
  FALSE)

Arguments

name

string. Name of the custom function that will be used in the BUGS model. must be a valid BUGS language function name.

n_param

integer. Number of arguments of the custom function or distribution.

fun_dim

custom R function returning the size vector of the output. It will be called when compiling the model. Its arguments are the dimension vectors of the inputs.

fun_eval

custom R function which evaluates the function. Its arguments are the parameters values.

fun_check_param

custom R function which checks if the argument values are valid. Its arguments are the parameters values. Returns a logical. (default returns TRUE)

fun_is_discrete

custom R function returning a logical that is TRUE if the output is discrete. Its arguments are logicals indicating if the arguments are discrete. (default returns FALSE)

All the given R functions must have the same number of input arguments.

fun_sample

custom R function which samples from the distribution. Its arguments are the parameters values.

Value

NULL

See Also

biips_model

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#' # Add custom functions and distributions to BUGS language
#' Add custom function `f`
f_dim <- function(x_dim, t_dim) {
  # Check dimensions of the input and return dimension of the output of function f
  stopifnot(prod(x_dim) == 1, prod(t_dim) == 1)
  x_dim
}
f_eval <- function(x, t) {
  # Evaluate function f
  0.5 * x + 25 * x/(1 + x^2) + 8 * cos(1.2 * t)
}
biips_add_function('f', 2, f_dim, f_eval)

#' Add custom sampling distribution `dMN`
dMN_dim <- function(mu_dim, Sig_dim) {
  # Check dimensions of the input and return dimension of the output of
  # distribution dMN
  stopifnot(prod(mu_dim) == mu_dim[1], length(Sig_dim) == 2, mu_dim[1] == Sig_dim)
  mu_dim
}
dMN_sample <- function(mu, Sig) {
  # Draw a sample of distribution dMN
  mu + t(chol(Sig)) %*% rnorm(length(mu))
}
biips_add_distribution('dMN', 2, dMN_dim, dMN_sample)

#' # Compile model
modelfile <- system.file('extdata', 'hmm_f.bug', package = 'rbiips')
stopifnot(nchar(modelfile) > 0)
cat(readLines(modelfile), sep = '\n')

data <- list(tmax = 10, p = c(.5, .5), logtau_true = log(1), logtau = log(1))
model <- biips_model(modelfile, data, sample_data = TRUE)

biips/rbiips documentation built on Nov. 28, 2020, 2:12 p.m.