#### Kernel density estimator for the original sample, a naive bootstrap resample and a smooth bootstrap ressmple ####
#### for a sample of size n=100 from a chi-squared fistribution with 6 degrees of freedom                         ####
library(ks)
x<-rchisq(100,6) 
plot(density(x,bw="nrd0"),type="l",xlab="N=100",xlim=c(0,25),ylim=c(0.00,0.16),col=2,lwd=3,main="Función de densidad (N(0,1))")
abline(h=0.00)
rug(x)
lines(density(x,bw="ucv"),col=4)
lines(density(x,bw="bcv"),col=1)
bandwidth<-hscv(x)
lines(density(x,bw=bandwidth),lwd=2,col=3)
legend("top",legend=c("Kernel estimator (original sample)","Kernel estimator (Naive bootstrap)","Kernel estimator (Smooth boottrap)"),lwd=c(3,1,1),col=c(2,4,1))

n=length(x)


remuestranp<-NULL
B=1000
for (k in 1:B)
{
for (i in 1:n)
{
j=1
u=runif(1)
while (u>=j/n)
{
j=j+1
}
remuestranp[i]=x[j]
}
remuestrasu=remuestranp+0.1*rnorm(n,bandwidth)

plot(density(x,bw="nrd0"),type="l",xlab="N=100",xlim=c(0,25),ylim=c(0.00,0.16),col=2,lwd=3,main="Chi-square 6 d.f. density function")
abline(h=0.00)
rug(x)
lines(density(remuestranp,bw="nrd0"),col=4)
lines(density(remuestrasu,bw="nrd0"),col=1)
legend("top",legend=c("Kernel estimator (original sample)","Kernel estimator (Naive bootstrap)","Kernel estimator (Smooth bootstrap)"),lwd=c(3,1,1),col=c(2,4,1))

}

