[R] Help interpreting density().

Bill.Venables at csiro.au Bill.Venables at csiro.au
Tue Jul 29 08:12:56 CEST 2008

You should read the documentation more carefully.  The bw is not
"essentially the sd".  To quote the documentation the bw is "the
smoothing bandwidth to be used. The kernels are scaled such that this is
the standard deviation of the smoothing kernel."  That is a very
different thing.

You are confusing the standard deviation of the distribution with the
standard deviation of the gaussian smoothing kernels.  

In the second case, density(rpois(1000, 0)), you are getting the kernel
density for a sample of 1000 zeros.  So there is just one distinct
smoothing kernel and the bw is a default used for this case.  If you 

plot(density(rpois(1000, 0)))

you will see what that smoothing kernel looks like.

Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary):  +61 7 3826 7304
Mobile:                         +61 4 8819 4402
Home Phone:                     +61 7 3286 7700
mailto:Bill.Venables at csiro.au

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of rkevinburton at charter.net
Sent: Tuesday, 29 July 2008 2:15 PM
To: r-help at r-project.org
Subject: [R] Help interpreting density().

I issue the following:

> d <- density(rnorm(1000))
> d

and get:

        density.default(x = rnorm(1000))

Data: rnorm(1000) (1000 obs.);  Bandwidth 'bw' = 0.2235

       x                 y            
 Min.   :-3.5157   Min.   :2.416e-05  
 1st Qu.:-1.6892   1st Qu.:1.129e-02  
 Median : 0.1373   Median :7.267e-02  
 Mean   : 0.1373   Mean   :1.367e-01  
 3rd Qu.: 1.9639   3rd Qu.:2.693e-01  
 Max.   : 3.7904   Max.   :4.014e-01  

The documentation indicates that the bw is essentially the sd. Yet I
have specified an sd of 1? How am I to interpret the ranges of the
values? x ranges almost from -4 to +4 and y ranges from 0 to 0.4. The
mean x is .1 which isn't too awfully close to what I would expect (0.0).
Then there is:

> d <- density(rpois(1000,0))
> d

        density.default(x = rpois(1000, 0))

Data: rpois(1000, 0) (1000 obs.);       Bandwidth 'bw' = 0.2261

       x                 y          
 Min.   :-0.6782   Min.   :0.01979  
 1st Qu.:-0.3391   1st Qu.:0.14073  
 Median : 0.0000   Median :0.57178  
 Mean   : 0.0000   Mean   :0.73454  
 3rd Qu.: 0.3391   3rd Qu.:1.32830  
 Max.   : 0.6782   Max.   :1.76436  

Here I am getting the mean that I expect from a Poisson distribuition
but y ranges from 0 to 1.75. Again I am not sure what these numbers
mean. How can I map the output to the standard distirbution description

Thank you.


R-help at r-project.org mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list