###########################################################
n=1000
h=0.45
#生成data###############################################
xi=rnorm(n, mean=0, sd=1)
##h=1.06*sd(xi)*n^(-1/5)
###############ker#############################################
ker=function(u)
{
kern=0.75*(1-u^2)*(abs(u)<=1)
return(kern)
}
#############密度估计####################################
hatfx=function(x)
{
hf=sum(ker((xi-x)/h))/(n*h)
return(hf)
}
#################plot######################################
ira=30
a=range(xi)[1]
b=range(xi)[2]
x0=seq(a,b,length=ira)
hatf=c()
for(i in 1:ira)
hatf[i]=hatfx(x0[i])
plot(x0,hatf,type="l",col=1,xlim=c(a,b),ylim=c(0,0.5),xlab="x",lwd=2,
ylab="hatf(x)",cex.lab=1.5,cex.axis=1.5)
lines(x0,(1/sqrt(2*pi))*exp(-x0^2/2),lty=3,col=1,lwd=2)