library(ElemStatLearn);data(ozone,package="ElemStatLearn")
ozone<-ozone[order(ozone$ozone),]
head(ozone)
## ozone radiation temperature wind
## 17 1 8 59 9.7
## 19 4 25 61 9.7
## 14 6 78 57 18.4
## 45 7 48 80 14.3
## 106 7 49 69 10.3
## 7 8 19 61 20.1
We want to predict the temperature with a function of ozone. ### Bagged loess
ll<-matrix(NA,nrow=10,ncol=155)
for(i in 1:10){
ss<-sample(1:dim(ozone)[1],replace=T)
ozone0<-ozone[ss,]
ozone0<-ozone0[order(ozone0$ozone),]
loess0<-loess(temperature~ozone,data=ozone0,span=0.2)
ll[i,]<-predict(loess0,newdata=data.frame(ozone=1:155))
}
We plot the results of the code above.
plot(ozone$ozone,ozone$temperature,pch=10,cex=0.5)
for(i in 1:10){lines(1:155,ll[i,],col="grey",lwd=2)}
lines(1:155,apply(ll,2,mean),col="red",lwd=2)
In the plot above, the black dots represent the original data; the 10 grey lines the loess fits of each resampling; the red line, the average of the 10 grey lines.