# [R] Density Estimation

Wolski wolski at molgen.mpg.de
Wed Sep 15 16:00:05 CEST 2004

```Hi!

The function density returns you a object of class density.
This object has an x and an y attribute which you can access by x y,
Hi!

Use approx and runif.

eg.:

dd<-density(rnorm(100,3,5))
plot(dd)

Using the function ?approx you can compute the density value for any x.
#the x is a dummy here.
mydist<-function(x,dd)
{

while(1)
{
tmp <- runif(1,min=min(dd\$x),max=max(dd\$x))
lev <- approx(dd\$x,dd\$y,tmp)\$y
if(runif(1,c(0,1)) <= lev)
{
return(tmp)
}
}
}

x <- 0
mydist(x,dd)

res<-rep(0,500)
res<-sapply(res,mydist,dd)
lines(density(res),col=2)

/E.

On 9/15/2004 at 12:36 PM Brian Mac Namee wrote:

>>>Hi there,
>>>
>>>Sorry if this is a rather loing post. I have a simple list of single
>>>feature data points from which I would like to generate a probability
>>>that an unseen point comes from the same distribution. To do this I am
>>>trying to estimate the probability density of the list of points and
>>>use this to generate a probability for the new unseen points. I have
>>>managed to use the R density function to generate the density estimate
>>>but have not been able to do anything with this - i.e. generate a
>>>rpobability that a new point comes from the same distribution. Is
>>>there a function to do this, or am I way off the mark using the
>>>density function at all?
>>>
>>>
>>>Brian.
>>>
>>>______________________________________________
>>>R-help at stat.math.ethz.ch mailing list
>>>https://stat.ethz.ch/mailman/listinfo/r-help

Dipl. bio-chem. Witold Eryk Wolski             @         MPI-Moleculare Genetic
Ihnestrasse 63-73 14195 Berlin                'v'
tel: 0049-30-83875219                        /   \
mail: witek96 at users.sourceforge.net        ---W-W----    http://www.molgen.mpg.de/~wolski
wolski at molgen.mpg.de

```