R, there are some buildin
packages for implementing the local polynomial estimate. For example, the package
KernSmooth contains several functions.Function bkde() computes the kernel density
estimate and Function bkde2D() computes the 2D kernel density estimate.Function locpoly() is for the local polynomial fitting including a local polynomial estimate of the density of X.
# Define the local constant estimator with R
local.constant=function(y,x,z,h,ker){
# parameters: x=variable; h=bandwidth; z=grid point; ker=kernel
nz<-length(z)
nx<-length(x)
x0=rep(1,nx*nz)
dim(x0)=c(nx,nz)
x1=t(x0)
x0=x*x0
x1=z*x1
x0=x0-t(x1)
if(ker==1){x1=kernel(x0/h)} # Epanechnikov kernel
if(ker==0){x1=dnorm(x0/h)} # normal kernel
x2=y*x1
f1=apply(x1,2,mean)
f2=apply(x2,2,mean)
f3=f2/f1
return(f3)
}
|