[R] density() with confidence intervals

Greg Snow Greg.Snow at imail.org
Fri Sep 3 21:42:34 CEST 2010


Here is a simple approach that uses bootstrapping (this could probably be improved by using better bootstrap estimates and not ignoring the dependence between points):

xx <- faithful$eruptions

fit1 <- density(xx)

fit2 <- replicate(10000, { x <- sample(xx, replace=TRUE); 
	density(x, from=min(fit1$x), to=max(fit1$x))$y } )

fit3 <- apply(fit2, 1, quantile, c(0.025,0.975) )

plot(fit1, ylim=range(fit3))
polygon( c(fit1$x, rev(fit1$x)), c(fit3[1,], rev(fit3[2,])), col='grey', border=F)
lines(fit1)


-- 
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 David Croll
> Sent: Friday, September 03, 2010 9:23 AM
> To: r-help at r-project.org
> Subject: [R] density() with confidence intervals
> 
> 
> 
> Hello R users & R friends,
> 
> 
> I just want to ask you if density() can produce a confidence interval,
> indicating how "certain" the density() line follows the true frequency
> distribution based on the sample you feed into density().
> 
> I've heard of loess.predict(loess(y ~ x), se=TRUE) which gives you a SE
> estimate of the smoothed scatterplot - but density() kernel smoothing
> is not the same as locally-weighted polynomial scatterplot smoothing...
> 
> 
> Feel free to ask me if I did not put my question into clear words :)
> 
> 
> Kind regards & thanks in advance,
> 
> 
> David
> --
> Sicherer, schneller und einfacher. Die aktuellen Internet-Browser -
> jetzt kostenlos herunterladen! http://portal.gmx.net/de/go/chbrowser
> 
> ______________________________________________
> 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.



More information about the R-help mailing list