[R] Antw: Re: Density estimate with bounds
Giovanni Petris
GPetris at uark.edu
Thu Nov 5 20:33:44 CET 2009
What is the problem that you see?
(I think the trouble is that Laplace density is not smooth at x=0,
while a kernel density estimate returns a smooth density)
It would help to know what you are trying to achieve with this
exercise...
Giovanni
> Date: Thu, 05 Nov 2009 14:36:44 +0100
> From: Justine Rochon <Justine.Rochon at klinik.uni-regensburg.de>
> Sender: r-help-bounces at r-project.org
> Cc: r-help at r-project.org
> Precedence: list
>
> Hi Duncan,
>
> Thank you for your e-mail.
>
> It works for the uniform distribution, but I have trouble with the exponential
> distribution:
>
> x <- rexp(10000)
> ex_x <- c(-x, x)
> den <- density(ex_x)
> plot(den$x, 2*den$y, xlim=c(0,5), type="l")
>
> Best regards,
>
> Justine
>
>
>
>
>
>
> ________________________
> Justine Rochon
> - Biostatistician -
> Center for Clinical Studies
> University Hospital Regensburg
> Franz-Josef-Strauß-Allee 11
> D-93053 Regensburg
> Phone: ++49-(0)941-944-5626
> Fax: ++49-(0)941-944-5632
> Email: justine.rochon at klinik.uni-regensburg.de
>
>
> >>> Duncan Murdoch <murdoch at stats.uwo.ca> 05.11.2009 12:36 >>>
> On 05/11/2009 4:35 AM, 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?
> >
> > Any help would be greatly appreciated.
>
> One way is to extend the data by reflection on each end. That is,
>
> x <- runif(10000)
> ex_x <- c(-x, x, 2-x)
> den <- density(ex_x)
> plot(den$x, 3*den$y, xlim=c(0,1), type="l")
>
> You need the rescaling to 3*den$y because you've tripled the range.
>
> Duncan Murdoch
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
--
Giovanni Petris <GPetris at uark.edu>
Associate Professor
Department of Mathematical Sciences
University of Arkansas - Fayetteville, AR 72701
Ph: (479) 575-6324, 575-8630 (fax)
http://definetti.uark.edu/~gpetris/
More information about the R-help
mailing list