Re: [R] density at particular values

2010-11-21 Thread Ted Harding
On 21-Nov-10 19:11:20, William Dunlap wrote:
>> -Original Message-
>> From: r-help-boun...@r-project.org 
>> [mailto:r-help-boun...@r-project.org] On Behalf Of Shant Ch
>> Sent: Saturday, November 20, 2010 6:34 PM
>> To: David Winsemius
>> Cc: r-help@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)
  # [1] -33.32023
  max(x)
  # [1] 716.6736
  d <- density(x,from=(-34),to=720,n=755)
  c(d$x[36],d$y[36])  ## Using density() on the full range of data
  # [1] 1.000 0.1125064

  density(x,from=1,to=1,n=1)$y ## William's targeted density()
  # [1] 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) 
Fax-to-email: +44 (0)870 094 0861
Date: 21-Nov-10   Time: 19:39:41
-- XFMail --

__
R-help@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.


Re: [R] density at particular values

2010-11-21 Thread William Dunlap

> -Original Message-
> From: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org] On Behalf Of Shant Ch
> Sent: Saturday, November 20, 2010 6:34 PM
> To: David Winsemius
> Cc: r-help@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
Spotfire, TIBCO Software
wdunlap tibco.com  

> 
> Shant
>  
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> ________________
> From: David Winsemius 
> 
> Cc: r-help@r-project.org
> Sent: Sat, November 20, 2010 8:54:32 PM
> Subject: Re: [R] density at particular values
> 
> 
> On Nov 20, 2010, at 8:07 PM, Shant Ch wrote:
> 
> > Hello everyone!
> > 
> > I want to use density function of R to compute the density 
> at x(=0, say). But 
> >it
> > is giving me the 5-number summary and mean of the data and 
> densities at that
> > point.
> > I just want the densities at different values specified by 
> me. Can anyone let 
> >me
> > know how can I find that?
> 
> Here's what you should have done (even before posting):
> 
> ?density
> 
> "Value
> x   the n coordinates of the points where the density is estimated.
> 
> y   the estimated density values. These will be non-negative, 
> but can be zero."
> 
> Realize that the "specified by me" part is either going to be 
> modified to "pick 
> an existing estimate near my specification" or that you will 
> need to approximate 
> the value. So what is the actual problem (and the actual data setup) ?
> 
> --David.
> 
> 
> > 
> > For example
> > 
> > 
> > Thanks in advance for your help.
> > 
> > 
> > Shant
> > 
> > 
> > 
> > 
> > [[alternative HTML version deleted]]
> > 
> > __
> > R-help@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.
> 
> David Winsemius, MD
> West Hartford, CT
> 
> 
>   
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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@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.


Re: [R] density at particular values

2010-11-20 Thread Bill.Venables
It's actually not too difficult to write the density function itself as 
returning a function rather than a list of x and y values.  Here is a no frills 
(well, few frills) version:

### cut here ###
densityfun <- local({
  normd <- function(value, bw) {
force(value); force(bw)
function(z) dnorm(z, mean = value, sd = bw)
  }
  function(x, bw = bw.nrd0, adjust = 1) {
bandw <- bw(x) * adjust
flist <- lapply(x, normd, bw = bandw)
function(z)
if(length(z) <= 1)
mean(sapply(flist, function(fun) fun(z)))
else rowMeans(sapply(flist, function(fun) fun(z)))
  }
})
### cut here ###

To test it:


library(MASS)
x <- faithful$eruptions
dx <- density(x, n = 500)
plot(dx)
dfun <- densityfun(x)
sx <- sample(dx$x, 25)
points(sx, dfun(sx), pch = 4)
curve(dfun, add = TRUE, col = "blue", n = 500)


This idea could be extneded to provide essentially the same features as 
density() itself.  The details are left as an exercise...  If anyone ever needs 
to integrate a kernel density estimate, (and for now I can't see why they 
would, but if), then this would provide a way to do it with integrate().

Bill Venables.


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of David Winsemius
Sent: Sunday, 21 November 2010 3:21 PM
To: Shant Ch
Cc: r-help@r-project.org
Subject: Re: [R] density at particular values


On Nov 20, 2010, at 9:34 PM, Shant Ch wrote:

> 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.

I am not sure what the number six (6) represents in the two palces it  
occurs:

 > str(density(x))
List of 7
  $ x: num [1:512] -59.2 -59 -58.8 -58.5 -58.3 ...
  $ y: num [1:512] 3.30e-05 5.27e-05 8.19e-05 1.24e-04  
