Re: [R] help in density estimation

2010-09-23 Thread Duncan Murdoch

 On 23/09/2010 11:42 AM, wangguojie2006 wrote:

b-runif(1000,0,1)
f-density(b)


f is a list of things, including x values where the density is computed, 
and y values for the density there.  So you could do it by linear 
interpolation using approx or approxfun.  For example


 b - runif(1000,0,1)
 flist - density(b)
 f - approxfun(flist$x, flist$y)
 f(0.2)
[1] 0.9717893
 f(-1)
[1] NA

If you don't like the NA for an out-of-range argument, then choose 
something different from the default for the rule argument to approxfun.


Duncan Murdoch

__
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] help in density estimation

2010-09-23 Thread Ted Harding
On 23-Sep-10 16:52:09, Duncan Murdoch wrote:
   On 23/09/2010 11:42 AM, wangguojie2006 wrote:
 b-runif(1000,0,1)
 f-density(b)
 
 f is a list of things, including x values where the density is
 computed, 
 and y values for the density there.  So you could do it by linear 
 interpolation using approx or approxfun.  For example
 
   b - runif(1000,0,1)
   flist - density(b)
   f - approxfun(flist$x, flist$y)
   f(0.2)
 [1] 0.9717893
   f(-1)
 [1] NA
 
 If you don't like the NA for an out-of-range argument, then choose 
 something different from the default for the rule argument to
 approxfun.
 
 Duncan Murdoch

Or, perhaps more transparently (and more explicitly modifiable):

  b-runif(1000,0,1)
  f - density(b, from=0, to=1, n=512)
  plot(f$x, f$y, type=l, col=blue,
   xlim=c(0,1), ylim=c(0,1.5)) ## Plot the density estimate
  x0 - 0.5## Target value of x
  i0 - max(which(f$x = x0))
  i1 - min(which(f$x  x0))
  u0 - f$x[i0] ; v0 - f$y[i1]
  u1 - f$x[i1] ; v1 - f$y[i1]
  y0 - v0 + (v1-v0)*(x0-u0)/(u1-u0)   ## Linear interpolation
  points(x0, y0, pch=+, col=red)   ## Add interpolated point

Ted.


E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk
Fax-to-email: +44 (0)870 094 0861
Date: 23-Sep-10   Time: 18:05: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] help in density estimation

2010-09-23 Thread Greg Snow
You could do:

b - runif(1, 0, 1)
tmp - density(b, from=0.5, to=0.5, n=1)
tmp$y

As one direct approach.

You could also look at the logspline package for an alternative for density 
estimation that provides you with a density function (and also allows for 
finite  domains).

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of wangguojie2006
 Sent: Thursday, September 23, 2010 9:43 AM
 To: r-help@r-project.org
 Subject: [R] help in density estimation
 
 
 Hi, guys,
 
 I'm using kernel density estimation. But how can I return to a
 density
 estimation for a fixed point?
 
 For example,
 
 b-runif(1000,0,1)
 f-density(b)
 
 How can I get the value of density(b) at b=0.5?
 
 Your help is extremely appreciated. Thanks.
 
 Jay
 --
 View this message in context: http://r.789695.n4.nabble.com/help-in-
 density-estimation-tp2552264p2552264.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 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] help in density estimation

2010-09-23 Thread wangguojie2006

You guys are really good.
Thanks.
-- 
View this message in context: 
http://r.789695.n4.nabble.com/help-in-density-estimation-tp2552264p2552484.html
Sent from the R help mailing list archive at Nabble.com.

__
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] help in density estimation

2010-09-23 Thread Ted Harding
There was a typo error in my code below. See the inserted correction.

On 23-Sep-10 17:05:45, Ted Harding wrote:
 On 23-Sep-10 16:52:09, Duncan Murdoch wrote:
   On 23/09/2010 11:42 AM, wangguojie2006 wrote:
 b-runif(1000,0,1)
 f-density(b)
 
 f is a list of things, including x values where the density is
 computed, 
 and y values for the density there.  So you could do it by linear 
 interpolation using approx or approxfun.  For example
 
   b - runif(1000,0,1)
   flist - density(b)
   f - approxfun(flist$x, flist$y)
   f(0.2)
 [1] 0.9717893
   f(-1)
 [1] NA
 
 If you don't like the NA for an out-of-range argument, then choose 
 something different from the default for the rule argument to
 approxfun.
 
 Duncan Murdoch
 
 Or, perhaps more transparently (and more explicitly modifiable):
 
  b-runif(1000,0,1)
  f - density(b, from=0, to=1, n=512)
  plot(f$x, f$y, type=l, col=blue,
   xlim=c(0,1), ylim=c(0,1.5)) ## Plot the density estimate
  x0 - 0.5## Target value of x
  i0 - max(which(f$x = x0))
  i1 - min(which(f$x  x0))
##u0 - f$x[i0] ; v0 - f$y[i1]## WRONG!
  u0 - f$x[i0] ; v0 - f$y[i0]## Correction
  u1 - f$x[i1] ; v1 - f$y[i1]
  y0 - v0 + (v1-v0)*(x0-u0)/(u1-u0)   ## Linear interpolation
  points(x0, y0, pch=+, col=red)   ## Add interpolated point
 
 Ted.
 
 
 E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk
 Fax-to-email: +44 (0)870 094 0861
 Date: 23-Sep-10   Time: 18:05:41
 -- XFMail --


E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk
Fax-to-email: +44 (0)870 094 0861
Date: 23-Sep-10   Time: 20:38:53
-- 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.