if (FALSE) {
## tensor product `tesmd2' example
## simulating data...
require(scam)
set.seed(2)
n <- 30
x1 <- sort(runif(n)); x2 <- sort(runif(n)*4-1)
f <- matrix(0,n,n)
for (i in 1:n) for (j in 1:n)
f[i,j] <- 2*sin(pi*x1[i])-exp(4*x2[j])/(1+exp(4*x2[j]))
f <- as.vector(t(f))
y <- f+rnorm(length(f))*.2
x11 <- matrix(0,n,n)
x11[,1:n] <- x1
x11 <- as.vector(t(x11))
x22 <- rep(x2,n)
dat <- list(x1=x11,x2=x22,y=y)
## fit model ...
b <- scam(y~s(x1,x2,bs="tesmd2",k=10),data=dat)
## plot results ...
old.par <- par(mfrow=c(2,2),mar=c(4,4,2,2))
plot(b,se=TRUE)
plot(b,scheme=1,theta = 30, phi = 40)
plot(y,b$fitted.values,xlab="Simulated data",ylab="Fitted data")
par(old.par)
vis.scam(b,theta = 40, phi = 20)
## example with cyclic cubic regression spline along the 1st covariate...
set.seed(4)
n <- 30
x1 <- sort(runif(n)); x2 <- sort(runif(n)*4-1)
f <- matrix(0,n,n)
for (i in 1:n) for (j in 1:n)
f[i,j] <- sin(2*pi*x1[i])-exp(4*x2[j])/(1+exp(4*x2[j]))
f <- as.vector(t(f))
y <- f+rnorm(length(f))*.2
x11 <- matrix(0,n,n)
x11[,1:n] <- x1
x11 <- as.vector(t(x11))
x22 <- rep(x2,n)
dat <- list(x1=x11,x2=x22,y=y)
## fit model ...
b1 <- scam(y~s(x1,x2,bs="tesmd2",xt=list("cc"),k=10), data=dat)
## plot results ...
old.par <-par(mfrow=c(2,2))
plot(b1,se=TRUE)
plot(b1,scheme=1,theta = 30, phi = 40)
plot(y,b1$fitted.values,xlab="Simulated data",ylab="Fitted data")
par(old.par)
vis.scam(b1,theta=40,phi=20)
}
Run the code above in your browser using DataLab