# [R] density

Michael Young michaeltyoung at gmail.com
Thu Jul 26 04:52:38 CEST 2012

```I can't help you decide which bandwidth method to use, but here's how
you view the density source code...

methods("density")
density.default

On Wed, Jul 25, 2012 at 5:56 PM, li li <hannah.hlx at gmail.com> wrote:
>
> Hi all,
>   I have a question regarding the density function which gives the
> kernel density estimator.
>   I want to decide the bandwidth when using gaussian kernel, given a set
> of
> observations. I am not familiar with different methods for  bandwidth
> determination.  Below are the different ways in R on deciding the
> bandwidth.
> Can anyone give an idea on which ones are preferred.
>   Also, how can I take a look at the source code for the density function?
>   Thank you very much.
>         Hannah
>
>
> x <- rnorm(1000)
>
> > bw.nrd(x)
>
> [1] 0.2688588
>
> > bw.nrd0(x)
>
> [1] 0.2282763
>
> > bw.ucv(x)
>
> [1] 0.2112366
>
> > bw.bcv(x)
>
> [1] 0.2890085
>
> Warning message:
>
> In bw.bcv(x) : minimum occurred at one end of the range
>
> > bw.SJ(x)
>
> [1] 0.2716242
>
> > density(x, give.Rkern=T, kernel="gaussian")
>
> [1] 0.2820948
>
> > density(x, kernel="gaussian")
>
>
> Call:
>
> density.default(x = x, kernel = "gaussian")
>
>
> Data: x (1000 obs.); Bandwidth 'bw' = 0.2283
>
>
>        x                   y
>
>  Min.   :-3.974672   Min.   :0.0000199
>
>  1st Qu.:-1.987712   1st Qu.:0.0076405
>
>  Median :-0.000752   Median :0.0529498
>
>  Mean   :-0.000752   Mean   :0.1256971
>
>  3rd Qu.: 1.986208   3rd Qu.:0.2552411
>
>  Max.   : 3.973168   Max.   :0.3883532
>
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help