#### Movie to show the effect of changes in the bandwidth for kernel density estimation ####
#### A chi-squared with 6 degress of freedom and sample size n=100 is simulated         ####
#### The bandiwidht is increased and then decreased in consecutive plots                #### 

library(ks)
#### x<-rchisq(100,6) 
x<-precip 
bandwidth<-hscv(x)
bandwidth<-0.01*bandwidth

B=600

for (k in 1:B)
{

plot(density(x,bw=bandwidth),type="l",xlab="N=70sumary(",xlim=c(0,80),ylim=c(0.00,0.08),col=2,lwd=3,main="Kernel density estimation")
abline(h=0.00)
rug(x)
bandwidth<-bandwidth*1.01
#### legend("top",legend=c("Kernel density estimation","Kernel estimator (Naive bootstrap)","Kernel estimator  (Smoothed bootstrap)"),lwd=c(3,1,1),col=c(2,4,1))

}


for (k in 1:B)
{

plot(density(x,bw=bandwidth),type="l",xlab="N=70sumary(",xlim=c(0,80),ylim=c(0.00,0.08),col=2,lwd=3,main="Kernel density estimation")
abline(h=0.00)
rug(x)
bandwidth<-bandwidth/1.01
#### legend("top",legend=c("Kernel density estimation","Kernel estimator (Naive bootstrap)","Kernel estimator  (Smoothed bootstrap)"),lwd=c(3,1,1),col=c(2,4,1))

}

for (k in 1:B)
{

plot(density(x,bw=bandwidth),type="l",xlab="N=70sumary(",xlim=c(0,80),ylim=c(0.00,0.08),col=2,lwd=3,main="Kernel density estimation")
abline(h=0.00)
rug(x)
bandwidth<-bandwidth*1.01
#### legend("top",legend=c("Kernel density estimation","Kernel estimator (Naive bootstrap)","Kernel estimator  (Smoothed bootstrap)"),lwd=c(3,1,1),col=c(2,4,1))

}


for (k in 1:B)
{

plot(density(x,bw=bandwidth),type="l",xlab="N=70sumary(",xlim=c(0,80),ylim=c(0.00,0.08),col=2,lwd=3,main="Kernel density estimation")
abline(h=0.00)
rug(x)
bandwidth<-bandwidth/1.01
#### legend("top",legend=c("Kernel density estimation","Kernel estimator (Naive bootstrap)","Kernel estimator  (Smoothed bootstrap)"),lwd=c(3,1,1),col=c(2,4,1))

}

for (k in 1:B)
{

plot(density(x,bw=bandwidth),type="l",xlab="N=70sumary(",xlim=c(0,80),ylim=c(0.00,0.08),col=2,lwd=3,main="Kernel density estimation")
abline(h=0.00)
rug(x)
bandwidth<-bandwidth*1.01
#### legend("top",legend=c("Kernel density estimation","Kernel estimator (Naive bootstrap)","Kernel estimator  (Smoothed bootstrap)"),lwd=c(3,1,1),col=c(2,4,1))

}


for (k in 1:B)
{

plot(density(x,bw=bandwidth),type="l",xlab="N=70sumary(",xlim=c(0,80),ylim=c(0.00,0.08),col=2,lwd=3,main="Kernel density estimation")
abline(h=0.00)
rug(x)
bandwidth<-bandwidth/1.01
#### legend("top",legend=c("Kernel density estimation","Kernel estimator (Naive bootstrap)","Kernel estimator  (Smoothed bootstrap)"),lwd=c(3,1,1),col=c(2,4,1))

}

