Skip to content

Instantly share code, notes, and snippets.

@MansMeg
Last active September 25, 2018 13:53
Show Gist options
  • Save MansMeg/bb5f5772cbacc143cffd to your computer and use it in GitHub Desktop.
Save MansMeg/bb5f5772cbacc143cffd to your computer and use it in GitHub Desktop.
Random draw from inverse gamma and inverse chisquare
#' @title
#' rInvGamma
#'
#' @description
#' Random draws from the inverse gamma distribution.
#'
#' @param n See n in rgamma()
#' @param shape Alpha
#' @param scale Beta
#'
#' @details
#' Samples an inverse gamma (taken from MCMCpack - cred to them)
#'
#' @refereces
#' http://en.wikipedia.org/wiki/Inverse-gamma_distribution
rInvGamma <- function (n, shape, scale = 1) {
return(1/rgamma(n = n, shape = shape, rate = scale))
}
#' @title
#' rInvChiSq
#'
#' @description
#' Random draws from the inverse chisquare distribution.
#'
#' @param n See n in rgamma()
#' @param nu See nu at the Wiki page.
#'
#' @references
#' http://en.wikipedia.org/wiki/Inverse-gamma_distribution
#'
rInvChiSq <- function (n, nu) {
return(rInvGamma(n, shape = nu / 2, scale = 1/2))
}
#' @title
#' rScaledInvChiSq
#'
#' @description
#' Random draws from the scaled inverse chisquare distribution.
#'
#' @param n See n in rgamma()
#' @param nu See nu at the Wiki page.
#' @param tau2 See tau2 at the Wiki page.
#'
#' @references
#' http://en.wikipedia.org/wiki/Scaled-inverse-chi-squared_distribution
#'
rScaledInvChiSq <- function (n, nu, tau2) {
return(rInvGamma(n, shape = nu / 2, scale = (nu * tau2) / 2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment