[R] density

David L Carlson dcarlson at tamu.edu
Thu Jul 26 23:50:57 CEST 2012


If you want a recommendation, why not use the one that comes with the manual
page for density():

?density

Under bw

"The default, "nrd0", has remained the default for historical and
compatibility reasons, rather than as a general recommendation, where e.g.,
"SJ" would rather fit, see also V&R (2002)."

Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S.
New York: Springer.

----------------------------------------------
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 Michael Young
> Sent: Wednesday, July 25, 2012 9:53 PM
> To: li li
> Cc: r-help
> Subject: Re: [R] density
> 
> 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
> > 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.



More information about the R-help mailing list