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]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.