Skip to content

Density

5 messages · Li Li, William Dunlap, David L Carlson

#
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
#
You can use approx(d, xout=) (or spline()) on the output of density()
to get density estimates at the points in xout.  E.g.,
   > X <- c(rnorm(20, -1, 1), rgamma(50,1/2))
   > d <- density(X)
   > plot(d)
   > points(approx(d, xout=-2:2))

Or you could use the functions in package:logspline to fit the density
function and evaluate it where you wish
  > z <- logspline(X)
  > points(-2:2, dlogspline(-2:2, fit=z), col="red")

(The vector '-2:2' about could be any set of numbers, including your
original data points.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
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