0
$\begingroup$

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

enter image description here

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 - enter image description here

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
$\endgroup$

1 Answer 1

0
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – Glen_b
    Commented Sep 27, 2017 at 7:11
  • $\begingroup$ @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. $\endgroup$
    – Eran
    Commented Sep 27, 2017 at 7:17
  • $\begingroup$ @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 $\endgroup$ Commented Sep 27, 2017 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.