# [R] density ranges for uniform law

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Tue Apr 8 11:40:50 CEST 2003

```On 08-Apr-03 ZABALZA-MEZGHANI Isabelle wrote:
> samp <- runif(10000,-1,1)
> plot(density(samp))
>
> Instead of varying between -1 and 1, the density varies between
> approximaly -1.5 and 1.5
> Could someone explain me what is happening ? Maybe some arguments for
> density estimation need to be set ?

density() computes a kernel-density estimate of the density, i.e.
it replaces each observation by a distribution ("kernel") which is
spread out over a certain width on either side of it, and sums these
contributions. Therefore, observations near the ends of the range [-1,1]
are replaced by distributions which extend beyond the range, with the
result you have seen.

There are options to density() which can limit the estimated density
to the range [-1,1]: try

plot(density(samp,from=-1,to=1))

or

plot(density(samp,cut=0))

(which both seem to give the same result), though you may not think that
the result looks satisfactory at the ends.

Ideally, for this sort of problem is should be possible to make the width
of the kernel depend on the position (rank) of the observation it is
applied to -- for a uniform distribution in particular the variance
of an order statistic is strongly dependent on its rank (the median
over [-1,1] has variance 1/(n+2), the min or the max has variance
4n/((n+2)*(n+1)^2) approx = 4/(n^2) for a sample of n). If you know
that a sample is from a uniform distribution, the end-points are
very precisely estimated from the extremes of the sample, and a
fixed-width kernel-density estimate will not do justice to this..

I don't know whether this is possible directly with current R functions
(though one can always write one which does it).

I hope this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 08-Apr-03                                       Time: 10:40:50
------------------------------ XFMail ------------------------------

```