Gordon Sands
Gordon Sands

Reputation: 11

Finding the peak on a density plot

I've created a series of numbers using the rbeta function.

set.seed(123)
n = 100000
p1.12.2 = rbeta(n, 0.3225928, 1.2903712)
p4.7.2 = (rbeta(n, 0.3488823,  3.1399407)^2)
E2 = p4.7.2*p1.12.2

This runs fine but I would like to find the mode of E2, so I've done this by getting the peak of the density plot.

d = density(E2)
i = which.max(d$y)
M2 = d$x[i]
M2

I keep getting a negative value for the mode. But the beta distribution is confined to 0-1. Any ideas where the negative values are coming from or is there another way to get the mode of a bin?

Upvotes: 1

Views: 3453

Answers (1)

AntoniosK
AntoniosK

Reputation: 16121

I think this is a "problem" due to how kernel density estimates work. What about approximating your peak value using a histogram and specifying a large number of breaks?

h = hist(E2, breaks=500)
i = which.max(h$counts)
M2 = h$mids[i]
M2

Try different values of breaks.

Upvotes: 2

Related Questions