##' Find parameters of Gamma distribution
##'
##' A function that finds the \code{shape} and \code{rate} paramters required by the Gamma distribution given the observed mean \code{mu} and standard deviation \code{sigma}
##'
##' @param mu the desired mean of the Gamma distribution
##' @param sigma the desired standard deviation of the Gamma distribution
##'
##' @return a named numeric vector giving the \code{shape} and \code{rate} parameters of the Gamma distribution
##'
##' @author John Giles
##'
##' @example R/examples/gamma_params.R
##'
gamma.params <- function(mu, sigma) {
params <- optim(par=c(mu*2, 2),
fn=function(x) abs(mu - x[1]/x[2]) + abs(sigma - sqrt(x[1]/(x[2]^2))),
method='Nelder-Mead')$par
names(params) <- c('shape', 'rate')
return(params)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.