# draw histogram by hand and then calculate probability density function from that

I have an array of data - arr = c(1,2,3,4,5,6,1,2,3,4). I draw a histogram and get the following graph

The frequency of 1,2,3,4 is going to be 2 for each of those numbers, but the graph shows different result — it shows 4 for the bin between 1 and 2. Can someone explain why ?

Also, if I plot the probability density function — how are the y-axis and x-axis values for the density plot calculated ?

Finally, I got the graph as expected with the following code -

h<-hist(x, breaks=10, col="red", xlab="random numbers",
+ main="Histogram with Normal Curve")
xfit<-seq(min(x),max(x),length=40)
yfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
yfit <- yfit*diff(h$mids[1:2])*length(x) lines(xfit, yfit, col="blue", lwd=2) xfit  ## 1 Answer The plot you've created is not a general PDF (probability density function), but a KDE (Kernel density estimation) which assumes the distribution is made of 1 or more normal distribution centres (kernels). Read here to see how it's calculated. • The KDE is certainly a valid density function; it won't be the density from which the data were drawn if course, but if the bandwidth changes with$n\$ in a suitable way it will be a consistent estimator of it. Commented Sep 27, 2017 at 7:11
• @Glen_b, I never implied it wasn't suitable. The point was to explain how this distribution is estimated. It seems like the OP assumed that this plotting is the PDE by definition. Now he knows it's only a specific type, albeit very usable.
– Eran
Commented Sep 27, 2017 at 7:17
• @Eran Yes thanks for the catch - I plotted the correct distribution curve now - and when I added the breaks parameter to the hist function - it gave the results as expect - thank you Commented Sep 27, 2017 at 7:35