# [R] How to find maximum values on the density function of arandom variable

Bert Gunter gunter.berton at gene.com
Fri Mar 13 04:22:22 CET 2009

```There is some considerable confusion in both the question and the reply.

rv is **not**  a random variable. It is an (iid) sample from (i.e. a
"realization" of) a random variable. It has *no* "density function" and the
density() function is simply a procedure to **estimate** the density of the
underlying random variable from which rv was sampled at a finite number of
points. The result of density()and the max given in the reply will depend on
the particular parameters given to density()(see ?density for details), as
well as the data. In other words, both the question and answer posted are
nonsense.

Now let me contradict what I just said. **If** you consider rv a finite,
discrete distribution (i.e. the whole population), then, in fact, it does
have a discrete density, with point mass j(i)/n at each unique sample value
i, where n is the total sample size (= 10000 in the example) and j(i) is the
number of samples values == i, which would probably be 1 for all i. Then, of
course, one can talk about the density of this finite distribution in the
obvious way and its maximum or maxima, occur at those i for which n(i) is
largest.

But of course that's not what the poster really meant, so that brings us
back to the nonsense question and answer. What James probably meant to ask
was: "How can the maximum of the underlying population density function be
estimated?" Well, that's a complicated issue. One could, of course, use some
sort of density estimate -- there are tons -- and find its max; that was the
approach taken in the answer, but it's not so simple as it appears because
of the need to choose the **appropriate** estimate (including the parameters
of the statistical algorithm doing the estimating ). This is the sort of
thing that actually requires some careful thought and statistical expertise.
You will find, I believe, that the prescription for finding the max
suggested below can give quite different answers depending on the parameters
chosen for this estimate, and on the estimate used. So if you need to do
this right, may I suggest consulting the literature on density estimation or
perhaps talking with your local statistician?

-- Bert Gunter
Genentech Nonclinical Statistics

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Mike Lawrence
Sent: Thursday, March 12, 2009 5:40 PM
To: guox at ucalgary.ca
Cc: r-help at r-project.org
Subject: Re: [R] How to find maximum values on the density function of
arandom variable

rv <- rbinom(10000,1,0.1) + rnorm(10000)

d.rv = density(rv)
d.x = d.rv\$x
d.y = d.rv\$y

d.rv.max = d.rv\$x[which.max(d.rv\$y)]

plot(d.rv)
abline(v=d.rv.max)

#that what you want?

On Thu, Mar 12, 2009 at 6:28 PM,  <guox at ucalgary.ca> wrote:
> I would like to find the maximum values on the density function of a
> random variable. For example, I have a random variable
>
> rv <- rbinom(10000,1,0.1) + rnorm(10000)
>
> Its density function is given by density(rv) and can be displayed by
> plot(density(rv)). How to calculate its maximum values?
> A density function may have a few (global and local) maximum values.
> -james
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

--
Mike Lawrence
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tinyurl.com/mikes-public-calendar

~ Certainty is folly... I think. ~

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help