I would like to program a kernel estimate (with Epanechnikov kernel^1 for example). I tried the following code^2 by putting the manual code (blue) and the default code (red) on the same figure (see attached) but it always gives a difference between the two density curves!
1: The analytic form of the Epanechnikov kernel is: kappa(u) = (1-u^2), support |u| <=1, with u = (x-x_{i})/h.
2: My trial code:
x= faithful$eruptions
fit2 <- density(x, bw = 0.6, kernel = "epanechnikov")
xgrid = seq(-1, 8, 0.1)
kernelEpan <- function(x, obs, h) sum((1-((x-obs)/h)^2)*(abs(x-obs)<=h))/h
plot(xgrid, sapply(xgrid, FUN = kernelEpan, obs = faithful$eruptions, h = 0.6)/length(faithful$eruptions), type = "l", col = "blue")
lines(fit2, col = "red")

