Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw the true density (for the specified gamma distribution) on histogram

Tags:

r

I want to draw the true density on histogram. Here is my code, I'm not sure if my codes are right or not.

set.seed(600)
x <- rgamma(500,shape=8,scale=0.1)
mean(x)
hist(x,prob=T,main='Gamma,scale=0.1')
lines(density(x),col='red',lwd=2)

Thanks!

like image 890
user2884661 Avatar asked Jan 12 '23 01:01

user2884661


1 Answers

density() gives a local estimate of the density. The true density can be derived from dgamma, and plotted like this...

lines( sort(x) , y = dgamma( sort(x) , shape = 8 , scale = 0.1 ) , col = "blue" , lty = 2 , lwd = 2 )

enter image description here

like image 184
Simon O'Hanlon Avatar answered Jan 17 '23 14:01

Simon O'Hanlon