## simulating data...
require(scam)
n <- 200
set.seed(1)
x0 <- rep(1:4,50)
x1 <- runif(n)*6-3
f1 <- 3*exp(-x1^2) # unconstrained smooth term
x2 <- runif(n)*4-1;
f2 <- exp(4*x2)/(1+exp(4*x2)) # monotone increasing smooth
x3 <- runif(n)*5;
f3 <- -log(x3)/5 # monotone decreasing smooth
f <- f1+f2+f3
y <- 2*x0 + f + rnorm(n)*.3
x0 <- factor(x0)
## fit the model and plot ...
b <- scam(y~x0+s(x1,k=15,bs="cr")+s(x2,k=30,bs="mpi")+s(x3,k=30,bs="mpd"))
plot(b,pages=1,residuals=TRUE,all.terms=TRUE,shade=TRUE,shade.col=3)
## same with EFS and BFGS methods for smoothing parameter and models coefficients estimations...
b <- scam(y~x0+s(x1,k=15,bs="cr")+s(x2,k=30,bs="mpi")+s(x3,k=30,bs="mpd"),optimizer=c("efs","bfgs"))
plot(b,pages=1,residuals=TRUE,all.terms=TRUE,shade=TRUE,shade.col=3)
if (FALSE) {
## example with 2-d plots...
## simulating data...
set.seed(2)
n <- 30
x0 <- rep(1:9,100)
x1 <- sort(runif(n)*4-1)
x2 <- sort(runif(n))
x3 <- runif(n*n, 0, 1)
f <- matrix(0,n,n)
for (i in 1:n) for (j in 1:n)
{ f[i,j] <- -exp(4*x1[i])/(1+exp(4*x1[i]))+2*sin(pi*x2[j])}
f1 <- as.vector(t(f))
f2 <- x3*0
e <- rnorm(length(f1))*.1
y <- 2*x0 + f1 + f2 + e
x0 <- factor(x0)
x11 <- matrix(0,n,n)
x11[,1:n] <- x1
x11 <- as.vector(t(x11))
x22 <- rep(x2,n)
dat <- list(x0=x0,x1=x11,x2=x22,x3=x3,y=y)
## fit model and plot ...
b <- scam(y~x0+s(x1,x2,k=c(10,10),bs=c("tesmd1","ps"),m=2)+s(x3),data=dat,optimizer="efs")
op <- par(mfrow=c(2,2))
plot(b,all.terms=TRUE)
plot(y,b$fitted.values,xlab="Simulated data",ylab="Fitted data",pch=19,cex=.3)
par(op)
## and use of schemes...
op <- par(mfrow=c(2,2))
plot(b,all.terms=TRUE,scheme=1)
par(op)
op <- par(mfrow=c(2,2))
plot(b,all.terms=TRUE,scheme=c(2,1))
par(op)
}
Run the code above in your browser using DataLab