require(mgcv)
set.seed(1)
ff <- function(x, z, sx = 0.3, sz = 0.4) {
(pi^sx * sz) * (1.2 * exp(-(x - 0.2)^2/sx^2 - (z -
0.3)^2/sz^2) + 0.8 * exp(-(x - 0.7)^2/sx^2 -
(z - 0.8)^2/sz^2))
}
n <- 800
x <- runif(n)
z <- runif(n)
f <- ff(x,z)
v <- (x-.4)^2+z*.2
y <- f + rnorm(n)*.2
plot(v,x)
par(mfrow=c(2,2)); method <- "REML"
## list specifying tensor product of cubic B-splines
## and 2nd order TPS penalty
M = list(rep(3,2),matrix(c(2,0,1,0,2,1,1,1,2),3,3))
b <- gam(y~s(x,v,bs="bt",m=M,k=15),method=method) ## TPS penalty
plot(b,scheme=2)
## Actually the same is built in...
b <- gam(y~s(x,v,bs="bt",m=3.2,k=15),method=method) ## TPS penalty
plot(b,scheme=2)
length(coef(b)) ## note: < 15^2 = 225
## product of 4th and cubic spline with 3rd and 2nd order penalties
b <- gam(y~s(x,v,bs="bt",m=c(4,3),k=15),method=method)
plot(b,scheme=2)
b$sp
## product of cubic splines with first order penalties
b <- gam(y~s(x,v,bs="bt",m=cbind(c(3,3),c(1,1)),k=15),method=method)
plot(b,scheme=2)
Run the code above in your browser using DataLab