Convert gamma mean and dispersion parameters to shape and rate parameters

gammaGetShapeRate(mean, dispersion)

Arguments

mean

The mean of a gamma distribution

dispersion

The dispersion parameter of a gamma distribution

Value

A list that includes the shape and rate parameters of the gamma distribution

Details

In simstudy, users specify the gamma distribution as a function of two parameters - a mean and dispersion. In this case, the variance of the specified distribution is (mean^2)*dispersion. The base R function rgamma uses the shape and rate parameters to specify the gamma distribution. This function converts the mean and dispersion into the shape and rate.

Examples

set.seed(12345)
mean <- 5
dispersion <- 1.5
rs <- gammaGetShapeRate(mean, dispersion)
c(rs$shape, rs$rate)
#> [1] 0.6666667 0.1333333
vec <- rgamma(1000, shape = rs$shape, rate = rs$rate)
(estMoments <- c(mean(vec), var(vec)))
#> [1]  4.806329 36.893163
(theoryMoments <- c(mean, mean^2 * dispersion))
#> [1]  5.0 37.5
(theoryMoments <- c(rs$shape / rs$rate, rs$shape / rs$rate^2))
#> [1]  5.0 37.5