dyluns
dyluns

Reputation: 155

How to get the explicit form of an estimated density function

I'm doing density estimation in R.

I am trying to use density() to do a kernel density estimation. After this, I want to evaluate its performance. However, some criteria require the knowledge of the explicit form of the estimated density function and I don't know how to get it.

Currently, I'm thinking of using data points and their densities to express the estimated density function approximately (like a piecewise function).

Is there a better way to do this? More generally, is it possible to retrieve the explicit form of a function based on its graph in R?

Update:

Consider this criteria:

ISE=Integral[(f_hat-f)^2]

where f_hat is the estimated density and f is the true density.

Upvotes: 1

Views: 206

Answers (1)

Stéphane Laurent
Stéphane Laurent

Reputation: 84709

You can do that with the kde1d package. The main function of this package is kde1d. It is better than density to estimate a density.

library(kde1d)

set.seed(666)
x <- rnorm(100) # simulate some data
fit <- kde1d(x) # estimate density
d <- dkde1d(0, fit) # evaluate density estimate at 0
d
# 0.383205
dnorm(0)
# 0.3989423

The function is vectorized:

d <- dkde1d(c(-1,0,1), fit)
d
# 0.2675120 0.3832050 0.2213388

Upvotes: 1

Related Questions