Defining new functions and distributions in the BUGS/JAGS/STAN language
Asked Answered
P

3

7

I am very new statistical analysis world and have taken a recent interest in the BUGS/JAGS/STAN modelling language. Something which really surprises me is that I haven't seen any examples of new functions or distributions being defined to avoid code duplication. For example, say I frequently use the square of the poisson distribution, is there anyway to do the following ?

dsqpo <- function(lambda) {
  tmp ~ dpois(lambda)
  tmp2 <- tmp * tmp
  return(tmp2)
}

and then later on

model{
  ...
  x ~ dsqpo(alpha)
  y ~ dsqpo(beta)
}

Without defining a new temporary variable each time.

Penna answered 21/4, 2014 at 20:58 Comment(0)
P
2

For Stan, functions will be available with the next release. The current release, v2.2.0, does not have user-defined functions as part of the language.

For the proposed syntax, see: https://github.com/stan-dev/stan/wiki/Function-Syntax-and-Semantics-Design

For additional Stan-related help, check the stan-users google group: https://groups.google.com/forum/#!forum/stan-users

Purpleness answered 21/4, 2014 at 21:36 Comment(0)
M
2

In WinBUGS, OpenBUGS and JAGS, you can't define new functions as part of the modelling language. However you can do it with low-level programming in Component Pascal (for Win/OpenBUGS) or C++ (for JAGS).

For WinBUGS, see WBDev (http://www.winbugs-development.org.uk/wbdev.html). For OpenBUGS see the UDev subdirectory of the installed program, which contains a PDF manual, basically this works in the same way as WinBUGS.

For JAGS it's not properly documented - there's a user-written tutorial for adding new distributions at http://www.ncbi.nlm.nih.gov/pubmed/23959766, though nothing on functions I know of.

Mention answered 23/4, 2014 at 12:13 Comment(0)
C
1

The recent paper "Bayesian inference with Stan: A tutorial on adding custom distributions" describes how to do this in some detail. I include the doi for a persistent link.

Reference

Annis, J., Miller, B. J., & Palmeri, T. J. (2016). Bayesian inference with Stan: A tutorial on adding custom distributions. Behavior Research Methods, 1–24. http://doi.org/10.3758/s13428-016-0746-9

Charlettecharley answered 17/6, 2016 at 15:13 Comment(2)
I'm afraid you have to 'buy' this tutorial for non academics.Liggett
You can email the authors, or you can search on scihub.Charlettecharley

© 2022 - 2024 — McMap. All rights reserved.