I am using fitdist to fit a gamma distribution based on this link: How to draw fitted graph and actual graph of gamma distribution in one plot?

My code is as follows:

# save density function of 
den <- density(brain$freq_1)
dat <- data.frame(x = den$x, y = den$y)
ggplot(data = dat, aes(x = x, y = y)) + 
  geom_point(size = 1) +

enter image description here

# fit gamma dist:
fit.params <- fitdistr(x, "gamma", lower = c(0, 0))

         Length Class  Mode   
estimate 2      -none- numeric
sd       2      -none- numeric
vcov     4      -none- numeric
loglik   1      -none- numeric
n        1      -none- numeric

# plot density and fitted gamma dist:

ggplot(data = dat, aes(x = x,y = y)) + 
  geom_point(size = 1) +     
  geom_line(aes(x=dat$x, y=dgamma(dat$x,fit.params$estimate["shape"], fit.params$estimate["rate"])), color="red", size = 1) + theme_classic()

[enter image description here]

My question is why do I get this strange looking gamma distribution peaking out at zero?

  • $\begingroup$ Presumably because your data have a very heavy right tail (strong right skew) -- if you fit a gamma, that implies a very peaked distribution. What does a histogram of the log of your variable look like? $\endgroup$ – Glen_b -Reinstate Monica Feb 20 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.