Skip to content

Instantly share code, notes, and snippets.

@jwdink
Last active March 31, 2016 17:17
Show Gist options
  • Save jwdink/450d3f0dc511d2361eff to your computer and use it in GitHub Desktop.
Save jwdink/450d3f0dc511d2361eff to your computer and use it in GitHub Desktop.
Fit the gamma distribution to unscaled data using MASS::fitdistr
fitgamma <- function(x) {
# Equivalent to `MASS::fitdistr(x, densfun = "gamma")`, where x are first rescaled to
# the appropriate scale for a gamma distribution. Useful for fitting the gamma distribution to
# data which, when multiplied by a constant, follows this distribution
if (!requireNamespace("MASS")) stop("Requires MASS package.")
fit <- glm(formula = x ~ 1, family = Gamma)
out <- MASS::fitdistr(x * coef(fit), "gamma")
out$scaling_multiplier <- unname(coef(fit))
out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment