Skip to content

Instantly share code, notes, and snippets.

@stemangiola
Created March 19, 2019 10:04
Show Gist options
  • Save stemangiola/c8aef6237c7574e42a49a2be48dd6a66 to your computer and use it in GitHub Desktop.
Save stemangiola/c8aef6237c7574e42a49a2be48dd6a66 to your computer and use it in GitHub Desktop.
generate_gamma_sum <- function(N, k, theta) {
n_means = length(k)
sums = numeric(N)
for(n in 1:N) sums[n] = sum(rgamma(n_means, shape = k, scale = theta))
list(sums = sums, shape = k, scale = theta)
}
k=c(3,4,5)
theta=c(1, 2, 1)
generate_gamma_sum(10000, c(3,4,5), c(1, 2, 1)) %$%
sums %>%
MASS::fitdistr("gamma")
k_sum = sum(theta*k)^2/sum(theta^2*k)
theta_sum = sum(k*theta)/k_sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment