R/sim_funcs.R

##' 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)
}
gilesjohnr/genpatch documentation built on May 12, 2019, 10:50 a.m.