[R] Density estimate with bounds

Justine Rochon rochon at gmx.de
Thu Nov 5 11:28:39 CET 2009


Hi Gavin,

I have already played around with from, to. It seems, however, that
from=0, to=1 simply crops the density curve at 0 and 1:

x = runif(1000)
fx = density(x)
fxc = density(x, from=0, to=1)
plot(fx)
lines(fxc, col=2, lwd=2)

Unfortunately, this 'cropped' density does not integrate to 1
anymore:

 > sum(fx$y)*(fx$x[2]-fx$x[1])
[1] 1.000934
 > sum(fxc$y)*(fxc$x[2]-fxc$x[1])
[1] 0.9539383

I am naively thinking about a density estimator which is aware
of such bounds.

Best wishes,

Justine

-------- Original-Nachricht --------
> Datum: Thu, 05 Nov 2009 09:44:36 +0000
> Von: Gavin Simpson <gavin.simpson at ucl.ac.uk>
> An: Justine Rochon <rochon at gmx.de>
> CC: r-help at r-project.org
> Betreff: Re: [R] Density estimate with bounds

> On Thu, 2009-11-05 at 10:37 +0100, Justine Rochon wrote:
> > Dear R users,
> > 
> > I would like to show the estimated density of a (0, 1) uniformly
> > distributed random variable. The density curve, however, goes beyond 0
> > and 1 because of the kernel smoothing. 
> > 
> > Example:
> > 
> > x = runif(10000)
> > plot(density(x))
> > 
> > Is there a way to estimate the density curve strictly within (0, 1)
> > and still use some sort of smoothing?
> 
> Read ?density and in particular the arguments 'from' and 'to':
> 
>  from,to: the left and right-most points of the grid at which the
>           density is to be estimated; the defaults are 'cut * bw'
>           outside of 'range(x)'.
> 
> 
> HTH
> 
> G
> 
> > 
> > Any help would be greatly appreciated.
> > 
> > Best regards,
> > 
> > Justine Rochon
> > 
> -- 
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>  Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
>  ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
>  Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
>  Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
>  UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> 

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!




More information about the R-help mailing list