Skip to content

Instantly share code, notes, and snippets.

@cbrown5
Created February 16, 2021 23:07
Show Gist options
  • Save cbrown5/04b8688d23ef5cd1649f6878b6ef1d97 to your computer and use it in GitHub Desktop.
Save cbrown5/04b8688d23ef5cd1649f6878b6ef1d97 to your computer and use it in GitHub Desktop.
Parameterize gamma distribution with mode and SD
#Parameterize gamma distribution with mode and SD
#Choose summary stats for the gamma
xmode <- 1 #mode of distribution
xsd <- 0.5 # SD
#Calculate gamma parameters
beta <- (xmode + sqrt(xmode^2 + 4*xsd^2 ) ) / ( 2 * xsd^2 )
alpha <- 1 + xmode * beta
#Plot gamma
x <- seq(0.01, 4, length.out = 1000)
fx <- dgamma(x, shape = alpha, rate = beta)
#NB stan language is same parameterization as R defaults: shape and rate
plot(x, fx, type = "l")
abline(v = FP_mode)
rx <- rgamma(10000, shape = alpha, rate = beta)
sd(rx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment