# [R] density at particular values

(Ted Harding) ted.harding at wlandres.net
Sun Nov 21 20:39:46 CET 2010

```On 21-Nov-10 19:11:20, William Dunlap wrote:
>> -----Original Message-----
>> From: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r-project.org] On Behalf Of Shant Ch
>> Sent: Saturday, November 20, 2010 6:34 PM
>> To: David Winsemius
>> Cc: r-help at r-project.org
>> Subject: Re: [R] density at particular values
>>
>> David, I did look at ?density many times. I think I didn't
>> explain what I have to find.
>>
>> Suppose I have a data.
>> x<- c(rnorm(40,5,3),rcauchy(30,0,4),rexp(30,6))
>>
>> Suppose I don't have information about the mixture, I have
>> been given only the data.
>>
>> density(x) will give the 6 number summary of the data, given
>> as x and also the 6 number summary of the density of density
>> given as y.
>
> print(density(x)) displays a 6-number summary of the
> x and y outputs of density, which are only tangentially
> related to the data.  (The default x output is a evenly
> spaced seqence of n=512 values covering the range of
> the input x.)  I don't know why this display was chosen.
>
>> I want to find the density of the given data at x=1. I
>> basically want the value
>> of y(=density) for x=1 i.e. kernel density at x=1.
>
> If you really just want the density at a single
> point, x0 (=1 in your case), you can do
>    d0 <- density(x, from=x0, to=x0, n=1)\$y
> (density only lets you choose the output x vector
> as an evenly spaced sequence parameterized by
> from, to, and n.)
>
> Bill Dunlap

Almost, but not quite! (Perhaps ... ). For example:

set.seed(54321)
x<- c(rnorm(40,5,3),rcauchy(30,0,4),rexp(30,6)) # Shant Ch's mixture
min(x)
#  -33.32023
max(x)
#  716.6736
d <- density(x,from=(-34),to=720,n=755)
c(d\$x,d\$y)  ## Using density() on the full range of data
#  1.0000000 0.1125064

density(x,from=1,to=1,n=1)\$y ## William's targeted density()
#  0.1136818

So there's about 1% difference in this example. Of course, what the
answer *should* be is debatable, and 1% may be well within the
debatable range, and William's 0.1136818 may be just as acceptable
as my 0.1125064 (and that's without getting into issues of bandwidth
and so on). I'm just putting it up to illustrate that you should not
expect a "unique" answer to this question: what the answer is depends
on how you set about finding it!

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 21-Nov-10                                       Time: 19:39:41
------------------------------ XFMail ------------------------------

```