[R] Density

David L Carlson dcarlson at tamu.edu
Thu Aug 9 17:49:26 CEST 2012


Sorry, between x and X I got confused about what you were trying to do. The
quickest route is approx() or approxfun():

set.seed(42)
X <- rnorm(100)
den0 <- density(X, bw="SJ")
XY <- approx(den0$x, den0$y, X)

XY$y will differ slightly from your values (you could double the number of
points in the density() computation to m=1024, to get closer if you need to
– it depends on what you are using the results for):

Y <- sapply(1:100, function(i) fhat(X[i], X)
mean(Y-XY$y)
[1] -0.0002195961

sd(Y-XY$y)
[1] 5.682146e-05

plot(Y, XY$y, pch=4)
abline(0, 1)

I used your fhat function with sapply to save some steps, but it is possible
to speed things up by vectorizing the whole function:

Y1 <- 1/(length(X)*den0$bw)*colSums(dnorm((outer(X, X, "-")/den0$bw)))
identical(Y, Y1)
[1] TRUE

-------
David


From: li li [mailto:hannah.hlx at gmail.com] 
Sent: Thursday, August 09, 2012 8:55 AM
To: dcarlson at tamu.edu
Cc: r-help
Subject: Re: [R] Density

Hi David,
   Thanks a lot for the reply.
   I might not have stated the problem clearly. Let me try again.
  
   Given a set of observations X, I want to find out the estimated density 
values for the observations X?  
 
  I believe that the values "x" returned from "density" function is not the
observations
that are fed into the function and the returned "y" values are estimated
density values for "x".
   Below are in the R manual
 
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.
 
We can also check this using the code below.
 
X <- rnorm(100)
density(X)-> den0
den0
X[1:10]
(den0$x)[1:10]
(den0$y)[1:10]
round(dnorm((den0$x)[1:10]), 6)
round(dnorm(X[1:10]), 6)
 
Thank you.
     Hannah 
 
 
2012/8/8 David L Carlson <dcarlson at tamu.edu>
The numbers are there, they just aren't listed by the default print method
for density.  When you type the object name, den0, R runs
print.density(den0) which provides summary statistics. You need to look at
the manual page (?density) and pay close attention to the section labeled
"Value" which provides information about what values are returned by the
function.

str(den0)
den0$x
den0$y
plot(den0$x, den0$y, typ="l")

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of li li
> Sent: Wednesday, August 08, 2012 9:03 PM
> To: r-help
> Subject: [R] Density
>
> Dear all,
>    Given a set of observations X, I want to evaluate the kernel density
> estimator
> at these observed values. If I do the following, I do not get the those
> estimated values directly.
> Can anyone familiar with this give an idea on how to find out the
> estimated
> density values for X?
>
> > X <- rnorm(100)
>
> > density(X)-> den0
>
> > den0
>
>
> Call:
>
> density.default(x = X)
>
>
> Data: X (100 obs.); Bandwidth 'bw' = 0.354
>
>
>        x                 y
>
>  Min.   :-3.2254   Min.   :0.0002658
>
>  1st Qu.:-1.6988   1st Qu.:0.0359114
>
>  Median :-0.1721   Median :0.1438772
>
>  Mean   :-0.1721   Mean   :0.1635887
>
>  3rd Qu.: 1.3545   3rd Qu.:0.2866889
>
>  Max.   : 2.8812   Max.   :0.3776935
>
>
> I did write the code for the kernel density
>
> estimator myself. So once I find a proper bandwidth,
>
> I can use the following function. However, it would be nicer
>
> if there is a more direct way.
>
>
> > fhat <- function(x, X){
>
> +          h <- density(X, bw="SJ")$bw
>
> +          n <- length(X)
>
> +          1/(n*h)*sum(dnorm((x-X)/h))}
>
> >
>
> > est <- numeric(length(X)).
>
> > for (i in 1:length(X)){est[i] <- fhat(x=X[i], X=X)}
>
> >
>
> > est
>
>
>
>
> Thanks in advance.
>
>       Hannah
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list