Re: [R] density plot of simulated exponential distributed data

From: Breheny, Patrick <patrick.breheny_at_uky.edu>
Date: Wed, 27 Apr 2011 16:40:09 -0400

There is an extensive statistical literature on how to correct for boundary bias in kernel density estimates. See, for example,

An Improved Estimator of the Density Function at the Boundary S. Zhang, R. J. Karunamuni and M. C. Jones Journal of the American Statistical Association Vol. 94, No. 448 (Dec., 1999), pp. 1231-1241

And the references therein. However, a very simple -- certainly not optimal -- fix is to reflect the data about the origin before fitting the density:

y <- rexp(100)
yy <- c(y, -y)
dens <- density(yy)
dens$y <- 2*dens$y[dens$x >=0]
dens$x <- dens$x[dens$x >= 0]
plot(dens,col="red")
lines(density(y),col="gray")



Patrick Breheny
Assistant Professor
Department of Biostatistics
Department of Statistics
University of Kentucky

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Greg Snow Sent: Wednesday, April 27, 2011 1:55 PM
To: Juanjuan Chai; r-help_at_r-project.org
Subject: Re: [R] density plot of simulated exponential distributed data

You might want to use the logspline package instead of the density function, it allows you to specify bounds on a distribution.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow_at_imail.org
801.408.8111



> -----Original Message-----
> From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-
> project.org] On Behalf Of Juanjuan Chai
> Sent: Tuesday, April 26, 2011 4:19 PM
> To: r-help_at_r-project.org
> Subject: [R] density plot of simulated exponential distributed data
>
> Hi all,
>
> I tried to plot the density curve using the data from simulation. I am
> sure that the data should be exponentially distributed, but the plot
> of density curve always starts from (0,0) which is not the case for
> exponential distribution. Is there any way around this, to keep the
> curve dropping at 0?
>
> Thanks.
>
> The following are the codes I tested:
>
> data <- vector()
> rate <- 3
> i <- 1
> for(i in 1:1000){
> r <- runif(1)
> data[i] <- log(1-r)/(-rate)
> i <- i+1
> }
> plot(density(data))
>
> -JJ
>
> ______________________________________________
> 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.
______________________________________________ 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. ______________________________________________ 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.
Received on Wed 27 Apr 2011 - 20:43:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 27 Apr 2011 - 20:50:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive