## Local monotone increasing LSCOP-spline example...
## simulating data...
require(scam)
set.seed(4)
n <- 200
x <- sort(runif(n)*6)
f <- 4*tanh(2*x-5)-5*exp(-(x-4)^2) ## increasing up untill xt=2.978
xc <- 2.978
y <- f+rnorm(n)*.7
old.par <- par(mfrow=c(1,2))
plot(x,y,cex=.5)
lines(x,f,lty=2); abline(v=xc,lty=2,col="red")
## fit model ...
b <- scam(y~s(x,bs="lmpi",xt=list(xc=xc),k=15))
summary(b)
lines(x,fitted(b),col=2,lwd=2)
plot(b,shade=TRUE)
par(old.par)
## unconstrained model to compare with...
g <- scam(y~s(x))
plot(x,y,cex=.5)
lines(x,f,lty=2); abline(v=xc,lty=2,col=2)
lines(x,fitted(g),col=4,lwd=2)
lines(x,fitted(b),col=2,lwd=1)
## example with bs="lipl"...
set.seed(5)
n <- 200
x <- sort(runif(n)*4-1)
f <- 1.5*(1-exp(-2*x))
xc <- 1.5
y <- f+rnorm(n)*1.5
b <- scam(y~s(x,bs="lipl",xt=list(xc=xc)))
g <- scam(y~s(x,bs="ps")) ## unconstrained
old.par <- par(mfrow=c(2,2))
plot(x,y,cex=.5)
lines(x,f,lty=2); abline(v=xc,lty=2,col="red")
lines(x,fitted(b),col=2,lwd=2)
plot(b,shade=TRUE)
plot(g,shade=TRUE)
par(old.par)
Run the code above in your browser using DataLab