1.82e-04 ...
  $ bw   : num 1.37
  $ n: int 100
  $ call : language density.default(x = x)
  $ data.name: chr "x"
  $ has.na   : logi FALSE
  - attr(*, "class")= chr "density"

Perhaps you want to look at:

?approxfun

It would let you interpolate at points that are not part of the set  
density(x)$x

__
David.


>
> 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.
>
> Shant
>
>
>
>
>
>
>
>
>
>
>
> From: David Winsemius 
> To: Shant Ch 
> Cc: r-help@r-project.org
> Sent: Sat, November 20, 2010 8:54:32 PM
> Subject: Re: [R] density at particular values
>
>
> On Nov 20, 2010, at 8:07 PM, Shant Ch wrote:
>
> > Hello everyone!
> >
> > I want to use density function of R to compute the density at  
> x(=0, say). But it
> > is giving me the 5-number summary and mean of the data and  
> densities at that
> > point.
> > I just want the densities at different values specified by me. Can  
> anyone let me
> > know how can I find that?
>
> Here's what you should have done (even before posting):
>
> ?density
> 
> "Value
> x  the n coordinates of the points where the density is estimated.
>
> y  the estimated density values. These will be non-negative, but can  
> be zero."
>
> Realize that the "specified by me" part is either going to be  
> modified to "pick an existing estimate near my specification" or  
> that you will need to approximate the value. So what is the actual  
> problem (and the actual data setup) ?
>
> --David.
>
>
> >
> > For example
> >
> >
> > Thanks in advance for your help.
> >
> >
> > Shant
> >
> >
> >
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@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.
>
> David Winsemius, MD
> West Hartford, CT
>
>
>

David Winsemius, MD
West Hartford, CT

__
R-help@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@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.


Re: [R] density at particular values

2010-11-20 Thread David Winsemius


On Nov 20, 2010, at 9:34 PM, Shant Ch wrote:

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.


I am not sure what the number six (6) represents in the two palces it  
occurs:


> str(density(x))
List of 7
 $ x: num [1:512] -59.2 -59 -58.8 -58.5 -58.3 ...
 $ y: num [1:512] 3.30e-05 5.27e-05 8.19e-05 1.24e-04  
1.82e-04 ...

 $ bw   : num 1.37
 $ n: int 100
 $ call : language density.default(x = x)
 $ data.name: chr "x"
 $ has.na   : logi FALSE
 - attr(*, "class")= chr "density"

Perhaps you want to look at:

?approxfun

It would let you interpolate at points that are not part of the set  
density(x)$x


__
David.




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.


Shant











From: David Winsemius 
To: Shant Ch 
Cc: r-help@r-project.org
Sent: Sat, November 20, 2010 8:54:32 PM
Subject: Re: [R] density at particular values


On Nov 20, 2010, at 8:07 PM, Shant Ch wrote:

> Hello everyone!
>
> I want to use density function of R to compute the density at  
x(=0, say). But it
> is giving me the 5-number summary and mean of the data and  
densities at that

> point.
> I just want the densities at different values specified by me. Can  
anyone let me

> know how can I find that?

Here's what you should have done (even before posting):

?density

"Value
x  the n coordinates of the points where the density is estimated.

y  the estimated density values. These will be non-negative, but can  
be zero."


Realize that the "specified by me" part is either going to be  
modified to "pick an existing estimate near my specification" or  
that you will need to approximate the value. So what is the actual  
problem (and the actual data setup) ?


--David.


>
> For example
>
>
> Thanks in advance for your help.
>
>
> Shant
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.

David Winsemius, MD
West Hartford, CT





David Winsemius, MD
West Hartford, CT

__
R-help@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.


Re: [R] density at particular values

2010-11-20 Thread Jorge Ivan Velez
Hi Shant,

You could use approxfun() on the density as follows:

   > # data

> set.seed(123)

> x <- c(rnorm(40,5,3),rcauchy(30,0,4),rexp(30,6))

> d <- density(x)

> str(d)

List of 7

 $ x: num [1:512] -115 -115 -115 -114 -114 ...

 $ y: num [1:512] 3.24e-05 6.99e-05 1.40e-04 2.58e-04 4.43e-04 ...

 $ bw   : num 1.62

 $ n: int 100

 $ call : language density.default(x = x)

 $ data.name: chr "x"

 $ has.na   : logi FALSE

 - attr(*, "class")= chr "density"

