[R] Histogram to KDE

David L Carlson
2012-08-31T16:15:53Z

Using a data.frame x with columns bins and counts:

x <- structure(list(bins = c(3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5,
    11.5, 12.5, 13.5, 14.5, 15.5), counts = c(1, 1, 2, 3, 6, 18,
    19, 23, 8, 10, 6, 2, 1)), .Names = c("bins", "counts"),
row.names 4:16,
    class = "data.frame")

This will give you a plot of the kde estimate:

xkde <- density(rep(bins, counts), bw="SJ")
plot(xkde)

As for the standard error or the confidence interval, you would probably
need to use bootstrapping.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of firdaus.janoos
> Sent: Friday, August 31, 2012 9:52 AM
> To: r-help at r-project.org
> Subject: [R] Histogram to KDE
>
> Hello,
> I wanted to know if there was way to convert a histogram of a data-set
> to a
> kernel density estimate directly in R ?
>
> Specifically, I have a histogram [bins, counts] of samples {X1 ...
> XN} of a quantized variable X where there is one bin for each level of
> X,
> and I'ld like to directly get a kde estimate of the pdf of X from the
> histogram. Therefore, there is no additional quantization of X in the
> histogram. Most KDE methods in R seem to require the original sample
> set   - and I would like to avoid re-creating the samples from the
> histogram. Is there some quick way of doing this using one of the
> standard
> kde methods in R ?
>
> Also, a general statistical question - is there some measure of the
> standard error or confidence interval or similar of a KDE of a data-set
> ?
>
> Thanks,
> -fj
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.