dens<-function(x,kernel="Epanechnikov",lambda=1,npt=100) { r<-max(x)-min(x) xmax<-max(x)+.1*r xmin<-min(x)-.1*r n<-length(x) xgrid<-seq(from=xmin,to=xmax,length=npt) myy<-NULL for (i in 1:npt) { bm<-(rep(xgrid[i],n)-x)/lambda if(kernel=="Gaussian") bm<-dnorm(bm) # The other kernels in use are defined only on [-1,1], and are zero at 1. # This command will zero out everything not in the interval [-1,1] else { bm<-ifelse(abs(bm)>1,1,bm) if(kernel=="Epanechnikov") bm<-3*(1-bm^2)/4 else if(kernel=="Triangular") bm<-1-abs(bm) else if(kernel=="Biweight") bm<-15*(1-bm^2)^2/16 else return("Unknown kernel\n") } myy<-c(myy,sum(bm*x)) } plot(xgrid, myy,type="l",ylab=" ", xlab=" ") cbind(xgrid,myy) }