library(car) attach(Prestige) plot(income,prestige,xlab="Average Income",ylab="Prestige") lines(lowess(income,prestige,f=0.5,iter=0),lwd=2) mod.lo.inc<-loess(prestige~income,span=0.7,degree=1) mod.lo.inc inc.100<-seq(min(income),max(income),len=100) pres<-predict(mod.lo.inc,data.frame(income=inc.100)) plot(income,prestige) lines(inc.100,pres,lty=2,lwd=2) library(splines) lines(smooth.spline(income,prestige,df=3.85),lwd=2) legend("bottomright",c("loess","smoothing splines"),lty=c(2,1),lwd=c(2,2)) library(mgcv) mod.gam<-gam(prestige~s(income)+s(education)) mod.gam inc<-seq(min(income),max(income),len=25) ed<-seq(min(education),max(education),len=25) newdata<-expand.grid(income=inc,education=ed) fit.prestige<-matrix(predict(mod.gam,newdata),25,25) persp(inc,ed,fit.prestige,theta=45,phi=30,ticktype="detailed",xlab="Income",ylab="Education",zlab="Prestige",expand=2/3,shade=0.5) par(mfrow=c(1,2)) plot(mod.gam)