>

> # showing values around x = 1

> # here, "y" is the density

> with(d, cbind(x, y))[d$x > 0 & d$x < 1.5,]

 x y

[1,] 0.3489268 0.1084530

[2,] 0.8031464 0.1070246

[3,] 1.2573661 0.1015048

>

> # function to estimate the density at x0

> f <- with(d,  approxfun(x, y))

>

> # estimating f at x0 = 1

> f(1)

[1] 0.1046324


HTH,
Jorge

On Sat, Nov 20, 2010 at 9:34 PM, Shant Ch <> wrote:

> 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.
>
> 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.
>
> Shant
>
>
>
>
>
>
>
>
>
>
>
>
>
> 
> From: David Winsemius 
>
> Cc: r-help@r-project.org
> Sent: Sat, November 20, 2010 8:54:32 PM
> Subject: Re: [R] density at particular values
>
>
> On Nov 20, 2010, at 8:07 PM, Shant Ch wrote:
>
> > Hello everyone!
> >
> > I want to use density function of R to compute the density at x(=0, say).
> But
> >it
> > is giving me the 5-number summary and mean of the data and densities at
> that
> > point.
> > I just want the densities at different values specified by me. Can anyone
> let
> >me
> > know how can I find that?
>
> Here's what you should have done (even before posting):
>
> ?density
> 
> "Value
> x   the n coordinates of the points where the density is estimated.
>
> y   the estimated density values. These will be non-negative, but can be
> zero."
>
> Realize that the "specified by me" part is either going to be modified to
> "pick
> an existing estimate near my specification" or that you will need to
> approximate
> the value. So what is the actual problem (and the actual data setup) ?
>
> --David.
>
>
> >
> > For example
> >
> >
> > Thanks in advance for your help.
> >
> >
> > Shant
> >
> >
> >
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@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.
>
> David Winsemius, MD
> West Hartford, CT
>
>
>
>[[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] density at particular values

2010-11-20 Thread Shant Ch
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.

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.

Shant
 













From: David Winsemius 

Cc: r-help@r-project.org
Sent: Sat, November 20, 2010 8:54:32 PM
Subject: Re: [R] density at particular values


On Nov 20, 2010, at 8:07 PM, Shant Ch wrote:

> Hello everyone!
> 
> I want to use density function of R to compute the density at x(=0, say). But 
>it
> is giving me the 5-number summary and mean of the data and densities at that
> point.
> I just want the densities at different values specified by me. Can anyone let 
>me
> know how can I find that?

Here's what you should have done (even before posting):

?density

"Value
x   the n coordinates of the points where the density is estimated.

y   the estimated density values. These will be non-negative, but can be zero."

Realize that the "specified by me" part is either going to be modified to "pick 
an existing estimate near my specification" or that you will need to 
approximate 
the value. So what is the actual problem (and the actual data setup) ?

--David.


> 
> For example
> 
> 
> Thanks in advance for your help.
> 
> 
> Shant
> 
> 
> 
> 
> [[alternative HTML version deleted]]
> 
> __
> R-help@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.

David Winsemius, MD
West Hartford, CT


  
[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] density at particular values

2010-11-20 Thread David Winsemius


On Nov 20, 2010, at 8:07 PM, Shant Ch wrote:


Hello everyone!

I want to use density function of R to compute the density at x(=0,  
say). But it
is giving me the 5-number summary and mean of the data and densities  
at that

point.
I just want the densities at different values specified by me. Can  
anyone let me

know how can I find that?


Here's what you should have done (even before posting):

?density

"Value
x   the n coordinates of the points where the density is estimated.

y   the estimated density values. These will be non-negative, but can  
be zero."


Realize that the "specified by me" part is either going to be modified  
to "pick an existing estimate near my specification" or that you will  
need to approximate the value. So what is the actual problem (and the  
actual data setup) ?


--
David.




For example


Thanks in advance for your help.


Shant




[[alternative HTML version deleted]]

__
R-help@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.


David Winsemius, MD
West Hartford, CT

__
R-help@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] density at particular values

2010-11-20 Thread Shant Ch
Hello everyone!

I want to use density function of R to compute the density at x(=0, say). But 
it 
is giving me the 5-number summary and mean of the data and densities at that 
point. 
I just want the densities at different values specified by me. Can anyone let 
me 
know how can I find that?

For example 


Thanks in advance for your help.


Shant



  
[[alternative HTML version deleted]]

__
R-help@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.