if (FALSE) {
## Monotone increasing and convex SCOP-splines example
## simulating data...
set.seed(1)
n <- 100
x <- runif(n)*2
f <- 5*x^2/8
y <- rpois(n,exp(f))
dat <- data.frame(x=x,y=y)
## fit model ...
b <- scam(y~s(x,bs="micx"),family=poisson,data=dat)
## fit unconstrained model ...
b1 <- scam(y~s(x,bs="cr"),family=poisson,data=dat)
## plot results ...
plot(x,y,xlab="x",ylab="y",cex=.5)
x1 <- sort(x,index=TRUE)
lines(x1$x,exp(f)[x1$ix]) ## the true function
lines(x1$x,b$fitted.values[x1$ix],col=2) ## mixed constrained fit
lines(x1$x,b1$fitted.values[x1$ix],col=3) ## unconstrained fit
## numeric 'by' variable example...
set.seed(10)
n <- 100
x <- runif(n)*2
f <- x^2
z <- runif(n,-2,3)
y <- f*z + rnorm(n)*0.4
dat <- data.frame(x=x,z=z,y=y)
b <- scam(y~s(x,by=z,bs="micxBy"),data=dat)
summary(b)
par(mfrow=c(1,2))
plot(b,shade=TRUE)
## unconstrained fit...
b1 <- scam(y~s(x,by=z),data=dat)
plot(b1,shade=TRUE)
summary(b1)
}
Run the code above in your browser using DataLab