user2246905
user2246905

Reputation: 1039

normalizing constant from kernel density in R

How can I get the normalizing constant from a non-standard distribution. Ex:

x <- c(rnorm(500,10,1),rnorm(500,20,2),rnorm(500,35,1))

after using density in R

dens<-density(x,n=length(x),adjust=0.4)

Upvotes: 2

Views: 2987

Answers (1)

WhiteViking
WhiteViking

Reputation: 3201

The result of density() should be a proper probability density function, so the area under the curve returned by density() should already be 1.

Your example:

set.seed(1)
x <- c(rnorm(500,10,1), rnorm(500,20,2), rnorm(500,35,1))
dens <- density(x, n=length(x), adjust=0.4)

We can get an approximation of the area under the pdf by numerically integrating it:

install.packages("sfsmisc")
library(sfsmisc)
integrate.xy(dens$x, dens$y)

Which gives

[1] 1.000241

The area is indeed rather close to 1.

There are numerical accuracy issues however which may cause this area to deviate significantly from 1. They are discussed here for example.

Upvotes: 6

Related Questions