# produces first figure
plot(all.x,all.y,xlab="x",ylab="y")
axis(1,at=all.x,labels=FALSE,col="grey")
axis(2,at=all.y,labels=FALSE,col="grey")
	# Re-use these commands for most of the other
	# figures

# add the sample mean
abline(h=mean(all.y),lty=3)
# add the regression line
fit.all = lm(all.y~all.x)
abline(a=fit.all$coefficients[1],b=fit.all$coefficients[2])

# Add k-nearest-neighbors curves
library(knnflex)
all.dist = knn.dist(c(all.x,seq(from=0,to=1,length.out=100)))
all.nn1.predict = knn.predict(1:110,111:210,all.y,all.dist,k=1)
abline(h=mean(all.y),lty=2)
lines(seq(from=0,to=1,length.out=100),all.nn1.predict,col="blue")
all.nn3.predict = knn.predict(1:110,111:210,all.y,all.dist,k=3)
lines(seq(from=0,to=1,length.out=100),all.nn3.predict,col="red")
all.nn5.predict = knn.predict(1:110,111:210,all.y,all.dist,k=5)
lines(seq(from=0,to=1,length.out=100),all.nn5.predict,col="green")
all.nn20.predict = knn.predict(1:110,111:210,all.y,all.dist,k=20)
lines(seq(from=0,to=1,length.out=100),all.nn20.predict,col="purple")

# Add kernel-smoothing curves
lines(ksmooth(all.x, all.y, "normal", bandwidth=2),col="blue",lty=2)
lines(ksmooth(all.x, all.y, "normal", bandwidth=1),col="red",lty=2)
lines(ksmooth(all.x, all.y, "normal", bandwidth=0.1),col="green",lty=2)
lines(ksmooth(all.x, all.y, "box", bandwidth=2),col="blue")
lines(ksmooth(all.x, all.y, "box", bandwidth=1),col="red")
lines(ksmooth(all.x, all.y, "box", bandwidth=0.1),col="green")

# Define a function which is nearly constant, but with rapid small
# oscillations
ugly.func = function(x) {1 + 0.01*sin(100*x)}
# Scatter-plot of ugly.func + noise
r = runif(100)
r.y = ugly.func(r) + rnorm(length(r),0,0.5)
plot(r,r.y,xlab="x",ylab="y")
# Add the true regression function
curve(ugly.func,add=TRUE)
# Add the mean line in red
abline(h=mean(r.y),col="red")
sine.fit = lm(r.y ~ 1+ sin(100*r))
fitted.sine = function(x) {
  sine.fit$coefficients[1] + sine.fit$coefficients[2]*sin(100*x)
}
curve(fitted.sine(x),col="blue",add=TRUE)
