[R] Getting a cdf equal to 1 from a variable kernel density estimation
Lionel Delmas
lioneldelm at gmail.com
Wed May 27 11:43:26 CEST 2015
When I integrate the variable kernel density estimation of a sample from
-inf to inf, I should get 1. But it is not what I get with my code below. I
find a value higher than 2. How can I fix this?
n<-1000
df <- data.frame(x=unlist(lapply(1, function(i) rnorm(n, 0,sd=1))))
df<-as.data.frame(df[order(df$x),])
names(df)[1]<-"x"
library(functional)
gaussianKernel <- function(u, h) exp(-sum(u^2)/(2*h^2))
densityFunction <- function(x, df, ker, h){
difference = t(t(df) - x)
W = sum(apply(difference, 1, ker, h=h))
W/(nrow(df)*(h^(length(df))))}
myDensityFunction <- Curry(densityFunction, df=df, ker=gaussianKernel , h=2)
vect<-vector()for (i in 1:length(df$x)){
f<-myDensityFunction(df$x[i])
vect<-c(vect,f)}
plot(df$x,vect,ylim=c(0,1),xlim=c(-5,5),type="l")
f <- approxfun(df$x, vect, yleft = 0, yright = 0)
integrate(f, -Inf, Inf)
Thanks
[[alternative HTML version deleted]]
More information about the R-help
mailing list