# Programs used for graphics and tables of the book: # "Data anlysis and data mining" by A.Azzalini and B.Scarpa, # © Oxford University Press, 2012 (ISBN 978-0-19-976710-6). # # Code regarding section 4.5 (© 2003, 2004, 2012 A.Azzalini and B.Scarpa #------------------------------------------------------------------------ source("base-www.R") library(sm) # auto <- read.table("auto.dat", header=TRUE) attach(auto) y <- city.distance x1 <- engine.size x2 <- curb.weight xlab<-"Engine size (L)" ylab<-"Curb weight (kg)" zlab<- "City distance (km/L)" #----- library(gam) a2<- gam(city.distance ~ s(engine.size) + s(curb.weight), data=auto) pa2 <- preplot(a2) name<- "figure 4.12a" plot(pa2$"s(engine.size)", se=TRUE, ylim=c(-7,10), xlab="Engine size",ylab="s(Engine size)") pause(name) # name<- "figure 4.12b" plot(pa2$"s(curb.weight)", se=TRUE,ask=TRUE, ylim=c(-7,10), xlab="Curb weight",ylab="s(Curb weight)") pause(name) #----------------------------------------- x1 <- seq(min(engine.size), max(engine.size), length=50) x2 <- seq(min(curb.weight), max(curb.weight), length=50) x12<- data.frame( engine.size = as.vector(outer(x1,rep(1,length(x1)))), curb.weight=as.vector(outer(rep(1,length(x2)),x2))) a2p <- predict(a2, x12) sm.options(ngrid=50) h<- c(0.5, 150) # # name<-"figure 4.13a" matrice.gam<-matrix(a2p,50,50) w1<-convex.hull(cbind(engine.size,curb.weight),cbind(x1,x2)) persp(x1, x2, matrice.gam*w1, theta=120, phi=20, xlab="Engine size", ylab="Curb weight", zlab="City distance", ticktype="detailed") pause(name) #---- name<-"figure 4.13b" sm <- sm.regression(cbind(engine.size, curb.weight), city.distance, h=h, options=list(xlab=xlab, ylab=ylab, zlab=zlab), display="none") matrice.sm<-matrix(sm$estimate,50,50) persp(x1, x2, matrice.sm, theta=120, phi=20, xlab="Engine size", ylab="Curb weight", zlab="City distance", ticktype="detailed") pause(name) # detach(package:gam) detach(package:splines) detach(package:sm) detach.all()