Christos Varvarrigos
Christos Varvarrigos

Reputation: 61

Fitting Curve of Gamma Distribution in Hist

When I run this:

x<-c(73,6,77,81,91,120,150,61,65,68,18,20,23,12,14,18,23,26,26,27,2,3,3,40,41,41,6,10,11,12,37,38,38,6,73,6,51)
a<-1.286486;b<-30.59584

hist(x,breaks=c(0,20,40,60,80,100,120,160),probability = T,xaxt="n")
curve(dgamma(x,a,b),from=0,to=160,col="red",lwd=2,add=T)

It should generate the curve of the Gamma Distribution on the histogram. Instead, it just makes a flat line along the x-axis.

enter image description here

What am I doing wrong here?

Upvotes: 3

Views: 1304

Answers (2)

Dave2e
Dave2e

Reputation: 24089

Your coefficients to the model are incorrect. The rate parameter should be closer to 0.03.

x<-c(73,6,77,81,91,120,150,61,65,68,18,20,23,12,14,18,23,26,26,27, 2,3,3,40,41,41,6,10,11,12,37,38,38,6,73,6,51)

library(fitdistrplus)
model<-fitdist(x, "gamma")
print(model$estimate)
#    shape      rate 
#1.1911710 0.0311047 
a=model$estimate[1]
b=model$estimate[2]    

h<-hist(x,breaks=c(0,20,40,60,80,100,120,160),probability = T)
curve(dgamma(x,a,b),from=0,to=160,col="red",lwd=2,add=T)

enter image description here

Upvotes: 3

Marco De Virgilis
Marco De Virgilis

Reputation: 1089

It appears that your x vector doesn't contain the realizations of gamma distribution with those parameters (a,b). Try this:

a<-1.286486
b<-30.59584
num<-rgamma(1000,a,b)
hist(num,nclass = 100,freq=F)
curve(dgamma(x,a,b),col="red",lwd=2,add=T)

Upvotes: 1

Related Questions