[R] density estimation: compute sum(value * probability) for
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Sun Nov 14 09:50:53 CET 2004
On 13-Nov-04 bogdan romocea wrote:
> Dear R users,
>
> However, how do I compute sum(values*probabilities)? The
> probabilities produced by the density function sum to only 26%:
>> sum(den$y)
> [1] 0.2611142
>
> Would it perhaps be ok to simply do
>> sum(den$x*den$y) * (1/sum(den$y))
> [1] 1073.22
> ?
What you're missing is the "dx"! A density estimation estimates
the probability density function g(x) such that int[g(x)*dx] = 1,
and R's 'density' function returns estimated values of "g" at a
discrete set of points.
An integral can be approximated by a discrete summation of the
form
sum(g(x.i)*delta.x
You can recover the set of x-values at which the density is estimated,
and hence the implicit value of delta.x, from the returned density.
Example:
X<-rnorm(1000)
f<-density(X)
x<-f$x
delta.x<-x[2]-x[1]
g<-f$y
sum(g*delta.x)
[1] 1.000976
Hoping this helps,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861 [NB: New number!]
Date: 14-Nov-04 Time: 08:50:53
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list