Skip to content

Instantly share code, notes, and snippets.

@fawda123
Last active December 14, 2015 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fawda123/5166190 to your computer and use it in GitHub Desktop.
Save fawda123/5166190 to your computer and use it in GitHub Desktop.
loess_fitting
obs<-100
x<-runif(obs,0,100)
y<-x^2+rnorm(obs,0,800)
require(scales)
#examples with span
par(mfrow=c(2,2),mar=c(4,4,0.5,0.5),family='serif')
span<-round(seq(0.5,0.1,length=4),2)
for(i in span){
plot(x,y,bty="n",pch=19,cex=1.5,col=alpha('black',0.7),cex.lab=1.5)
mod<-loess(y~x,span=i,control = loess.control(surface = "direct"))
pred.vals<-predict(mod,data.frame(x = seq(min(x),max(x),length=500)))
lines(seq(min(x),max(x),length=500),pred.vals,lwd=2,col="red")
rmse.val<-round(sqrt(sum(mod$resid^2)/obs))
text(0,max(y),paste("\nspan",i,'\nRMSE',rmse.val),adj=0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment